9 #ifdef HAVE_PTHREAD_SETAFFINITY_NP 10 #if defined(__FreeBSD__) 11 #include <sys/param.h> 12 #include <sys/cpuset.h> 13 #include <pthread_np.h> 15 typedef cpuset_t cpu_set_t;
17 static inline int ABTD_CPU_COUNT(cpu_set_t *p_cpuset)
20 for (i = 0; i < CPU_SETSIZE; i++) {
21 if (CPU_ISSET(i, p_cpuset)) {
31 #define ABTD_CPU_COUNT CPU_COUNT 35 ABTI_ES_AFFINITY_CHAMELEON,
37 ABTI_ES_AFFINITY_DEFAULT
39 static int g_affinity_type = ABTI_ES_AFFINITY_DEFAULT;
40 static cpu_set_t g_cpusets[CPU_SETSIZE];
41 static cpu_set_t g_initial_cpuset;
43 static inline cpu_set_t ABTD_affinity_get_cpuset_for_rank(
int rank)
47 if (g_affinity_type == ABTI_ES_AFFINITY_CHAMELEON) {
48 int num_threads_per_socket = num_cores / 2;
50 int socket_id = rank / num_threads_per_socket;
52 (rank - num_threads_per_socket * socket_id - rem + socket_id) +
53 num_threads_per_socket * rem;
54 return g_cpusets[target % num_cores];
56 }
else if (g_affinity_type == ABTI_ES_AFFINITY_KNC) {
64 const int NUM_HTHREAD = 4;
65 int NUM_PHYSICAL_CORES = num_cores / NUM_HTHREAD;
67 if (rank < NUM_PHYSICAL_CORES) {
68 target = NUM_HTHREAD * rank;
69 }
else if (rank < NUM_PHYSICAL_CORES * 2) {
70 target = NUM_HTHREAD * (rank - NUM_PHYSICAL_CORES) + 1;
71 }
else if (rank < NUM_PHYSICAL_CORES * 3) {
72 target = NUM_HTHREAD * (rank - NUM_PHYSICAL_CORES * 2) + 2;
74 target = NUM_HTHREAD * (rank - NUM_PHYSICAL_CORES * 3) + 3;
76 return g_cpusets[target % num_cores];
79 return g_cpusets[rank % num_cores];
84 void ABTD_affinity_init(
void)
86 #ifdef HAVE_PTHREAD_SETAFFINITY_NP 90 #if defined(__FreeBSD__) 91 for (i = 0; i < CPU_SETSIZE; i++) {
92 CPU_ZERO(&g_cpusets[i]);
93 CPU_SET(i, &g_cpusets[i]);
95 num_cores = CPU_SETSIZE;
97 i = sched_getaffinity(getpid(),
sizeof(cpu_set_t), &g_initial_cpuset);
100 for (i = 0; i < CPU_SETSIZE; i++) {
101 CPU_ZERO(&g_cpusets[i]);
102 if (CPU_ISSET(i, &g_initial_cpuset)) {
103 CPU_SET(i, &g_cpusets[num_cores]);
111 char *env = getenv(
"ABT_AFFINITY_TYPE");
113 env = getenv(
"ABT_ENV_AFFINITY_TYPE");
115 if (strcmp(env,
"chameleon") == 0) {
116 g_affinity_type = ABTI_ES_AFFINITY_CHAMELEON;
117 }
else if (strcmp(env,
"knc") == 0) {
118 g_affinity_type = ABTI_ES_AFFINITY_KNC;
127 void ABTD_affinity_finalize(
void)
129 #ifdef HAVE_PTHREAD_SETAFFINITY_NP 130 pthread_t ctx = pthread_self();
131 pthread_setaffinity_np(ctx,
sizeof(cpu_set_t), &g_initial_cpuset);
135 int ABTD_affinity_set(ABTD_xstream_context *p_ctx,
int rank)
137 #ifdef HAVE_PTHREAD_SETAFFINITY_NP 140 cpu_set_t cpuset = ABTD_affinity_get_cpuset_for_rank(rank);
141 if (!pthread_setaffinity_np(p_ctx->native_thread,
sizeof(cpu_set_t),
160 int ABTD_affinity_set_cpuset(ABTD_xstream_context *p_ctx,
int cpuset_size,
163 #ifdef HAVE_PTHREAD_SETAFFINITY_NP 169 for (i = 0; i < cpuset_size; i++) {
170 ABTI_ASSERT(p_cpuset[i] < CPU_SETSIZE);
171 CPU_SET(p_cpuset[i], &cpuset);
174 i = pthread_setaffinity_np(p_ctx->native_thread,
sizeof(cpu_set_t),
189 int ABTD_affinity_get_cpuset(ABTD_xstream_context *p_ctx,
int cpuset_size,
190 int *p_cpuset,
int *p_num_cpus)
192 #ifdef HAVE_PTHREAD_SETAFFINITY_NP 198 i = pthread_getaffinity_np(p_ctx->native_thread,
sizeof(cpu_set_t),
202 if (p_cpuset != NULL) {
203 for (i = 0; i < CPU_SETSIZE; i++) {
204 if (CPU_ISSET(i, &cpuset)) {
205 if (num_cpus < cpuset_size) {
206 p_cpuset[num_cpus] = i;
215 if (p_num_cpus != NULL) {
216 *p_num_cpus = ABTD_CPU_COUNT(&cpuset);
#define HANDLE_ERROR_FUNC_WITH_CODE(n)
ABTI_global * gp_ABTI_global
#define ABT_ERR_FEATURE_NA