Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit cb6f3c4

Browse files
committed
[OpenMP] Enable thread affinity on FreeBSD
Reviewers: chandlerc, jlpeyton, jdoerfert, dim Reviewed-By: dim Differential Revision: https://reviews.llvm.org/D68580 git-svn-id: https://llvm.org/svn/llvm-project/openmp/trunk@374118 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent ace8505 commit cb6f3c4

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

runtime/src/kmp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3341,7 +3341,7 @@ extern int __kmp_aux_set_affinity_mask_proc(int proc, void **mask);
33413341
extern int __kmp_aux_unset_affinity_mask_proc(int proc, void **mask);
33423342
extern int __kmp_aux_get_affinity_mask_proc(int proc, void **mask);
33433343
extern void __kmp_balanced_affinity(kmp_info_t *th, int team_size);
3344-
#if KMP_OS_LINUX
3344+
#if KMP_OS_LINUX || KMP_OS_FREEBSD
33453345
extern int kmp_set_thread_affinity_mask_initial(void);
33463346
#endif
33473347
#endif /* KMP_AFFINITY_SUPPORTED */

runtime/src/kmp_affinity.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,7 +1968,7 @@ static void __kmp_dispatch_set_hierarchy_values() {
19681968
__kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1] =
19691969
nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
19701970
__kmp_hier_max_units[kmp_hier_layer_e::LAYER_L1 + 1] = __kmp_ncores;
1971-
#if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_WINDOWS)
1971+
#if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS)
19721972
if (__kmp_mic_type >= mic3)
19731973
__kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores / 2;
19741974
else
@@ -1982,7 +1982,7 @@ static void __kmp_dispatch_set_hierarchy_values() {
19821982
__kmp_hier_threads_per[kmp_hier_layer_e::LAYER_THREAD + 1] = 1;
19831983
__kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L1 + 1] =
19841984
__kmp_nThreadsPerCore;
1985-
#if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_WINDOWS)
1985+
#if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS)
19861986
if (__kmp_mic_type >= mic3)
19871987
__kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
19881988
2 * __kmp_nThreadsPerCore;

runtime/src/kmp_affinity.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ class KMPHwlocAffinity : public KMPAffinity {
160160
};
161161
#endif /* KMP_USE_HWLOC */
162162

