monitor and threads
This commit is contained in:
65
src/Core/testing/AffinityTest.cpp
Normal file
65
src/Core/testing/AffinityTest.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "Core/Threads.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
#ifdef __linux__
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#endif
|
||||
|
||||
using namespace uLib;
|
||||
|
||||
void TestThreadAffinity() {
|
||||
std::cout << "Testing Thread Affinity..." << std::endl;
|
||||
#ifdef __linux__
|
||||
Thread t;
|
||||
t.Start();
|
||||
t.SetAffinity(0); // Bind to CPU 0
|
||||
|
||||
cpu_set_t cpuset;
|
||||
CPU_ZERO(&cpuset);
|
||||
pthread_getaffinity_np(t.GetNativeHandle(), sizeof(cpu_set_t), &cpuset);
|
||||
assert(CPU_ISSET(0, &cpuset));
|
||||
|
||||
t.Join();
|
||||
std::cout << " Passed (Thread bound to CPU 0)." << std::endl;
|
||||
#else
|
||||
std::cout << " Affinity not supported on this OS, skipping." << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
void TestTeamAffinity() {
|
||||
std::cout << "Testing Team Affinity..." << std::endl;
|
||||
#ifdef __linux__
|
||||
#ifdef _OPENMP
|
||||
Team team(2);
|
||||
std::vector<int> cpus = {0, 1};
|
||||
team.SetAffinity(cpus);
|
||||
|
||||
// We check affinity inside a parallel region
|
||||
#pragma omp parallel
|
||||
{
|
||||
cpu_set_t cpuset;
|
||||
CPU_ZERO(&cpuset);
|
||||
pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
|
||||
int tid = Thread::GetThreadNum();
|
||||
int expected_cpu = cpus[tid % cpus.size()];
|
||||
assert(CPU_ISSET(expected_cpu, &cpuset));
|
||||
}
|
||||
std::cout << " Passed (Team threads bound correctly)." << std::endl;
|
||||
#endif
|
||||
#else
|
||||
std::cout << " Affinity not supported on this OS, skipping." << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Helper to get native handle if needed (oops, I forgot to add it to Thread class)
|
||||
// I'll add GetNativeHandle() to Thread class in Threads.h
|
||||
|
||||
int main() {
|
||||
TestThreadAffinity();
|
||||
TestTeamAffinity();
|
||||
std::cout << "All Affinity tests finished!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -25,6 +25,9 @@ set( TESTS
|
||||
HRPTest
|
||||
MutexTest
|
||||
ThreadsTest
|
||||
OpenMPTest
|
||||
TeamTest
|
||||
AffinityTest
|
||||
)
|
||||
|
||||
set(LIBRARIES
|
||||
@@ -33,6 +36,7 @@ set(LIBRARIES
|
||||
Boost::serialization
|
||||
Boost::program_options
|
||||
${ROOT_LIBRARIES}
|
||||
OpenMP::OpenMP_CXX
|
||||
)
|
||||
uLib_add_tests(Core)
|
||||
|
||||
|
||||
47
src/Core/testing/OpenMPTest.cpp
Normal file
47
src/Core/testing/OpenMPTest.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "Core/Threads.h"
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
|
||||
#ifdef _OPENMP
|
||||
#include <omp.h>
|
||||
#endif
|
||||
|
||||
using namespace uLib;
|
||||
|
||||
class OpenMPThread : public Thread {
|
||||
public:
|
||||
void Run() override {
|
||||
#ifdef _OPENMP
|
||||
Thread::SetNumThreads(2);
|
||||
int max = Thread::GetNumThreads();
|
||||
std::cout << " OpenMP max threads in uLib::Thread: " << max << std::endl;
|
||||
|
||||
int shared_counter = 0;
|
||||
#pragma omp parallel reduction(+:shared_counter)
|
||||
{
|
||||
shared_counter += 1;
|
||||
}
|
||||
std::cout << " Parallel region executed with " << shared_counter << " threads." << std::endl;
|
||||
assert(shared_counter <= max);
|
||||
#else
|
||||
std::cout << " OpenMP not available, skipping parallel check." << std::endl;
|
||||
assert(Thread::GetNumThreads() == 1);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::cout << "Testing OpenMP compatibility..." << std::endl;
|
||||
#ifdef _OPENMP
|
||||
std::cout << " OpenMP is AVAILABLE." << std::endl;
|
||||
#else
|
||||
std::cout << " OpenMP is NOT available." << std::endl;
|
||||
#endif
|
||||
|
||||
OpenMPThread t;
|
||||
t.Start();
|
||||
t.Join();
|
||||
|
||||
std::cout << "OpenMP compatibility test finished!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
40
src/Core/testing/TeamTest.cpp
Normal file
40
src/Core/testing/TeamTest.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "Core/Threads.h"
|
||||
#include <iostream>
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
using namespace uLib;
|
||||
|
||||
void TestTaskTeam() {
|
||||
std::cout << "Testing Task and Team..." << std::endl;
|
||||
|
||||
std::atomic<int> counter(0);
|
||||
auto task_func = [&]() {
|
||||
counter++;
|
||||
Thread::Sleep(10);
|
||||
};
|
||||
|
||||
Team team(4);
|
||||
std::cout << " Team size: " << team.GetSize() << std::endl;
|
||||
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel
|
||||
#pragma omp single
|
||||
#endif
|
||||
{
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
team.Run(new Task(task_func));
|
||||
}
|
||||
team.Wait();
|
||||
}
|
||||
|
||||
assert(counter == 20);
|
||||
std::cout << " Passed (counter: " << counter << ")." << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
TestTaskTeam();
|
||||
std::cout << "All Team tests passed!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user