find a way to create Thread binded to LogicalCore properly

This commit is contained in:
Youmu Liu
2026-04-14 18:08:22 +08:00
parent b07013a003
commit fdf11c729f
15 changed files with 228 additions and 254 deletions
+1 -1
View File
@@ -148,7 +148,7 @@ SN_MK_PROC_DECL_STD(float, 512, , fma)
register __m512 c_3 = c_0;
for (size_t z = 0; z < common_z; z+=4) {
SN_MAT_KERNEL_LOCAL const *right = right + z;
SN_MAT_KERNEL_LOCAL const *right_ = right + z;
SN_MAT_KERNEL_LOCAL const *left_0 = left + z + 0;
SN_MAT_KERNEL_LOCAL const *left_1 = left + z + 1;
SN_MAT_KERNEL_LOCAL const *left_2 = left + z + 2;
+4 -20
View File
@@ -1,10 +1,11 @@
#include "sn_target.h"
#include "sn_intrinsic.h"
#include "StgSharpNative.h"
static SIMDID global_simdid = { 0 };
/* clang-format off */
#if SN_IS_ARCH(SN_ARCH_X86_64)
#include "sn_intrinsic.h"
#include "StgSharpNative.h"
static SIMDID global_simdid = { 0 };
#include "sn_intrinsic.context.x86.h"
@@ -350,23 +351,6 @@ SN_API SIMDID SN_DECL sn_get_simd_level_global()
return global_simdid;
}
#else
SN_API SIMDID SN_DECL sn_get_simd_level_local()
{
SIMDID simdid;
simdid.mask = 0;
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;
}
#endif
+9 -12
View File
@@ -137,7 +137,7 @@ static int sn_linux_count_cpu_list(char const *path)
if (!sn_linux_read_first_line(path, buffer, sizeof(buffer)))
return 0;
sn_linux_counter_context counter = {0};
sn_linux_counter_context counter = { 0 };
if (!sn_linux_visit_id_list(buffer, sn_linux_count_id, &counter))
return 0;
@@ -173,8 +173,7 @@ static size_t sn_linux_round_up_to_page_size(size_t size)
return rounded - (rounded % effective_page_size);
}
static int sn_linux_bind_memory_to_node(void *address, size_t size, int numa_id,
uint32_t flags)
static int sn_linux_bind_memory_to_node(void *address, size_t size, int numa_id, uint32_t flags)
{
#if defined(SYS_mbind)
size_t bits_per_word = sizeof(unsigned long) * 8u;
@@ -185,9 +184,8 @@ static int sn_linux_bind_memory_to_node(void *address, size_t size, int numa_id,
node_mask[(size_t)numa_id / bits_per_word] = 1ul << ((size_t)numa_id % bits_per_word);
int syscall_result = (int)syscall(SYS_mbind, address, size,
MPOL_BIND | MPOL_F_STATIC_NODES, node_mask,
(unsigned long)numa_id + 1ul, flags);
int syscall_result = (int)syscall(SYS_mbind, address, size, MPOL_BIND | MPOL_F_STATIC_NODES,
node_mask, (unsigned long)numa_id + 1ul, flags);
free(node_mask);
return syscall_result == 0;
#else
@@ -216,7 +214,7 @@ static int sn_linux_populate_writable_mapping(void *address, size_t size)
/* 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)
SN_API void SN_DECL sn_set_thread_affinity(int core_id, int numa_id)
{
int cpu_count = (int)sysconf(_SC_NPROCESSORS_ONLN);
if (cpu_count <= 0)
@@ -246,7 +244,7 @@ SN_API int SN_DECL sn_get_numa_overall(int *out_numa_count, int *count_of_each_n
if (!sn_linux_read_first_line("/sys/devices/system/node/online", buffer, sizeof(buffer)))
return 0;
sn_linux_max_context max_context = {-1};
sn_linux_max_context max_context = { -1 };
if (!sn_linux_visit_id_list(buffer, sn_linux_track_max_id, &max_context) ||
max_context.max_value < 0)
return 0;
@@ -258,7 +256,7 @@ SN_API int SN_DECL sn_get_numa_overall(int *out_numa_count, int *count_of_each_n
for (int index = 0; index <= max_context.max_value; index++)
count_of_each_numa[index] = 0;
sn_linux_fill_node_count_context fill_context = {count_of_each_numa};
sn_linux_fill_node_count_context fill_context = { count_of_each_numa };
if (!sn_linux_visit_id_list(buffer, sn_linux_fill_node_cpu_count, &fill_context))
return 0;
}
@@ -277,7 +275,7 @@ SN_API int SN_DECL sn_get_numa_each(int numa_id, uint64_t *internal, uint32_t *g
if (!sn_linux_read_first_line(path, buffer, sizeof(buffer)))
return 0;
sn_linux_fill_cpu_context fill_context = {internal, global, 0};
sn_linux_fill_cpu_context fill_context = { internal, global, 0 };
return sn_linux_visit_id_list(buffer, sn_linux_fill_cpu_id, &fill_context);
}
@@ -293,8 +291,7 @@ SN_API void *SN_DECL sn_numa_alloc(size_t size, int numa_id)
mmap_flags |= MAP_POPULATE;
#endif
void *memory = mmap(NULL, allocation_size, PROT_READ | PROT_WRITE,
mmap_flags, -1, 0);
void *memory = mmap(NULL, allocation_size, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
if (memory == MAP_FAILED)
return NULL;
+4 -5
View File
@@ -6,9 +6,9 @@
/* 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)
SN_API void SN_DECL sn_set_thread_affinity(int core_id, int numa_id)
{
if (core_id < 0) {
if (core_id < 0 || numa_id < 0) {
/* Query which group the thread currently belongs to */
GROUP_AFFINITY current;
GetThreadGroupAffinity(GetCurrentThread(), &current);
@@ -21,14 +21,13 @@ SN_API void SN_DECL sn_set_thread_affinity(int core_id)
SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL);
return;
}
WORD group = (WORD)(core_id / 64);
int index = core_id % 64;
DWORD count = GetActiveProcessorCount(group);
DWORD count = GetActiveProcessorCount(numa_id);
if ((DWORD)index >= count)
return;
GROUP_AFFINITY ga;
memset(&ga, 0, sizeof(ga));
ga.Group = group;
ga.Group = (WORD)numa_id;
ga.Mask = (KAFFINITY)1 << index;
SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL);
}