-
Notifications
You must be signed in to change notification settings - Fork 0
/
std_queue_shared_next_numa_split.cpp
213 lines (189 loc) · 6.02 KB
/
std_queue_shared_next_numa_split.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <queue>
#include <iostream>
#include <pthread.h>
#include <thread>
#include <atomic>
#include <chrono>
#include <mutex>
#include <hwloc.h>
#include <condition_variable>
#include <helper.h>
#include <cmath>
struct Workitem {
int _id;
std::string _msg;
Workitem(int id, std::string msg) {
_id = id;
_msg = msg;
}
};
typedef std::queue<std::shared_ptr<Workitem>> queue_type;
class Producer;
class Worker {
queue_type queue;
std::mutex mutex;
Producer &producer;
int _node;
int _sum;
friend class Producer;
public:
Worker(Producer &p, int node);
void work();
};
class Producer {
std::vector<Worker*> _worker_instances;
std::vector<std::thread> _worker_threads;
std::atomic_int _status;
std::mutex _mutex;
int _iterations;
int _node;
friend class Worker;
public:
Producer(int threads, int iterations, int node);
~Producer();
void register_worker(Worker* worker);
void waitForRegistered();
void run(std::condition_variable& start, std::mutex& start_mutex, std::atomic_size_t& next);
};
Worker::Worker(Producer &p, int node) : producer(p), _node(node), _sum(0) {
//std::cout << "Worker: registering Worker " << this << std::endl;
p.register_worker(this);
//bindToNode(node);
//pin_to_core(10);
pinToNode(node);
}
void Worker::work() {
//std::cout << "Worker: Starting" << std::endl;
while (producer._status != 0) {
mutex.lock();
if(!queue.empty()) {
int i = (queue.front())->_id;
_sum += i;
//std::cout << i << std::endl;
queue.pop();
mutex.unlock();
}
else{
mutex.unlock();
std::this_thread::yield();
}
}
//while(true) {
// mutex.lock();
while(!queue.empty()) {
int i = queue.front()->_id;
_sum += i;
//std::cout << i << std::endl;
queue.pop();
// mutex.unlock();
}
std::cout << _sum << std::endl;
//}
}
Producer::Producer(int threads, int iterations, int node) : _status(-1), _iterations(iterations), _node(node){
_status = -1;
//bindToNode(node);
//pinToNode(node);
for(int i = 0; i < threads; i++) {
std::thread thread([this] {
Worker worker(*this, this->_node);
worker.work();
});
_worker_threads.push_back(std::move(thread));
}
waitForRegistered();
//std::cout << "Producer on Node" << node << ": All Workers registered!" << std::endl;
_status = 1;
}
Producer::~Producer() {
if(_worker_threads.size() > 0) {
_worker_threads.clear();
}
}
void Producer::register_worker(Worker* worker) {
std::lock_guard<std::mutex> lock(_mutex);
_worker_instances.push_back(worker);
//std::cout << "Producer: registered Worker " << worker << std::endl;
}
void Producer::waitForRegistered() {
while(true) {
_mutex.lock();
if (_worker_instances.size() >= _worker_threads.size()) {
_mutex.unlock();
break;
}
else {
_mutex.unlock();
sleep(0.5);
}
}
}
void Producer::run(std::condition_variable& start, std::mutex& start_mutex, std::atomic_size_t& shared_next) {
pinToNode(_node);
//std::cout << "Producer: Waiting" << std::endl;
{
std::unique_lock<std::mutex> lk(start_mutex);
start.wait(lk);
}
//std::cout << "Producer: Starting" << std::endl;
//int _sum = 0;
//while (true) {_sum += 1;}
for (int i = 0; i < _iterations ; ++i) {
Worker* nextWorker = _worker_instances[shared_next.fetch_add(1) % _worker_instances.size()];
nextWorker->mutex.lock();
nextWorker->queue.push(std::make_shared<Workitem>(i,"Workitem"));
nextWorker->mutex.unlock();
}
//std::cout << "Producer: Finished Generating!" << std::endl;
_status = 0;
for(size_t i = 0; i < _worker_threads.size(); i++){
_worker_threads[i].join();
//std::cout << "Producer: Thread " << i << " joined!" << std::endl;
}
}
int main(int argc, char *argv[]) {
if (argc != 3) {
std::cout<< "usage: " << argv[0] << "<num_threads> <num_workitems> " << std::endl;
exit(1);
}
size_t threads = std::atoi(argv[1]);
size_t total_items = std::atoi(argv[2]);
std::condition_variable start_cv;
std::mutex start_mutex;
std::atomic_size_t next;
std::vector<std::vector<unsigned>> cores = getCoresForNodes(threads+1);
//printVV(cores);
std::vector<Producer*> producers;
std::vector<std::thread> prod_threads;
size_t num_nodes = getNumberOfNodes(getTopology());
if (threads < num_nodes) {num_nodes = threads;}
for (size_t nodes = num_nodes; nodes > 0; --nodes) {
size_t threads_for_node = ceil(float(threads)/nodes);
size_t node = num_nodes - nodes;
#ifdef DEBUG
std::cout << "Main: Creating " << threads_for_node << " threads on node " << num_nodes - nodes << "..." << std::endl;
#endif
Producer* prod = new Producer(threads_for_node ,total_items/num_nodes, node);
producers.push_back(std::move(prod));
threads -= threads_for_node;
#ifdef DEBUG
std::cout << "Main: Finished. Remaining threads: " << threads << ", remaining nodes: " << nodes-1 << "." << std::endl;
#endif
}
for(auto p : producers) {
std::thread thread(&Producer::run, p, std::ref(start_cv), std::ref(start_mutex), std::ref(next));
prod_threads.push_back(std::move(thread));
}
sleep(2);
#ifdef DEBUG
std::cout << "Starting clock..." << std::endl;
#endif
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now() ;
start_cv.notify_all();
for(auto& p : prod_threads) {p.join();}
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now() ;
typedef std::chrono::duration<int,std::milli> millisecs_t ;
millisecs_t duration( std::chrono::duration_cast<millisecs_t>(end-start) ) ;
std::cout << duration.count() << " ms.\n" ;
return 0;
}