Debugged multithreaded version. Now investigating some performance issues (not every thread is being used). This is an interesting version.
This commit is contained in:
93
inc/sync.h
93
inc/sync.h
@@ -1,5 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
@@ -17,6 +21,14 @@ typedef LPVOID ThreadArg;
|
||||
|
||||
const TimeSpan infinite_ts = { .QuadPart = LLONG_MAX };
|
||||
|
||||
int get_num_cores() {
|
||||
SYSTEM_INFO sysinfo;
|
||||
GetSystemInfo(&sysinfo);
|
||||
return sysinfo.dwNumberOfProcessors;
|
||||
}
|
||||
|
||||
const int num_cores = get_num_cores();
|
||||
|
||||
LARGE_INTEGER _init_freq() {
|
||||
LARGE_INTEGER freq;
|
||||
QueryPerformanceFrequency(&freq);
|
||||
@@ -27,8 +39,13 @@ static LARGE_INTEGER freq = _init_freq();
|
||||
#endif
|
||||
|
||||
Thread make_thread(ThreadFunc t, ThreadArg a);
|
||||
Thread make_thread(ThreadFunc t, ThreadArg a, int core_affinity);
|
||||
void join(Thread t);
|
||||
void sleep(TimeSpan ts);
|
||||
void allow_all_processors();
|
||||
void set_affinity(Thread &t, int core);
|
||||
void set_affinity(int core);
|
||||
int get_affinity();
|
||||
|
||||
Mutex make_mutex();
|
||||
void lock(Mutex &m);
|
||||
@@ -64,11 +81,60 @@ double to_hours(TimeSpan &ts);
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
uint64_t bitmask (unsigned short n) {
|
||||
if (n == 64) return -((uint64_t)1);
|
||||
return (((uint64_t) 1) << n) - 1;
|
||||
}
|
||||
|
||||
const int tab64[64] = {
|
||||
63, 0, 58, 1, 59, 47, 53, 2,
|
||||
60, 39, 48, 27, 54, 33, 42, 3,
|
||||
61, 51, 37, 40, 49, 18, 28, 20,
|
||||
55, 30, 34, 11, 43, 14, 22, 4,
|
||||
62, 57, 46, 52, 38, 26, 32, 41,
|
||||
50, 36, 17, 19, 29, 10, 13, 21,
|
||||
56, 45, 25, 31, 35, 16, 9, 12,
|
||||
44, 24, 15, 8, 23, 7, 6, 5};
|
||||
|
||||
int log2_64 (uint64_t value)
|
||||
{
|
||||
value |= value >> 1;
|
||||
value |= value >> 2;
|
||||
value |= value >> 4;
|
||||
value |= value >> 8;
|
||||
value |= value >> 16;
|
||||
value |= value >> 32;
|
||||
return tab64[((uint64_t)((value - (value >> 1))*0x07EDD5E59A4E28C2)) >> 58];
|
||||
}
|
||||
|
||||
Thread make_thread(ThreadFunc f, ThreadArg a) {
|
||||
DWORD tid;
|
||||
return CreateThread(NULL, 0, f, a, 0, &tid);
|
||||
}
|
||||
|
||||
struct DummyThreadArgs {
|
||||
int core_affinity;
|
||||
ThreadFunc f;
|
||||
ThreadArg a;
|
||||
};
|
||||
|
||||
DWORD _dummy_thread(LPVOID a) {
|
||||
DummyThreadArgs *wrap = static_cast<DummyThreadArgs*>(a);
|
||||
set_affinity(wrap->core_affinity);
|
||||
return wrap->f(wrap->a);
|
||||
}
|
||||
|
||||
Thread make_thread(ThreadFunc f, ThreadArg a, int core_affinity) {
|
||||
DWORD tid;
|
||||
DummyThreadArgs *args = (DummyThreadArgs*)malloc(sizeof(DummyThreadArgs));
|
||||
*args = {
|
||||
.core_affinity=core_affinity,
|
||||
.f=f,
|
||||
.a=a
|
||||
};
|
||||
return CreateThread(NULL, 0, _dummy_thread, args, 0, &tid);
|
||||
}
|
||||
|
||||
void join(Thread t) {
|
||||
WaitForSingleObject(t, INFINITE);
|
||||
}
|
||||
@@ -77,6 +143,33 @@ void sleep(TimeSpan ts) {
|
||||
Sleep(static_cast<DWORD>(to_ms(ts)));
|
||||
}
|
||||
|
||||
void allow_all_processors() {
|
||||
Thread t = GetCurrentThread();
|
||||
DWORD affinity = bitmask(num_cores);
|
||||
SetProcessAffinityMask(t, affinity);
|
||||
}
|
||||
|
||||
void set_affinity(Thread &t, int core) {
|
||||
DWORD mask = 1 << (core % num_cores);
|
||||
DWORD old = SetThreadAffinityMask(t, mask);
|
||||
DWORD confirm = SetThreadAffinityMask(t, mask);
|
||||
assert(old && GetLastError() != ERROR_INVALID_PARAMETER && mask == confirm);
|
||||
}
|
||||
|
||||
void set_affinity(int core) {
|
||||
Thread cur = GetCurrentThread();
|
||||
set_affinity(cur, core);
|
||||
}
|
||||
|
||||
int get_affinity() {
|
||||
Thread t = GetCurrentThread();
|
||||
DWORD mask = 1;
|
||||
DWORD affinity = SetThreadAffinityMask(t, (DWORD_PTR)mask);
|
||||
DWORD check = SetThreadAffinityMask(t, (DWORD_PTR)affinity);
|
||||
assert(check == mask);
|
||||
return log2_64(affinity);
|
||||
}
|
||||
|
||||
Mutex make_mutex() {
|
||||
Mutex m;
|
||||
InitializeCriticalSection(&m);
|
||||
|
||||
Reference in New Issue
Block a user