-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_handler.h
300 lines (224 loc) · 9.09 KB
/
thread_handler.h
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
Copyright (c) 2024, Hammurabi Mendes.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef THREAD_HANDLER_H
#define THREAD_HANDLER_H
#ifdef USE_MPI
#include <mpi.h>
#endif /* USE_MPI */
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <vector>
#include <array>
#include <queue>
#include <unordered_map>
#include <iostream>
#include <unistd.h>
#include <limits>
#ifdef DEBUG
#include <cassert>
#endif /* DEBUG */
#include "ibutils.hpp"
#include "utils/Queues.hpp"
#include "utils/Barrier.hpp"
#include "utils/TimeHolder.hpp"
#include "utils/Random.hpp"
#include "utils/AffinityHandler.hpp"
using std::thread;
using std::recursive_mutex;
using std::condition_variable;
using std::atomic;
using std::vector;
using std::array;
using std::queue;
using std::unordered_map;
using std::cout;
using std::cerr;
using std::endl;
using std::ios;
namespace seriema {
/*
* Definitions
*/
constexpr uint64_t QP_GLOBAL_FLUSH_INTERVAL = 15360;
constexpr uint64_t QP_THREAD_FLUSH_INTERVAL = 1024;
constexpr uint64_t THREAD_INCOMING_MEMORY_BUFFERS = 128;
constexpr uint64_t THREAD_OUTGOING_MEMORY_BUFFERS = 128;
// Best size for rate: 512B
// Best size for bandwidth: 16K
constexpr uint64_t RDMA_MEMORY_SIZE = 16384;
constexpr uint64_t GLOBAL_ALLOCATOR_CHUNK_SIZE = 4096 * 512;
constexpr uint64_t GLOBAL_ALLOCATOR_SUPERCHUNK_SIZE = 4096 * 512 * 512;
#define FLAG_DUMMY (1 << 24)
#define FLAG_SERVICE (1 << 25)
#define FLAG_SINGLE (1 << 26)
#define FLAG_MULTIPLE (1 << 27)
/*
* Rank-related globals
*/
extern int number_processes;
extern int process_rank;
extern int number_threads;
extern int number_threads_process;
extern int local_process_rank;
extern int local_number_processes;
extern MPI_Comm local_communicator;
extern thread_local int thread_rank;
extern thread_local int thread_id;
extern thread_local vector<int> local_thread_ids;
/*
* Operation-related globals
*/
extern vector<thread *> service_threads;
extern recursive_mutex print_mutex;
#ifdef USE_MPI
extern MPI_Request global_request;
#endif /* USE_MPI */
/*
* Configuration-related data
*/
// Best with number_service_threads == multiplier_queue_pairs, multiple_completion_queues = false
struct Configuration {
int number_threads_process;
bool single_thread_queue_pairs;
int multiplier_queue_pairs = 1;
bool multiple_completion_queues;
int number_service_threads = 1;
int device = -1;
int device_port = -1;
int service_microsleep = 0;
bool create_completion_channel_shared = false;
bool create_completion_channel_private = false;
Configuration(int number_threads_process = 1, bool single_thread_queue_pairs = false, bool multiple_completion_queues = false): number_threads_process{number_threads_process}, single_thread_queue_pairs{single_thread_queue_pairs}, multiple_completion_queues{multiple_completion_queues} {
if(single_thread_queue_pairs) {
multiplier_queue_pairs = number_threads_process;
}
}
void check_configuration() {
uint64_t number_queue_pairs = multiplier_queue_pairs * number_processes;
if(number_service_threads != 1 && number_service_threads != multiplier_queue_pairs) {
cerr << "Invalid configuration: number_service_threads different than 1 and different than multiplier_queue_pairs" << endl;
exit(EXIT_FAILURE);
}
if(number_service_threads != 1 && !multiple_completion_queues) {
cerr << "Invalid configuration: number_service_threads is not 1 and multiple_completion_queues is False" << endl;
exit(EXIT_FAILURE);
}
}
};
extern Configuration global_configuration;
extern IBContext *context;
extern IBQueuePair **queue_pairs;
extern IBTransmitter<QP_GLOBAL_FLUSH_INTERVAL, QP_THREAD_FLUSH_INTERVAL> **transmitters;
extern int number_queue_pairs;
/*
* Process-mapping and transmitter-mapping functions
*/
inline uint64_t get_transmitter_index(int destination_thread_id) noexcept {
#ifdef DEBUG
assert(destination_thread_id < seriema::number_threads);
#endif /* DEBUG */
uint64_t destination_process_rank = destination_thread_id / number_threads_process;
uint64_t destination_thread_rank = destination_thread_id % number_threads_process;
return (destination_process_rank * global_configuration.multiplier_queue_pairs) + (destination_thread_rank % global_configuration.multiplier_queue_pairs);
}
inline IBTransmitter<QP_GLOBAL_FLUSH_INTERVAL, QP_THREAD_FLUSH_INTERVAL> *get_transmitter(int destination_thread_id) noexcept {
#ifdef DEBUG
assert(destination_thread_id < seriema::number_threads);
#endif /* DEBUG */
return transmitters[get_transmitter_index(destination_thread_id)];
}
inline int get_process_rank(int thread_id) noexcept {
return thread_id / number_threads_process;
}
inline int get_thread_rank(int thread_id) noexcept {
return thread_id % number_threads_process;
}
inline int get_random_thread_id(int process_rank) noexcept {
return (process_rank * number_threads_process) + Random<int>::getRandom(0, number_threads_process - 1);
}
}; // namespace seriema
#include "memory_allocation.hpp"
namespace seriema {
extern FastQueuePC<ReceivedMessageInformation> *incoming_message_queues;
extern FastQueuePC<RDMAMemory *> *incoming_memory_queues;
extern RDMAAllocator<GLOBAL_ALLOCATOR_CHUNK_SIZE, GLOBAL_ALLOCATOR_SUPERCHUNK_SIZE> **global_allocators;
struct ThreadContext {
MemoryAllocator *incoming_allocator = nullptr;
MemoryAllocator *outgoing_allocator = nullptr;
LinearMemoryAllocator *linear_allocator = nullptr;
LinearAllocator<MemoryAllocator> *fine_outgoing_allocator = nullptr;
// Allocated and deleted outside this class
RDMAAllocator<> *global_allocator;
unordered_map<int, queue<RDMAMemory *>> incoming_memory_map;
#ifndef WITHOUT_INLINE_CALLS
char inline_buffer[MAX_INLINE_DATA];
RDMAMemory inline_memory{inline_buffer, MAX_INLINE_DATA};
#endif /* WITHOUT_INLINE_CALLS */
ThreadContext(bool create_allocators, RDMAAllocator<> *global_allocator): incoming_allocator{incoming_allocator}, outgoing_allocator{outgoing_allocator}, linear_allocator{linear_allocator}, global_allocator{global_allocator} {
if(create_allocators) {
incoming_allocator = new MemoryAllocator(global_allocator, THREAD_INCOMING_MEMORY_BUFFERS, RDMA_MEMORY_SIZE);
outgoing_allocator = new MemoryAllocator(global_allocator, THREAD_OUTGOING_MEMORY_BUFFERS, RDMA_MEMORY_SIZE);
linear_allocator = new LinearMemoryAllocator(global_allocator);
fine_outgoing_allocator = new LinearAllocator(new MemoryAllocator(global_allocator, THREAD_OUTGOING_MEMORY_BUFFERS, RDMA_MEMORY_SIZE));
}
}
virtual ~ThreadContext() {
if(fine_outgoing_allocator) {
delete fine_outgoing_allocator;
}
if(linear_allocator) {
delete linear_allocator;
}
if(incoming_allocator) {
delete incoming_allocator;
}
if(outgoing_allocator) {
delete outgoing_allocator;
}
}
};
extern thread_local ThreadContext *thread_context;
extern AffinityHandler affinity_handler;
/*
* Functions
*/
void init_thread(int offset, bool create_allocators = true);
void finalize_thread(bool perform_flush_queue_pairs = true);
void flush_send_completion_queues();
int init_thread_handler(int &argc, char **&argv, Configuration &configuration);
int finalize_thread_handler();
// TODO: Ugh
inline vector<int> &get_local_thread_ids() {
if(local_thread_ids.empty()) {
for(int i = 0; i < number_threads_process; i++) {
local_thread_ids.push_back((process_rank * number_threads_process) + i);
}
}
return local_thread_ids;
}
}; // namespace seriema
#endif /* THREAD_HANDLER_H */