monitor and threads
This commit is contained in:
@@ -26,6 +26,15 @@
|
||||
#include "Threads.h"
|
||||
#include <chrono>
|
||||
|
||||
#ifdef _OPENMP
|
||||
#include <omp.h>
|
||||
#endif
|
||||
|
||||
#ifdef __linux__
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#endif
|
||||
|
||||
namespace uLib {
|
||||
|
||||
Thread::Thread() : m_Running(false) {}
|
||||
@@ -81,4 +90,113 @@ void Thread::Yield() {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
|
||||
void Thread::SetAffinity(int cpu) {
|
||||
#ifdef __linux__
|
||||
if (m_Thread.joinable()) {
|
||||
cpu_set_t cpuset;
|
||||
CPU_ZERO(&cpuset);
|
||||
CPU_SET(cpu, &cpuset);
|
||||
pthread_setaffinity_np(m_Thread.native_handle(), sizeof(cpu_set_t), &cpuset);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void Thread::SetAffinity(const std::vector<int>& cpus) {
|
||||
#ifdef __linux__
|
||||
if (m_Thread.joinable()) {
|
||||
cpu_set_t cpuset;
|
||||
CPU_ZERO(&cpuset);
|
||||
for (int cpu : cpus) {
|
||||
CPU_SET(cpu, &cpuset);
|
||||
}
|
||||
pthread_setaffinity_np(m_Thread.native_handle(), sizeof(cpu_set_t), &cpuset);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void Thread::SetNumThreads(int n) {
|
||||
#ifdef _OPENMP
|
||||
omp_set_num_threads(n);
|
||||
#endif
|
||||
}
|
||||
|
||||
int Thread::GetNumThreads() {
|
||||
#ifdef _OPENMP
|
||||
return omp_get_max_threads();
|
||||
#else
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int Thread::GetThreadNum() {
|
||||
#ifdef _OPENMP
|
||||
return omp_get_thread_num();
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Team Implementation //
|
||||
|
||||
Team::Team(int num_threads) : m_Size(num_threads), m_UseOpenMP(false) {
|
||||
#ifdef _OPENMP
|
||||
m_UseOpenMP = true;
|
||||
if (m_Size > 0) omp_set_num_threads(m_Size);
|
||||
else m_Size = omp_get_max_threads();
|
||||
#else
|
||||
if (m_Size <= 0) m_Size = 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
Team::~Team() {
|
||||
Wait();
|
||||
}
|
||||
|
||||
void Team::Run(Task* task) {
|
||||
if (!task) return;
|
||||
#ifdef _OPENMP
|
||||
if (m_UseOpenMP) {
|
||||
#pragma omp task
|
||||
task->Execute();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
// Fallback to synchronous execution if no OpenMP
|
||||
task->Execute();
|
||||
}
|
||||
|
||||
void Team::Wait() {
|
||||
#ifdef _OPENMP
|
||||
if (m_UseOpenMP) {
|
||||
#pragma omp taskwait
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void Team::SetSize(int n) {
|
||||
m_Size = n;
|
||||
#ifdef _OPENMP
|
||||
if (m_UseOpenMP) omp_set_num_threads(m_Size);
|
||||
#endif
|
||||
}
|
||||
|
||||
void Team::SetAffinity(const std::vector<int>& cpus) {
|
||||
if (cpus.empty()) return;
|
||||
#ifdef __linux__
|
||||
#ifdef _OPENMP
|
||||
if (m_UseOpenMP) {
|
||||
#pragma omp parallel
|
||||
{
|
||||
int tid = omp_get_thread_num();
|
||||
int cpu = cpus[tid % cpus.size()];
|
||||
cpu_set_t cpuset;
|
||||
CPU_ZERO(&cpuset);
|
||||
CPU_SET(cpu, &cpuset);
|
||||
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace uLib
|
||||
|
||||
Reference in New Issue
Block a user