163+
#if KMP_OS_LINUX || KMP_OS_FREEBSD
163164
#if KMP_OS_LINUX
164165
/* On some of the older OS's that we build on, these constants aren't present
165166
in <asm/unistd.h> #included from <sys.syscall.h>. They must be the same on
@@ -234,6 +235,10 @@ class KMPHwlocAffinity : public KMPAffinity {
234235
#endif /* __NR_sched_getaffinity */
235236
#error Unknown or unsupported architecture
236237
#endif /* KMP_ARCH_* */
238+
#elif KMP_OS_FREEBSD
239+
#include <pthread.h>
240+
#include <pthread_np.h>
241+
#endif
237242
class KMPNativeAffinity : public KMPAffinity {
238243
class Mask : public KMPAffinity::Mask {
239244
typedef unsigned char mask_t;
@@ -294,8 +299,13 @@ class KMPNativeAffinity : public KMPAffinity {
294299
int get_system_affinity(bool abort_on_error) override {
295300
KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
296301
"Illegal get affinity operation when not capable");
302+
#if KMP_OS_LINUX
297303
int retval =
298304
syscall(__NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask);
305+
#elif KMP_OS_FREEBSD
306+
int retval =
307+
pthread_getaffinity_np(pthread_self(), __kmp_affin_mask_size, reinterpret_cast<cpuset_t *>(mask));
308+
#endif
299309
if (retval >= 0) {
300310
return 0;
301311
}
@@ -308,8 +318,13 @@ class KMPNativeAffinity : public KMPAffinity {
308318
int set_system_affinity(bool abort_on_error) const override {
309319
KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
310320
"Illegal get affinity operation when not capable");
321+
#if KMP_OS_LINUX
311322
int retval =
312323
syscall(__NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask);
324+
#elif KMP_OS_FREEBSD
325+
int retval =
326+
pthread_setaffinity_np(pthread_self(), __kmp_affin_mask_size, reinterpret_cast<cpuset_t *>(mask));
327+
#endif
313328
if (retval >= 0) {
314329
return 0;
315330
}
@@ -347,7 +362,7 @@ class KMPNativeAffinity : public KMPAffinity {
347362
}
348363
api_type get_api_type() const override { return NATIVE_OS; }
349364
};
350-
#endif /* KMP_OS_LINUX */
365+
#endif /* KMP_OS_LINUX || KMP_OS_FREEBSD */
351366

352367
#if KMP_OS_WINDOWS
353368
class KMPNativeAffinity : public KMPAffinity {

runtime/src/kmp_os.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
#error Unknown compiler
7070
#endif
7171

72-
#if (KMP_OS_LINUX || KMP_OS_WINDOWS) && !KMP_OS_CNK
72+
#if (KMP_OS_LINUX || KMP_OS_WINDOWS || KMP_OS_FREEBSD) && !KMP_OS_CNK
7373
#define KMP_AFFINITY_SUPPORTED 1
7474
#if KMP_OS_WINDOWS && KMP_ARCH_X86_64
7575
#define KMP_GROUP_AFFINITY 1

runtime/src/kmp_runtime.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4493,7 +4493,7 @@ static void __kmp_initialize_team(kmp_team_t *team, int new_nproc,
44934493
KF_TRACE(10, ("__kmp_initialize_team: exit: team=%p\n", team));
44944494
}
44954495

4496-
#if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
4496+
#if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
44974497
/* Sets full mask for thread and returns old mask, no changes to structures. */
44984498
static void
44994499
__kmp_set_thread_affinity_mask_full_tmp(kmp_affin_mask_t *old_mask) {
@@ -5041,7 +5041,7 @@ __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
50415041
__kmp_partition_places(team);
50425042
#endif
50435043
} else { // team->t.t_nproc < new_nproc
5044-
#if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
5044+
#if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
50455045
kmp_affin_mask_t *old_mask;
50465046
if (KMP_AFFINITY_CAPABLE()) {
50475047
KMP_CPU_ALLOC(old_mask);
@@ -5090,7 +5090,7 @@ __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
50905090
__kmp_reinitialize_team(team, new_icvs, NULL);
50915091
}
50925092

5093-
#if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
5093+
#if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
50945094
/* Temporarily set full mask for master thread before creation of
50955095
workers. The reason is that workers inherit the affinity from master,
50965096
so if a lot of workers are created on the single core quickly, they
@@ -5125,7 +5125,7 @@ __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
51255125
}
51265126
}
51275127

5128-
#if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
5128+
#if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
51295129
if (KMP_AFFINITY_CAPABLE()) {
51305130
/* Restore initial master thread's affinity mask */
51315131
__kmp_set_system_affinity(old_mask, TRUE);

runtime/src/z_Linux_util.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static void __kmp_print_cond(char *buffer, kmp_cond_align_t *cond) {
100100
}
101101
#endif
102102

103-
#if (KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED)
103+
#if ((KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED)
104104

105105
/* Affinity support */
106106

@@ -122,16 +122,21 @@ void __kmp_affinity_bind_thread(int which) {
122122
void __kmp_affinity_determine_capable(const char *env_var) {
123123
// Check and see if the OS supports thread affinity.
124124

125+
#if KMP_OS_LINUX
125126
#define KMP_CPU_SET_SIZE_LIMIT (1024 * 1024)
127+
#elif KMP_OS_FREEBSD
128+
#define KMP_CPU_SET_SIZE_LIMIT (sizeof(cpuset_t))
129+
#endif
126130

127-
int gCode;
128-
int sCode;
129-
unsigned char *buf;
130-
buf = (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT);
131131

132+
#if KMP_OS_LINUX
132133
// If Linux* OS:
133134
// If the syscall fails or returns a suggestion for the size,
134135
// then we don't have to search for an appropriate size.
136+
int gCode;
137+
int sCode;
138+
unsigned char *buf;
139+
buf = (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT);
135140
gCode = syscall(__NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf);
136141
KA_TRACE(30, ("__kmp_affinity_determine_capable: "
137142
"initial getaffinity call returned %d errno = %d\n",
@@ -270,6 +275,23 @@ void __kmp_affinity_determine_capable(const char *env_var) {
270275
}
271276
}
272277
}
278+
#elif KMP_OS_FREEBSD
279+
int gCode;
280+
unsigned char *buf;
281+
buf = (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT);
282+
gCode = pthread_getaffinity_np(pthread_self(), KMP_CPU_SET_SIZE_LIMIT, reinterpret_cast<cpuset_t *>(buf));
283+
KA_TRACE(30, ("__kmp_affinity_determine_capable: "
284+
"initial getaffinity call returned %d errno = %d\n",
285+
gCode, errno));
286+
if (gCode == 0) {
287+
KMP_AFFINITY_ENABLE(KMP_CPU_SET_SIZE_LIMIT);
288+
KA_TRACE(10, ("__kmp_affinity_determine_capable: "
289+
"affinity supported (mask size %d)\n"<
290+
(int)__kmp_affin_mask_size));
291+
KMP_INTERNAL_FREE(buf);
292+
return;
293+
}
294+
#endif
273295
// save uncaught error code
274296
// int error = errno;
275297
KMP_INTERNAL_FREE(buf);

0 commit comments

Comments
 (0)