-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkerPoolApp.cpp
66 lines (50 loc) · 2.19 KB
/
WorkerPoolApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <WorkerPool.h>
#include <Processor.h>
auto runWithCoreAffinity() -> void {
dxpool::Processor processor;
const auto cores = processor.FindAvailableCores();
dxpool::WorkerPoolBuilder builder;
auto pool = builder.OnCores(cores)
.WithThreadsPerCore(1) // create one thread per core
.Build();
std::cout << "number of worker threads in the pool: " << pool->Size() << std::endl;
auto taskWithResult = [](int valueA, int valueB) -> int { return valueA + valueB;};
auto result = pool->Submit<decltype(taskWithResult), int, int, int>(std::move(taskWithResult), 1, 1);
std::cout << "task returned " << result.get() << std::endl;
// fire and forget task, the pool will wait for it to finish when going out of scope
pool->Submit([&processor] {
const auto thisCore = processor.FindAvailableCores();
std::cout << "task running on core " << thisCore.begin()->GetID() << std::endl;
});
}
auto runWithNUMAAffinity() -> void {
dxpool::Processor processor;
const auto node = processor.FindAvailableNumaNodes();
dxpool::WorkerPoolBuilder builder;
auto pool = builder.OnNUMANode(*node.begin())
.WithThreadsPerCore(1)
.Build();
std::cout << "number of worker threads in the pool: " << pool->Size() << std::endl;
pool->Submit([&processor] {
const auto thisCore = processor.FindAvailableCores();
const auto thisNUMANode = processor.FindAvailableNumaNodes();
std::cout << "running on NUMA node " << thisNUMANode.begin()->GetID() << " core " << thisCore.begin()->GetID() << std::endl;
});
}
auto runAllExamples() -> void {
std::cout << "-- executing tasks with core affinity" << std::endl;
runWithCoreAffinity();
std::cout << std::endl;
std::cout << "-- executing tasks with NUMA node affinity" << std::endl;
runWithNUMAAffinity();
}
auto main() -> int {
// WorkerPoolBuilder can throw InvalidWorkerPoolBuilderArgumentsError
try {
runAllExamples();
} catch(const dxpool::InvalidWorkerPoolBuilderArgumentsError& ex) {
std::cerr << "Error running examples: " << ex.what() << std::endl;
}
return 0;
}