168 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
171 #include <sys/param.h>
172 #include <sys/cpuset.h>
173 #include <pthread_np.h>
174 typedef cpuset_t cpu_set_t;
185 ABTD_affinity_cpuset initial_cpuset;
187 ABTD_affinity_cpuset *cpusets;
192 static inline int int_rem(
int a,
unsigned int b)
198 int ret = (a % int_b) + int_b;
199 return ret >= int_b ? (ret - int_b) : ret;
204 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
205 int i, num_cores = 0;
208 int ret = pthread_getaffinity_np(native_thread,
sizeof(cpu_set_t), &cpuset);
211 for (i = 0; i < CPU_SETSIZE; i++) {
212 if (CPU_ISSET(i, &cpuset)) {
216 *p_num_cores = num_cores;
224 ABTD_affinity_cpuset *p_cpuset)
226 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
228 int ret = pthread_getaffinity_np(native_thread,
sizeof(cpu_set_t), &cpuset);
231 int i, j, num_cpuids = 0;
232 for (i = 0; i < CPU_SETSIZE; i++) {
233 if (CPU_ISSET(i, &cpuset))
236 p_cpuset->num_cpuids = num_cpuids;
237 ret =
ABTU_malloc(
sizeof(
int) * num_cpuids, (
void **)&p_cpuset->cpuids);
238 ABTI_CHECK_ERROR(ret);
239 for (i = 0, j = 0; i < CPU_SETSIZE; i++) {
240 if (CPU_ISSET(i, &cpuset))
241 p_cpuset->cpuids[j++] = i;
250 int *cpuids,
int *p_num_cpuids)
252 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
254 int ret = pthread_getaffinity_np(native_thread,
sizeof(cpu_set_t), &cpuset);
257 int i, num_cpuids = 0;
258 for (i = 0; i < CPU_SETSIZE; i++) {
259 if (CPU_ISSET(i, &cpuset)) {
260 if (num_cpuids < max_cpuids) {
261 cpuids[num_cpuids] = i;
266 *p_num_cpuids = num_cpuids;
274 const ABTD_affinity_cpuset *p_cpuset)
276 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
280 if (p_cpuset->num_cpuids == 0) {
286 for (i = 0; i < p_cpuset->num_cpuids; i++)
287 CPU_SET(
int_rem(p_cpuset->cpuids[i], CPU_SETSIZE), &cpuset);
289 int ret = pthread_setaffinity_np(native_thread,
sizeof(cpu_set_t), &cpuset);
296 void ABTD_affinity_init(ABTI_global *p_global,
const char *affinity_str)
301 pthread_t self_native_thread = pthread_self();
302 ABTD_affinity_list *p_list = NULL;
306 ret =
get_num_cores(self_native_thread, &p_global->num_cores);
307 if (ret !=
ABT_SUCCESS || p_global->num_cores == 0)
313 ret = ABTD_affinity_list_create(affinity_str, &p_list);
315 if (p_list->num == 0) {
316 ABTD_affinity_list_free(p_list);
322 ret =
ABTU_calloc(p_list->num,
sizeof(ABTD_affinity_cpuset),
327 for (i = 0; i < p_list->num; i++) {
328 const ABTD_affinity_id_list *p_id_list = p_list->p_id_lists[i];
329 uint32_t j, num_cpuids = 0, len_cpuids = 8;
334 for (j = 0; j < p_id_list->num; j++) {
335 int cpuid_i =
int_rem(p_id_list->ids[j],
341 for (k = 0; k < num_cpuids; k++) {
348 if (num_cpuids == len_cpuids) {
350 sizeof(
int) * len_cpuids * 2,
362 if (num_cpuids != len_cpuids) {
364 sizeof(
int) * num_cpuids,
371 ABTD_affinity_list_free(p_list);
375 sizeof(ABTD_affinity_cpuset),
393 ABTD_affinity_list_free(p_list);
404 void ABTD_affinity_finalize(ABTI_global *p_global)
406 pthread_t self_native_thread = pthread_self();
407 if (p_global->set_affinity) {
428 ABTU_ret_err int ABTD_affinity_cpuset_read(ABTD_xstream_context *p_ctx,
429 int max_cpuids,
int *cpuids,
432 return read_cpuset(p_ctx->native_thread, max_cpuids, cpuids, p_num_cpuids);
436 ABTD_affinity_cpuset_apply(ABTD_xstream_context *p_ctx,
437 const ABTD_affinity_cpuset *p_cpuset)
442 ABTU_ret_err int ABTD_affinity_cpuset_apply_default(ABTD_xstream_context *p_ctx,
445 ABTD_affinity_cpuset *p_cpuset =
450 void ABTD_affinity_cpuset_destroy(ABTD_affinity_cpuset *p_cpuset)
454 p_cpuset->cpuids = NULL;