find a way to create Thread binded to LogicalCore properly
This commit is contained in:
@@ -110,7 +110,7 @@
|
||||
<LanguageStandard_C>stdc11</LanguageStandard_C>
|
||||
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)lib;$(ProjectDir)sn-head;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)lib;$(ProjectDir)sn-head;$(ProjectDir)sn-template;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<!-- DLL 应该用 Windows,不是 Console -->
|
||||
@@ -172,10 +172,10 @@
|
||||
<!-- source files -->
|
||||
<!-- ════════════════════════════════════════════════════════ -->
|
||||
<ItemGroup>
|
||||
<ClCompile Include="sn-src\m.basic.x86.c" />
|
||||
<ClCompile Include="sn-src\dllmain.c" />
|
||||
<ClCompile Include="sn-src\m.bufop.x86.c" />
|
||||
<ClCompile Include="sn-src\m.solve.x86.c" />
|
||||
<ClCompile Include="sn-src\m.transpose.x86.c" />
|
||||
<ClCompile Include="sn-src\mk.fma.x86.c" />
|
||||
<ClCompile Include="sn-src\s.factorial.x86.c" />
|
||||
<ClCompile Include="sn-src\sk.fma.x86.c" />
|
||||
@@ -208,6 +208,7 @@
|
||||
<ClInclude Include="sn-head\sn_target.h" />
|
||||
<ClInclude Include="sn-head\StgSharpNative.h" />
|
||||
<ClInclude Include="sn-head\wgl.h" />
|
||||
<ClInclude Include="sn-template\sn_template.fma.x86.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="naming.md" />
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
<Filter Include="Document">
|
||||
<UniqueIdentifier>{fbd74b53-5314-4d9d-a788-07bd1b413f74}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="模板文件">
|
||||
<UniqueIdentifier>{c7081772-a85a-4372-b55c-a6dac43c6913}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="sn-src\dllmain.c">
|
||||
@@ -39,9 +42,6 @@
|
||||
<ClCompile Include="sn-src\load_glfw.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="sn-src\m.basic.x86.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="sn-src\sk.fma.x86.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
@@ -69,6 +69,9 @@
|
||||
<ClCompile Include="sn-src\thread.windows.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="sn-src\m.transpose.x86.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="sn-head\gl.h">
|
||||
@@ -113,6 +116,9 @@
|
||||
<ClInclude Include="sn-head\sn_intrinsic.std.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="sn-template\sn_template.fma.x86.h">
|
||||
<Filter>模板文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Library Include="lib\glfw\glfw3.lib">
|
||||
|
||||
@@ -54,7 +54,7 @@ SN_API SIMDID SN_DECL sn_get_simd_level_global();
|
||||
SN_API SIMDID SN_DECL sn_get_simd_level_local();
|
||||
SN_API SIMDID SN_DECL sn_get_unite_simd(SIMDID left, SIMDID right);
|
||||
SN_API int SN_DECL sn_compare_simd(SIMDID left, SIMDID right);
|
||||
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);
|
||||
SN_API void* SN_DECL sn_numa_alloc(size_t size, int numa_id);
|
||||
SN_API void SN_DECL sn_numa_free(void *ptr, size_t size, int numa_id);
|
||||
SN_API int SN_DECL sn_get_numa_each(int numa_id, uint64_t *internal, uint32_t *global);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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(), ¤t);
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user