Skip to content

Commit 11cd2a3

Browse files
authored
[openmp] porting affinity feature to netbsd. (llvm#84618)
netbsd supports the portable hwloc's layer as well. for a hardware with 4 cpus, a cpu set is 4 and maxcpus is 256.
1 parent 5630dc6 commit 11cd2a3

File tree

6 files changed

+26
-14
lines changed

6 files changed

+26
-14
lines changed

openmp/runtime/src/kmp.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3912,7 +3912,7 @@ extern void __kmp_balanced_affinity(kmp_info_t *th, int team_size);
39123912
#if KMP_WEIGHTED_ITERATIONS_SUPPORTED
39133913
extern int __kmp_get_first_osid_with_ecore(void);
39143914
#endif
3915-
#if KMP_OS_LINUX || KMP_OS_FREEBSD
3915+
#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
39163916
extern int kmp_set_thread_affinity_mask_initial(void);
39173917
#endif
39183918
static inline void __kmp_assign_root_init_mask() {

openmp/runtime/src/kmp_affinity.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -2828,7 +2828,8 @@ static void __kmp_dispatch_set_hierarchy_values() {
28282828
__kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1] =
28292829
nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
28302830
__kmp_hier_max_units[kmp_hier_layer_e::LAYER_L1 + 1] = __kmp_ncores;
2831-
#if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS) && \
2831+
#if KMP_ARCH_X86_64 && \
2832+
(KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_WINDOWS) && \
28322833
KMP_MIC_SUPPORTED
28332834
if (__kmp_mic_type >= mic3)
28342835
__kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores / 2;
@@ -2843,7 +2844,8 @@ static void __kmp_dispatch_set_hierarchy_values() {
28432844
__kmp_hier_threads_per[kmp_hier_layer_e::LAYER_THREAD + 1] = 1;
28442845
__kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L1 + 1] =
28452846
__kmp_nThreadsPerCore;
2846-
#if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS) && \
2847+
#if KMP_ARCH_X86_64 && \
2848+
(KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_WINDOWS) && \
28472849
KMP_MIC_SUPPORTED
28482850
if (__kmp_mic_type >= mic3)
28492851
__kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
@@ -5557,7 +5559,7 @@ void __kmp_balanced_affinity(kmp_info_t *th, int nthreads) {
55575559
}
55585560
}
55595561

5560-
#if KMP_OS_LINUX || KMP_OS_FREEBSD
5562+
#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
55615563
// We don't need this entry for Windows because
55625564
// there is GetProcessAffinityMask() api
55635565
//

openmp/runtime/src/kmp_affinity.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class KMPHwlocAffinity : public KMPAffinity {
191191
};
192192
#endif /* KMP_USE_HWLOC */
193193

194-
#if KMP_OS_LINUX || KMP_OS_FREEBSD
194+
#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
195195
#if KMP_OS_LINUX
196196
/* On some of the older OS's that we build on, these constants aren't present
197197
in <asm/unistd.h> #included from <sys.syscall.h>. They must be the same on
@@ -314,6 +314,9 @@ class KMPHwlocAffinity : public KMPAffinity {
314314
#elif KMP_OS_FREEBSD
315315
#include <pthread.h>
316316
#include <pthread_np.h>
317+
#elif KMP_OS_NETBSD
318+
#include <pthread.h>
319+
#include <sched.h>
317320
#endif
318321
class KMPNativeAffinity : public KMPAffinity {
319322
class Mask : public KMPAffinity::Mask {
@@ -407,7 +410,7 @@ class KMPNativeAffinity : public KMPAffinity {
407410
#if KMP_OS_LINUX
408411
long retval =
409412
syscall(__NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask);
410-
#elif KMP_OS_FREEBSD
413+
#elif KMP_OS_FREEBSD || KMP_OS_NETBSD
411414
int r = pthread_getaffinity_np(pthread_self(), __kmp_affin_mask_size,
412415
reinterpret_cast<cpuset_t *>(mask));
413416
int retval = (r == 0 ? 0 : -1);
@@ -428,7 +431,7 @@ class KMPNativeAffinity : public KMPAffinity {
428431
#if KMP_OS_LINUX
429432
long retval =
430433
syscall(__NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask);
431-
#elif KMP_OS_FREEBSD
434+
#elif KMP_OS_FREEBSD || KMP_OS_NETBSD
432435
int r = pthread_setaffinity_np(pthread_self(), __kmp_affin_mask_size,
433436
reinterpret_cast<cpuset_t *>(mask));
434437
int retval = (r == 0 ? 0 : -1);
@@ -471,7 +474,7 @@ class KMPNativeAffinity : public KMPAffinity {
471474
}
472475
api_type get_api_type() const override { return NATIVE_OS; }
473476
};
474-
#endif /* KMP_OS_LINUX || KMP_OS_FREEBSD */
477+
#endif /* KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD */
475478

476479
#if KMP_OS_WINDOWS
477480
class KMPNativeAffinity : public KMPAffinity {

openmp/runtime/src/kmp_os.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
#error Unknown compiler
7676
#endif
7777

78-
#if (KMP_OS_LINUX || KMP_OS_WINDOWS || KMP_OS_FREEBSD) && !KMP_OS_WASI
78+
#if (KMP_OS_LINUX || KMP_OS_WINDOWS || KMP_OS_FREEBSD || KMP_OS_NETBSD) && \
79+
!KMP_OS_WASI
7980
#define KMP_AFFINITY_SUPPORTED 1
8081
#if KMP_OS_WINDOWS && KMP_ARCH_X86_64
8182
#define KMP_GROUP_AFFINITY 1

openmp/runtime/src/kmp_runtime.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5376,7 +5376,7 @@ __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
53765376
__kmp_reinitialize_team(team, new_icvs, NULL);
53775377
}
53785378

5379-
#if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
5379+
#if (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD) && KMP_AFFINITY_SUPPORTED
53805380
/* Temporarily set full mask for primary thread before creation of
53815381
workers. The reason is that workers inherit the affinity from the
53825382
primary thread, so if a lot of workers are created on the single
@@ -5412,7 +5412,7 @@ __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
54125412
}
54135413
}
54145414

5415-
#if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
5415+
#if (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD) && KMP_AFFINITY_SUPPORTED
54165416
/* Restore initial primary thread's affinity mask */
54175417
new_temp_affinity.restore();
54185418
#endif

openmp/runtime/src/z_Linux_util.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
#elif KMP_OS_NETBSD || KMP_OS_OPENBSD
6666
#include <sys/types.h>
6767
#include <sys/sysctl.h>
68+
#if KMP_OS_NETBSD
69+
#include <sched.h>
70+
#endif
6871
#elif KMP_OS_SOLARIS
6972
#include <libproc.h>
7073
#include <procfs.h>
@@ -122,7 +125,8 @@ static void __kmp_print_cond(char *buffer, kmp_cond_align_t *cond) {
122125
}
123126
#endif
124127

125-
#if ((KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED)
128+
#if ((KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD) && \
129+
KMP_AFFINITY_SUPPORTED)
126130

