-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpipe.cu
290 lines (242 loc) · 9.42 KB
/
gpipe.cu
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
#include "gpipe.cuh"
#include <math.h>
typedef struct PipeConsts
{
size_t queueSize;
size_t blockSize;
size_t blockSlotCount;
size_t queueSlotCount;
size_t userBufferSlotCount;
size_t LastSlotOccupancy;
size_t maxBlocksPerThread;
size_t blockSyncFactor;
} pipeConsts;
template <typename message_t>
struct GPipe
{
GPipeData_t *_gpipeData;
pipeConsts _pipeConsts;
size_t _threadsCount;
memoryProperties _memProperties;
const char* _fullPath;
bool _isConsumer;
__host__ pipeConsts getPipeSizeData(int threadsCount, int size_multiplier, int sync_rate_target)
{
pipeConsts consts;
size_t userBufferSize = threadsCount * sizeof(message_t);
size_t maxSlotsPerMessage = ROUND_UP_DIVISION(sizeof(message_t), sizeof(int));
consts.blockSyncFactor = getClosestFactor(sync_rate_target, maxSlotsPerMessage);
consts.blockSlotCount = threadsCount * consts.blockSyncFactor;
consts.blockSize = consts.blockSlotCount * sizeof(int);
consts.queueSize = ROUND_UP(size_multiplier * userBufferSize, consts.blockSize);
consts.queueSlotCount = consts.queueSize / sizeof(int);
consts.LastSlotOccupancy = (userBufferSize % sizeof(int));
consts.LastSlotOccupancy = consts.LastSlotOccupancy == 0 ? 4 : consts.LastSlotOccupancy;
consts.userBufferSlotCount = ROUND_UP_DIVISION(userBufferSize, sizeof(int));
consts.maxBlocksPerThread = ROUND_UP_DIVISION(consts.userBufferSlotCount, consts.blockSlotCount);
// Print all const
dbg_printf("Block size is %lu\n", consts.blockSize);
dbg_printf("Block slot count is %lu\n", consts.blockSlotCount);
dbg_printf("Queue size is %lu\n", consts.queueSize);
dbg_printf("Queue slot count is %lu\n", consts.queueSlotCount);
dbg_printf("Last slot Occupancy is %lu\n", consts.LastSlotOccupancy);
dbg_printf("userBuffer slot count is %lu\n", consts.userBufferSlotCount);
dbg_printf("Max slots per thread count is %lu\n", consts.maxBlocksPerThread);
dbg_printf("Sync rate is set to %lu\n", consts.blockSyncFactor);
return consts;
}
__host__ char* getPipeFullPath(const char* pipe_name)
{
const char* path = PIPE_PATH;
char *name = new char[strlen(pipe_name) + strlen(path) + 1];
sprintf(name, "%s%s", path, pipe_name);
return name;
}
GPipe(const char* pipe_name, bool isConsumer, int size_multiplier, int threadsCount, int sync_rate_target)
{
_fullPath = getPipeFullPath(pipe_name);
_pipeConsts = getPipeSizeData(threadsCount, size_multiplier, sync_rate_target);
_threadsCount = threadsCount;
_isConsumer = isConsumer;
_gpipeData = nullptr;
}
__host__ void gclose()
{
cleanMemoryMaping(_memProperties);
}
__host__ void initGpipeData(GPipeData_t *gpipeData)
{
cuda::atomic<size_t, cuda::thread_scope_system> *head;
cuda::atomic<size_t, cuda::thread_scope_system> *tail;
head = new cuda::atomic<size_t, cuda::thread_scope_system>(0);
tail = new cuda::atomic<size_t, cuda::thread_scope_system>(0);
int* data = new int[_pipeConsts.queueSlotCount];
int *ptr = (int*)((char*)(_gpipeData) + sizeof(GPipeData_t));
CUDA_CHECK(cudaMemcpy(&(gpipeData->_head), head, sizeof(*head), cudaMemcpyHostToDevice));
CUDA_CHECK(cudaMemcpy(&(gpipeData->_tail), tail, sizeof(*tail), cudaMemcpyHostToDevice));
CUDA_CHECK(cudaMemcpy(&(gpipeData->_messagesQueue), &ptr, sizeof(int*), cudaMemcpyHostToDevice));
CUDA_CHECK(cudaMemcpy(ptr, data, _pipeConsts.queueSize, cudaMemcpyHostToDevice));
delete(data);
}
__host__ void initConsumer()
{
dbg_printf("Initialize consumer with socket name %s\n", _fullPath);
// Create sharable memory
ShareableHandle ipc_handle;
size_t alloc_size = sizeof(GPipeData_t) + _pipeConsts.queueSize;
_memProperties = allocateSharableMemory(alloc_size, &ipc_handle);
dbg_printf("Consumer initialize: finish allocation\n");
_gpipeData = (GPipeData_t *)(_memProperties.ptr);
initGpipeData(_gpipeData);
// Create arguments
GpipeInitArguments initArguments;
initArguments._queue_size = _pipeConsts.queueSize;
initArguments._threads_count = _threadsCount;
initArguments._alloc_size = _memProperties.alloc_size;
// Send arguments to producer
dbg_printf("Consumer queue size %lu\n", initArguments._queue_size);
dbg_printf("Consumer readers count %lu\n", initArguments._threads_count);
dbg_printf("Consumer allocation size %lu\n", initArguments._alloc_size);
dbg_printf("Sending arguments to producer...\n");
int socket_fd = create_socket(_fullPath);
send_args(socket_fd, ipc_handle, &initArguments, sizeof(GpipeInitArguments));
ipcCloseSocket(socket_fd, _fullPath);
close(ipc_handle);
dbg_printf("Consumer: successfully sent all arguments to producer\n");
}
__host__ void initProducer()
{
dbg_printf("Initialize producer with socket name %s\n", _fullPath);
dbg_printf("Producer queue size %d\n", (int)_pipeConsts.queueSize);
dbg_printf("Producer readers count %d\n", (int)_threadsCount);
GpipeInitArguments initArguments;
ShareableHandle ipc_handle = recv_arguments(_fullPath, &initArguments, sizeof(GpipeInitArguments));
// Validate initialize arguments
if (initArguments._queue_size != _pipeConsts.queueSize || initArguments._threads_count != _threadsCount)
{
dbg_printf("Error: Mismatching pipe arguments! \n");
exit(-1);
}
_memProperties = importAndMapMemory(ipc_handle, initArguments._alloc_size);
_gpipeData = (GPipeData_t *)(_memProperties.ptr);
dbg_printf("Producer successfully initialized\n");
}
__host__ void init()
{
validateDeviceIsSupported();
if (_isConsumer)
initConsumer();
else
initProducer();
}
__device__ void gread(message_t *messages_buffer)
{
const int tid = GetThreadNum();
// Set pipe wrapper
GPipeData_t *data = (GPipeData_t *)_gpipeData;
int *buffer = (int*)messages_buffer;
int buffer_offset = tid;
for (int b = 0; b < _pipeConsts.maxBlocksPerThread; b++)
{
__shared__ size_t _shared_head;
if (tid == 0)
{
dbg_printf("Read - wait for messages\n");
_shared_head = data->_head.load(cuda::memory_order::memory_order_relaxed);
while (data->_tail.load(cuda::memory_order::memory_order_relaxed) - _shared_head < _pipeConsts.blockSlotCount);
cuda::atomic_thread_fence(cuda::memory_order_acquire, cuda::thread_scope_system);
dbg_printf("Read data from head %lu\n", _shared_head);
}
__syncthreads();
int queue_offset = _shared_head + tid;
for (int k = 0; k < _pipeConsts.blockSyncFactor; k++)
{
if (buffer_offset < _pipeConsts.userBufferSlotCount - 1)
{
int queue_offset_modulo = (queue_offset % _pipeConsts.queueSlotCount);
// dbg_printf("Read data from index %d in queue size to read offset %d \n", queue_offset, buffer_offset);
memcpy(buffer + buffer_offset, &(data->_messagesQueue[queue_offset_modulo]), sizeof(int));
}
// Is last slot
else if (buffer_offset == _pipeConsts.userBufferSlotCount - 1)
{
dbg_printf("Read remain bytes\n");
memcpy(buffer + buffer_offset, &(data->_messagesQueue[queue_offset % _pipeConsts.queueSlotCount]), _pipeConsts.LastSlotOccupancy);
}
else
{
break;
}
queue_offset += _threadsCount;
buffer_offset += _threadsCount;
}
__syncthreads();
if (tid == 0)
{
data->_head.store(_shared_head + _pipeConsts.blockSlotCount, cuda::memory_order::memory_order_release);
}
}
if (tid == 0) dbg_printf("Finish read message\n");
}
__device__ void gwrite(message_t *messages_buffer)
{
const int tid = GetThreadNum();
// Set pipe wrapper
GPipeData_t *data = (GPipeData_t*)_gpipeData;
int *buffer = (int*)messages_buffer;
int buffer_offset = tid;
for (int b = 0; b < _pipeConsts.maxBlocksPerThread; b++)
{
__shared__ size_t _shared_tail;
if (tid == 0)
{
dbg_printf("Write - wait for slots\n");
_shared_tail = data->_tail.load(cuda::memory_order::memory_order_relaxed);
while (_shared_tail - data->_head.load(cuda::memory_order::memory_order_relaxed) > _pipeConsts.queueSlotCount - _pipeConsts.blockSlotCount);
cuda::atomic_thread_fence(cuda::memory_order_acquire, cuda::thread_scope_system);
dbg_printf("Write tail number %lu\n", _shared_tail);
dbg_printf("Write head number %lu\n", data->_head.load(cuda::memory_order::memory_order_relaxed));
}
__syncthreads();
int queue_offset = _shared_tail + tid;
for (int k = 0; k < _pipeConsts.blockSyncFactor; k++)
{
if (buffer_offset < _pipeConsts.userBufferSlotCount - 1)
{
int queue_offset_modulo = (queue_offset % _pipeConsts.queueSlotCount);
// dbg_printf("Write data from index %d in queue size to buffer in %d \n", queue_offset, buffer_offset);
memcpy(&(data->_messagesQueue[queue_offset_modulo]), buffer + buffer_offset, sizeof(int));
}
else if (buffer_offset == _pipeConsts.userBufferSlotCount - 1)
{
dbg_printf("Write remain %llu bytes\n", _pipeConsts.LastSlotOccupancy);
memcpy(&(data->_messagesQueue[queue_offset % _pipeConsts.queueSlotCount]), buffer + buffer_offset, _pipeConsts.LastSlotOccupancy);
}
else
{
break;
}
queue_offset += _threadsCount;
buffer_offset += _threadsCount;
}
__syncthreads();
if (tid == 0)
{
data->_tail.store(_shared_tail + _pipeConsts.blockSlotCount, cuda::memory_order::memory_order_release);
}
}
}
__device__ void write_many(message_t** messages, int number_of_messages)
{
dbg_printf("write many start\n");
for (int i = 0; i < number_of_messages; i++)
{
gwrite(messages[i]);
}
}
__device__ void clean_queue(message_t* emptyMessage)
{
for(int i = 0; i < this->_queueSlotCount; i++)
_gpipeData->_messagesQueue[i] = *emptyMessage;
}
};