BRL-CAD
affinity.c
Go to the documentation of this file.
1 /* A F F I N I T Y . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE /* must come before common.h */
23 #endif
24 
25 #include "common.h"
26 
27 #ifdef HAVE_SCHED_H
28 # include <sched.h>
29 #endif
30 
31 #ifdef HAVE_SYS_CPUSET_H
32 # include <sys/cpuset.h>
33 #endif
34 
35 #ifdef HAVE_PTHREAD_H
36 # include <pthread.h>
37 #endif
38 
39 #ifdef HAVE_PTHREAD_NP_H
40 # include <pthread_np.h>
41 #endif
42 
43 #ifdef HAVE_MACH_THREAD_POLICY_H
44 # include <mach/thread_policy.h>
45 # include <mach/mach.h>
46 #endif
47 
48 #ifdef HAVE_WINDOWS_H
49 # include <windows.h>
50 #endif
51 
52 #include "bu/parallel.h"
53 #include "bu/log.h"
54 
55 int
57 {
58 #if defined(HAVE_PTHREAD_H) && defined(CPU_ZERO)
59 
60  /* Linux and BSD pthread affinity */
61 
62 #ifdef HAVE_CPU_SET_T
63  cpu_set_t set_of_cpus; /* bsd */
64 #else
65  cpuset_t set_of_cpus; /* linux */
66 #endif
67  int ret;
68  int ncpus = bu_avail_cpus();
69 
70  CPU_ZERO(&set_of_cpus);
71 
72  /* Set affinity to a single CPU core */
73  CPU_SET(cpu % ncpus, &set_of_cpus);
74  ret = pthread_setaffinity_np(pthread_self(), sizeof(set_of_cpus), &set_of_cpus);
75 
76  return ret;
77 
78 #elif defined(HAVE_MACH_THREAD_POLICY_H)
79 
80  /* Mac OS X mach thread affinity hinting. Mach implements a CPU
81  * affinity policy by default so this just sets up an additional
82  * hint on how threads can be grouped/ungrouped. Here we set all
83  * threads up into their own group so threads will get their own
84  * cpu and hopefully be kept in place by Mach from there.
85  */
86 
87  thread_extended_policy_data_t epolicy;
88  thread_affinity_policy_data_t apolicy;
89  thread_t curr_thread = mach_thread_self();
90  kern_return_t ret;
91 
92  /* discourage interrupting this thread */
93  epolicy.timeshare = FALSE;
94  ret = thread_policy_set(curr_thread, THREAD_EXTENDED_POLICY, (thread_policy_t) &epolicy, THREAD_EXTENDED_POLICY_COUNT);
95  if (ret != KERN_SUCCESS)
96  return -1;
97 
98  /* put each thread into a separate group */
99  apolicy.affinity_tag = cpu % bu_avail_cpus();
100  ret = thread_policy_set(curr_thread, THREAD_EXTENDED_POLICY, (thread_policy_t) &apolicy, THREAD_EXTENDED_POLICY_COUNT);
101  if (ret != KERN_SUCCESS)
102  return -1;
103 
104  return 0;
105 
106 #elif defined(HAVE_WINDOWS_H)
107  DWORD_PTR cpumask = (DWORD_PTR)1 << cpu % bu_avail_cpus();
108  BOOL ret = SetThreadAffinityMask(GetCurrentThread(), cpumask);
109  if (ret == 0)
110  return -1;
111 
112  return 0;
113 
114 #else
115 
116  /* don't know how to set thread affinity on this platform */
117  cpu = 0; /* do something with cpu to avoid an unused parameter warning */
118  return cpu;
119 
120 #endif
121 }
122 
123 
124 /*
125  * Local Variables:
126  * mode: C
127  * tab-width: 8
128  * indent-tabs-mode: t
129  * c-file-style: "stroustrup"
130  * End:
131  * ex: shiftwidth=4 tabstop=8
132  */
#define FALSE
int parallel_set_affinity(int cpu)
Definition: affinity.c:56
Header file for the BRL-CAD common definitions.
size_t bu_avail_cpus(void)
Definition: parallel.c:193