Skip to content

Commit 1e3bbf7

Browse files
committed
[OpenMP] Re-use affinity raii class in worker spawning
Get rid of explicit mask alloc, getthreadaffinity, set temp affinity, reset to old affinity, dealloc steps in favor of existing kmp_affinity_raii_t to push/pop a temporary affinity. Differential Revision: https://reviews.llvm.org/D154650
1 parent b9f3eaf commit 1e3bbf7

File tree

3 files changed

+27
-53
lines changed

3 files changed

+27
-53
lines changed

openmp/runtime/src/kmp.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,31 @@ class KMPAffinity {
798798
typedef KMPAffinity::Mask kmp_affin_mask_t;
799799
extern KMPAffinity *__kmp_affinity_dispatch;
800800

801+
class kmp_affinity_raii_t {
802+
kmp_affin_mask_t *mask;
803+
bool restored;
804+
805+
public:
806+
kmp_affinity_raii_t(const kmp_affin_mask_t *new_mask = nullptr)
807+
: restored(false) {
808+
if (KMP_AFFINITY_CAPABLE()) {
809+
KMP_CPU_ALLOC(mask);
810+
KMP_ASSERT(mask != NULL);
811+
__kmp_get_system_affinity(mask, /*abort_on_error=*/true);
812+
if (new_mask)
813+
__kmp_set_system_affinity(new_mask, /*abort_on_error=*/true);
814+
}
815+
}
816+
void restore() {
817+
if (!restored && KMP_AFFINITY_CAPABLE()) {
818+
__kmp_set_system_affinity(mask, /*abort_on_error=*/true);
819+
KMP_CPU_FREE(mask);
820+
}
821+
restored = true;
822+
}
823+
~kmp_affinity_raii_t() { restore(); }
824+
};
825+
801826
// Declare local char buffers with this size for printing debug and info
802827
// messages, using __kmp_affinity_print_mask().
803828
#define KMP_AFFIN_MASK_PRINT_LEN 1024

openmp/runtime/src/kmp_affinity.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,28 +1273,6 @@ bool kmp_topology_t::is_close(int hwt1, int hwt2, int hw_level) const {
12731273
////////////////////////////////////////////////////////////////////////////////
12741274

12751275
#if KMP_AFFINITY_SUPPORTED
1276-
class kmp_affinity_raii_t {
1277-
kmp_affin_mask_t *mask;
1278-
bool restored;
1279-
1280-
public:
1281-
kmp_affinity_raii_t() : restored(false) {
1282-
KMP_CPU_ALLOC(mask);
1283-
KMP_ASSERT(mask != NULL);
1284-
__kmp_get_system_affinity(mask, TRUE);
1285-
}
1286-
void restore() {
1287-
__kmp_set_system_affinity(mask, TRUE);
1288-
KMP_CPU_FREE(mask);
1289-
restored = true;
1290-
}
1291-
~kmp_affinity_raii_t() {
1292-
if (!restored) {
1293-
__kmp_set_system_affinity(mask, TRUE);
1294-
KMP_CPU_FREE(mask);
1295-
}
1296-
}
1297-
};
12981276

12991277
bool KMPAffinity::picked_api = false;
13001278

openmp/runtime/src/kmp_runtime.cpp

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4763,25 +4763,6 @@ static void __kmp_initialize_team(kmp_team_t *team, int new_nproc,
47634763
KF_TRACE(10, ("__kmp_initialize_team: exit: team=%p\n", team));
47644764
}
47654765

4766-
#if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
4767-
/* Sets full mask for thread and returns old mask, no changes to structures. */
4768-
static void
4769-
__kmp_set_thread_affinity_mask_full_tmp(kmp_affin_mask_t *old_mask) {
4770-
if (KMP_AFFINITY_CAPABLE()) {
4771-
int status;
4772-
if (old_mask != NULL) {
4773-
status = __kmp_get_system_affinity(old_mask, TRUE);
4774-
int error = errno;
4775-
if (status != 0) {
4776-
__kmp_fatal(KMP_MSG(ChangeThreadAffMaskError), KMP_ERR(error),
4777-
__kmp_msg_null);
4778-
}
4779-
}
4780-
__kmp_set_system_affinity(__kmp_affin_fullMask, TRUE);
4781-
}
4782-
}
4783-
#endif
4784-
47854766
#if KMP_AFFINITY_SUPPORTED
47864767

47874768
// __kmp_partition_places() is the heart of the OpenMP 4.0 affinity mechanism.
@@ -5347,12 +5328,6 @@ __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
53475328
#endif
53485329
}
53495330
} else { // team->t.t_nproc < new_nproc
5350-
#if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
5351-
kmp_affin_mask_t *old_mask;
5352-
if (KMP_AFFINITY_CAPABLE()) {
5353-
KMP_CPU_ALLOC(old_mask);
5354-
}
5355-
#endif
53565331

53575332
KA_TRACE(20,
53585333
("__kmp_allocate_team: increasing hot team thread count to %d\n",
@@ -5401,7 +5376,7 @@ __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
54015376
primary thread, so if a lot of workers are created on the single
54025377
core quickly, they don't get a chance to set their own affinity for
54035378
a long time. */
5404-
__kmp_set_thread_affinity_mask_full_tmp(old_mask);
5379+
kmp_affinity_raii_t new_temp_affinity{__kmp_affin_fullMask};
54055380
#endif
54065381

54075382
/* allocate new threads for the hot team */
@@ -5432,11 +5407,7 @@ __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
54325407
}
54335408

54345409
#if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
5435-
if (KMP_AFFINITY_CAPABLE()) {
5436-
/* Restore initial primary thread's affinity mask */
5437-
__kmp_set_system_affinity(old_mask, TRUE);
5438-
KMP_CPU_FREE(old_mask);
5439-
}
5410+
new_temp_affinity.restore();
54405411
#endif
54415412
#if KMP_NESTED_HOT_TEAMS
54425413
} // end of check of t_nproc vs. new_nproc vs. hot_team_nth

0 commit comments

Comments
 (0)