add startup numa sense

This commit is contained in:
Youmu Liu
2026-04-02 18:13:32 +08:00
parent ba30c02cb3
commit 2f74e5e8a9
10 changed files with 273 additions and 45 deletions
-10
View File
@@ -340,16 +340,6 @@ SN_API SIMDID SN_DECL sn_get_simd_level_local()
return simdid;
}
SN_API SIMDID SN_DECL sn_get_simd_level_global()
{
if (global_simdid.mask) {
return global_simdid;
}
global_simdid = sn_get_simd_level_local();
return global_simdid;
}
#else
SN_API SIMDID SN_DECL sn_get_simd_level_local()
+39
View File
@@ -0,0 +1,39 @@
#include "sn_target.h"
#include "StgSharpNative.h"
#if SN_IS_OS(SN_OS_LINUX)
#define _GNU_SOURCE
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
/* Pin the calling thread to a single logical processor.
* core_id >= 0 : bind to that logical processor (global index, across groups).
* core_id < 0 : clear affinity constraint within the current group. */
SN_API void SN_DECL sn_set_thread_affinity(int core_id)
{
long ncpus = sysconf(_SC_NPROCESSORS_ONLN);
cpu_set_t *set = CPU_ALLOC(ncpus);
size_t size = CPU_ALLOC_SIZE(ncpus);
CPU_ZERO_S(size, set);
if (core_id < 0) {
for (long i = 0; i < ncpus; i++)
CPU_SET_S((int)i, size, set);
} else {
CPU_SET_S(core_id, size, set);
}
pthread_setaffinity_np(pthread_self(), size, set);
CPU_FREE(set);
}
SN_API void SN_DECL sn_get_numa_overall(int *out_numa_count, int *count_of_each_numa)
{
}
SN_API void SN_DECL sn_get_numa_each(int numa_id, int *in_numa_cpuid, int *global_cpuid)
{
}
#endif
@@ -1,19 +1,13 @@
#include "sn_target.h"
#include "StgSharpNative.h"
#if SN_IS_OS(SN_OS_LINUX)
#define _GNU_SOURCE
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
#endif
#if SN_IS_OS(SN_OS_WINDOWS)
/* Pin the calling thread to a single logical processor.
* core_id >= 0 : bind to that logical processor (global index, across groups).
* core_id < 0 : clear affinity constraint within the current group. */
SN_API void SN_DECL sn_set_thread_affinity(int core_id)
{
#if SN_IS_OS(SN_OS_WINDOWS)
if (core_id < 0) {
/* Query which group the thread currently belongs to */
GROUP_AFFINITY current;
@@ -23,33 +17,53 @@ SN_API void SN_DECL sn_set_thread_affinity(int core_id)
GROUP_AFFINITY ga;
memset(&ga, 0, sizeof(ga));
ga.Group = current.Group;
ga.Mask = (count >= 64) ? ~(KAFFINITY)0
: ((KAFFINITY)1 << count) - 1;
ga.Mask = (count >= 64) ? ~(KAFFINITY)0 : ((KAFFINITY)1 << count) - 1;
SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL);
return;
}
WORD group = (WORD)(core_id / 64);
int index = core_id % 64;
int index = core_id % 64;
DWORD count = GetActiveProcessorCount(group);
if ((DWORD)index >= count)
return;
GROUP_AFFINITY ga;
memset(&ga, 0, sizeof(ga));
ga.Group = group;
ga.Mask = (KAFFINITY)1 << index;
ga.Mask = (KAFFINITY)1 << index;
SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL);
#elif SN_IS_OS(SN_OS_LINUX)
cpu_set_t set;
if (core_id < 0) {
CPU_ZERO(&set);
long ncpus = sysconf(_SC_NPROCESSORS_ONLN);
for (long i = 0; i < ncpus; i++)
CPU_SET((int)i, &set);
} else {
CPU_ZERO(&set);
CPU_SET(core_id, &set);
}
pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
#endif
}
SN_API int SN_DECL sn_get_numa_overall(int *out_numa_count, int *count_of_each_numa)
{
ULONG highest;
if (!GetNumaHighestNodeNumber(&highest))
return 0;
*out_numa_count = (int)highest + 1;
for (UCHAR n = 0; n <= (UCHAR)highest; n++) {
ULONGLONG mask = 0;
GetNumaNodeProcessorMask(n, &mask);
count_of_each_numa[n] = (int)__popcnt64(mask);
}
return 1;
}
SN_API int SN_DECL sn_get_numa_each(int numa_id, uint64_t *internal, uint64_t *global)
{
ULONGLONG mask = 0;
if (!GetNumaNodeProcessorMask((UCHAR)numa_id, &mask))
return 0;
int intra = 0;
while (mask) {
unsigned long idx;
_BitScanForward64(&idx, mask);
internal[intra] = intra;
global[intra] = (int)idx;
intra++;
mask &= mask - 1;
}
return 1;
}
#endif