From 7c465a5f41f5e862bb6438a93c34fa089e5dd109 Mon Sep 17 00:00:00 2001 From: Jonathan Peyton Date: Mon, 12 Sep 2016 19:02:53 +0000 Subject: [PATCH] Fix bitmask upper bounds check Rather than checking KMP_CPU_SETSIZE, which doesn't exist when using Hwloc, we use the get_max_proc() function which can vary based on the operating system. For example on Windows with multiple processor groups, it might be the case that the highest bit possible in the bitmask is not equal to the number of hardware threads on the machine but something higher than that. Differential Revision: https://reviews.llvm.org/D24206 llvm-svn: 281245 --- openmp/runtime/src/kmp.h | 1 + openmp/runtime/src/kmp_affinity.cpp | 31 ++++++++++++++++--------------- openmp/runtime/src/kmp_ftn_entry.h | 11 +---------- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/openmp/runtime/src/kmp.h b/openmp/runtime/src/kmp.h index 88e288b..60ae316 100644 --- a/openmp/runtime/src/kmp.h +++ b/openmp/runtime/src/kmp.h @@ -3170,6 +3170,7 @@ extern void __kmp_affinity_set_place(int gtid); extern void __kmp_affinity_determine_capable( const char *env_var ); extern int __kmp_aux_set_affinity(void **mask); extern int __kmp_aux_get_affinity(void **mask); +extern int __kmp_aux_get_affinity_max_proc(); extern int __kmp_aux_set_affinity_mask_proc(int proc, void **mask); extern int __kmp_aux_unset_affinity_mask_proc(int proc, void **mask); extern int __kmp_aux_get_affinity_mask_proc(int proc, void **mask); diff --git a/openmp/runtime/src/kmp_affinity.cpp b/openmp/runtime/src/kmp_affinity.cpp index 251ee0c..93299b5 100644 --- a/openmp/runtime/src/kmp_affinity.cpp +++ b/openmp/runtime/src/kmp_affinity.cpp @@ -4534,6 +4534,19 @@ __kmp_aux_get_affinity(void **mask) } int +__kmp_aux_get_affinity_max_proc() { + if (! KMP_AFFINITY_CAPABLE()) { + return 0; + } +#if KMP_GROUP_AFFINITY + if ( __kmp_num_proc_groups > 1 ) { + return (int)(__kmp_num_proc_groups*sizeof(DWORD_PTR)*CHAR_BIT); + } +#endif + return __kmp_xproc; +} + +int __kmp_aux_set_affinity_mask_proc(int proc, void **mask) { int retval; @@ -4557,11 +4570,7 @@ __kmp_aux_set_affinity_mask_proc(int proc, void **mask) } } - if ((proc < 0) -# if !KMP_USE_HWLOC - || ((unsigned)proc >= KMP_CPU_SETSIZE) -# endif - ) { + if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) { return -1; } if (! KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) { @@ -4597,11 +4606,7 @@ __kmp_aux_unset_affinity_mask_proc(int proc, void **mask) } } - if ((proc < 0) -# if !KMP_USE_HWLOC - || ((unsigned)proc >= KMP_CPU_SETSIZE) -# endif - ) { + if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) { return -1; } if (! KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) { @@ -4637,11 +4642,7 @@ __kmp_aux_get_affinity_mask_proc(int proc, void **mask) } } - if ((proc < 0) -# if !KMP_USE_HWLOC - || ((unsigned)proc >= KMP_CPU_SETSIZE) -# endif - ) { + if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) { return -1; } if (! KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) { diff --git a/openmp/runtime/src/kmp_ftn_entry.h b/openmp/runtime/src/kmp_ftn_entry.h index 7ad15e3..8f58b52 100644 --- a/openmp/runtime/src/kmp_ftn_entry.h +++ b/openmp/runtime/src/kmp_ftn_entry.h @@ -266,16 +266,7 @@ FTN_GET_AFFINITY_MAX_PROC( void ) if ( ! TCR_4(__kmp_init_middle) ) { __kmp_middle_initialize(); } - if ( ! ( KMP_AFFINITY_CAPABLE() ) ) { - return 0; - } - - #if KMP_GROUP_AFFINITY - if ( __kmp_num_proc_groups > 1 ) { - return (int)(__kmp_num_proc_groups*sizeof(DWORD_PTR)*CHAR_BIT); - } - #endif /* KMP_GROUP_AFFINITY */ - return __kmp_xproc; + return __kmp_aux_get_affinity_max_proc(); #endif } -- 2.7.4