diff --git a/openmp/runtime/src/kmp.h b/openmp/runtime/src/kmp.h --- a/openmp/runtime/src/kmp.h +++ b/openmp/runtime/src/kmp.h @@ -798,6 +798,31 @@ typedef KMPAffinity::Mask kmp_affin_mask_t; extern KMPAffinity *__kmp_affinity_dispatch; +class kmp_affinity_raii_t { + kmp_affin_mask_t *mask; + bool restored; + +public: + kmp_affinity_raii_t(const kmp_affin_mask_t *new_mask = nullptr) + : restored(false) { + if (KMP_AFFINITY_CAPABLE()) { + KMP_CPU_ALLOC(mask); + KMP_ASSERT(mask != NULL); + __kmp_get_system_affinity(mask, /*abort_on_error=*/true); + if (new_mask) + __kmp_set_system_affinity(new_mask, /*abort_on_error=*/true); + } + } + void restore() { + if (!restored && KMP_AFFINITY_CAPABLE()) { + __kmp_set_system_affinity(mask, /*abort_on_error=*/true); + KMP_CPU_FREE(mask); + } + restored = true; + } + ~kmp_affinity_raii_t() { restore(); } +}; + // Declare local char buffers with this size for printing debug and info // messages, using __kmp_affinity_print_mask(). #define KMP_AFFIN_MASK_PRINT_LEN 1024 diff --git a/openmp/runtime/src/kmp_affinity.cpp b/openmp/runtime/src/kmp_affinity.cpp --- a/openmp/runtime/src/kmp_affinity.cpp +++ b/openmp/runtime/src/kmp_affinity.cpp @@ -1273,28 +1273,6 @@ //////////////////////////////////////////////////////////////////////////////// #if KMP_AFFINITY_SUPPORTED -class kmp_affinity_raii_t { - kmp_affin_mask_t *mask; - bool restored; - -public: - kmp_affinity_raii_t() : restored(false) { - KMP_CPU_ALLOC(mask); - KMP_ASSERT(mask != NULL); - __kmp_get_system_affinity(mask, TRUE); - } - void restore() { - __kmp_set_system_affinity(mask, TRUE); - KMP_CPU_FREE(mask); - restored = true; - } - ~kmp_affinity_raii_t() { - if (!restored) { - __kmp_set_system_affinity(mask, TRUE); - KMP_CPU_FREE(mask); - } - } -}; bool KMPAffinity::picked_api = false; diff --git a/openmp/runtime/src/kmp_runtime.cpp b/openmp/runtime/src/kmp_runtime.cpp --- a/openmp/runtime/src/kmp_runtime.cpp +++ b/openmp/runtime/src/kmp_runtime.cpp @@ -4763,25 +4763,6 @@ KF_TRACE(10, ("__kmp_initialize_team: exit: team=%p\n", team)); } -#if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED -/* Sets full mask for thread and returns old mask, no changes to structures. */ -static void -__kmp_set_thread_affinity_mask_full_tmp(kmp_affin_mask_t *old_mask) { - if (KMP_AFFINITY_CAPABLE()) { - int status; - if (old_mask != NULL) { - status = __kmp_get_system_affinity(old_mask, TRUE); - int error = errno; - if (status != 0) { - __kmp_fatal(KMP_MSG(ChangeThreadAffMaskError), KMP_ERR(error), - __kmp_msg_null); - } - } - __kmp_set_system_affinity(__kmp_affin_fullMask, TRUE); - } -} -#endif - #if KMP_AFFINITY_SUPPORTED // __kmp_partition_places() is the heart of the OpenMP 4.0 affinity mechanism. @@ -5347,12 +5328,6 @@ #endif } } else { // team->t.t_nproc < new_nproc -#if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED - kmp_affin_mask_t *old_mask; - if (KMP_AFFINITY_CAPABLE()) { - KMP_CPU_ALLOC(old_mask); - } -#endif KA_TRACE(20, ("__kmp_allocate_team: increasing hot team thread count to %d\n", @@ -5401,7 +5376,7 @@ primary thread, so if a lot of workers are created on the single core quickly, they don't get a chance to set their own affinity for a long time. */ - __kmp_set_thread_affinity_mask_full_tmp(old_mask); + kmp_affinity_raii_t new_temp_affinity{__kmp_affin_fullMask}; #endif /* allocate new threads for the hot team */ @@ -5432,11 +5407,7 @@ } #if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED - if (KMP_AFFINITY_CAPABLE()) { - /* Restore initial primary thread's affinity mask */ - __kmp_set_system_affinity(old_mask, TRUE); - KMP_CPU_FREE(old_mask); - } + new_temp_affinity.restore(); #endif #if KMP_NESTED_HOT_TEAMS } // end of check of t_nproc vs. new_nproc vs. hot_team_nth