127131
/* Affinity support */
128132

@@ -149,6 +153,8 @@ void __kmp_affinity_determine_capable(const char *env_var) {
149153
#define KMP_CPU_SET_TRY_SIZE CACHE_LINE
150154
#elif KMP_OS_FREEBSD
151155
#define KMP_CPU_SET_SIZE_LIMIT (sizeof(cpuset_t))
156+
#elif KMP_OS_NETBSD
157+
#define KMP_CPU_SET_SIZE_LIMIT (256)
152158
#endif
153159

154160
int verbose = __kmp_affinity.flags.verbose;
@@ -236,7 +242,7 @@ void __kmp_affinity_determine_capable(const char *env_var) {
236242
KMP_INTERNAL_FREE(buf);
237243
return;
238244
}
239-
#elif KMP_OS_FREEBSD
245+
#elif KMP_OS_FREEBSD || KMP_OS_NETBSD
240246
long gCode;
241247
unsigned char *buf;
242248
buf = (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT);
@@ -1262,7 +1268,7 @@ static void __kmp_atfork_child(void) {
12621268
++__kmp_fork_count;
12631269

12641270
#if KMP_AFFINITY_SUPPORTED
1265-
#if KMP_OS_LINUX || KMP_OS_FREEBSD
1271+
#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
12661272
// reset the affinity in the child to the initial thread
12671273
// affinity in the parent
12681274
kmp_set_thread_affinity_mask_initial();

0 commit comments

Comments
 (0)