Skip to content

Commit

Permalink
[OpenMP] Re-use affinity raii class in worker spawning
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jpeyton52 committed Jul 24, 2023
1 parent b9f3eaf commit 1e3bbf7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 53 deletions.
25 changes: 25 additions & 0 deletions openmp/runtime/src/kmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,31 @@ class KMPAffinity {
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
Expand Down
22 changes: 0 additions & 22 deletions openmp/runtime/src/kmp_affinity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1273,28 +1273,6 @@ bool kmp_topology_t::is_close(int hwt1, int hwt2, int hw_level) const {
////////////////////////////////////////////////////////////////////////////////

#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;

Expand Down
33 changes: 2 additions & 31 deletions openmp/runtime/src/kmp_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4763,25 +4763,6 @@ static void __kmp_initialize_team(kmp_team_t *team, int new_nproc,
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.
Expand Down Expand Up @@ -5347,12 +5328,6 @@ __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
#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",
Expand Down Expand Up @@ -5401,7 +5376,7 @@ __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
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 */
Expand Down Expand Up @@ -5432,11 +5407,7 @@ __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
}

#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
Expand Down

0 comments on commit 1e3bbf7

Please sign in to comment.