-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuffer_opencl.cc
More file actions
420 lines (359 loc) · 12.1 KB
/
Copy pathbuffer_opencl.cc
File metadata and controls
420 lines (359 loc) · 12.1 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Licensed under the BSD license. See LICENSE.txt for more details.
#include "runtime/buffer_opencl.h"
#include "runtime/rpc_opencl_common.h"
#include <CL/cl.h>
#define OpenCL_MEMCPY_ASYNC_SIZE (64 << 10) // 64KB
namespace physis {
namespace runtime {
BufferOpenCLHost::BufferOpenCLHost(size_t elm_size)
: Buffer(elm_size) {
mpi_buf_ = new BufferHost(elm_size);
deleter_ = BufferOpenCLHost::DeleteChunk;
}
BufferOpenCLHost::BufferOpenCLHost(int num_dims, size_t elm_size)
: Buffer(num_dims, elm_size) {
mpi_buf_ = new BufferHost(num_dims, elm_size);
deleter_ = BufferOpenCLHost::DeleteChunk;
}
BufferOpenCLHost::~BufferOpenCLHost() {
delete mpi_buf_;
}
void BufferOpenCLHost::DeleteChunk(void *ptr) {
free(ptr);
return;
}
void *BufferOpenCLHost::GetChunk(const IntArray &size) {
void *ptr = NULL;
if (size.accumulate(num_dims_) > 0) {
LOG_INFO() << "Trying to allocate host pinned memory of "
<< GetLinearSize(size) << " bytes.\n";
ptr = (void *) malloc(GetLinearSize(size));
}
return ptr;
}
void BufferOpenCLHost::Copyin(const void *buf, const IndexArray &offset,
const IndexArray &size) {
EnsureCapacity(offset+size);
// Offset access is not yet supported.
PSAssert(offset == 0);
memcpy(Get(), buf, GetLinearSize(size));
}
void BufferOpenCLHost::Copyin(const BufferHost &buf, const IndexArray &offset,
const IndexArray &size) {
Copyin(buf.Get(), offset, size);
}
void BufferOpenCLHost::Copyout(void *buf, const IndexArray &offset,
const IndexArray &s) {
PSAssert(offset + s <= size());
// Offset access is not yet supported.
PSAssert(offset == 0);
memcpy(buf, Get(), GetLinearSize(s));
}
void BufferOpenCLHost::Copyout(BufferHost &buf,
const IndexArray &offset,
const IndexArray &size) {
buf.EnsureCapacity(num_dims_, elm_size_, size);
Copyout(buf.Get(), offset, size);
}
void BufferOpenCLHost::MPIRecv(int src, MPI_Comm comm,
const IndexArray &offset,
const IndexArray &size) {
mpi_buf_->MPIRecv(src, comm, IndexArray(), size);
Copyin(*mpi_buf_, offset, size);
//mpi_buf_->Delete();
}
void BufferOpenCLHost::MPISend(int dst, MPI_Comm comm,
const IndexArray &offset,
const IndexArray &size) {
Copyout(*mpi_buf_, offset, size);
mpi_buf_->MPISend(dst, comm, IndexArray(), size);
//mpi_buf_->Delete();
}
//
// BufferOpenCLDev
//
BufferOpenCLDev::BufferOpenCLDev(size_t elm_size, CLbaseinfo *clinfo_in)
: Buffer(elm_size),
base_clinfo_(clinfo_in), stream_clinfo_(0) /*, strm_(0)*/,
buf_mem(0),
tmp_buf_(0),
tmp_buf_size_(0)
{
pinned_buf_ = new BufferOpenCLHost(elm_size);
deleter_ = BufferOpenCLDev::DeleteChunk; // But this deleter_ won't be used
pitch = 0;
}
BufferOpenCLDev::BufferOpenCLDev(int num_dims, size_t elm_size, CLbaseinfo *clinfo_in)
: Buffer(num_dims, elm_size),
base_clinfo_(clinfo_in), stream_clinfo_(0) /*, strm_(0)*/,
buf_mem(0),
tmp_buf_(0),
tmp_buf_size_(0)
{
pinned_buf_ = new BufferOpenCLHost(num_dims, elm_size);
deleter_ = BufferOpenCLDev::DeleteChunk; // But this deleter_ won't be used
pitch = 0;
}
BufferOpenCLDev::~BufferOpenCLDev() {
free(tmp_buf_);
tmp_buf_ = 0;
tmp_buf_size_ = 0;
delete pinned_buf_;
}
const void *BufferOpenCLDev::Get() const
{
LOG_DEBUG() << "Never use BufferOpenCLDev::Get()!!!\n";
PSAbort(1);
return 0;
}
void *&BufferOpenCLDev::Get() {
LOG_DEBUG() << "Never use BufferOpenCLDev::Get()!!!\n";
PSAbort(1);
return buf_;
}
void BufferOpenCLDev::Copyin(const void *buf, const IntArray &offset,
const IntArray &size) {
pinned_buf_->Copyin(buf, size);
Copyin(*pinned_buf_, offset, size);
}
void BufferOpenCLDev::Copyin(const BufferHost &buf, const IntArray &offset,
const IntArray &size) {
Copyin(buf.Get(), offset, size);
}
void BufferOpenCLDev::Copyin(const BufferOpenCLHost &buf,
const IntArray &offset,
const IntArray &size) {
PSAssert(offset == 0);
EnsureCapacity(offset+size);
// CUDA code waits here until stream completes all operations
CLbaseinfo *clinfo_use = stream_clinfo();
if (!clinfo_use) {
LOG_DEBUG() << "Currently stream not set\n";
clinfo_use = base_clinfo();
}
PSAssert(clinfo_use);
cl_command_queue buf_queue_ = clinfo_use->get_queue();
PSAssert(buf_queue_ != 0);
cl_int status = clEnqueueWriteBuffer(
buf_queue_, Get_buf_mem(), CL_FALSE, /* Once no block */
0, GetLinearSize(size), buf.Get(),
0, NULL, NULL);
if (status != CL_SUCCESS) {
LOG_DEBUG() << "Calling clEnqueueWriteBuffer() failed.\n";
}
// And block
clFinish(buf_queue_);
}
void BufferOpenCLDev::Copyout(void *buf, const IntArray &offset,
const IntArray &size) {
Copyout(*pinned_buf_, offset, size);
pinned_buf_->Copyout(buf, size);
}
void BufferOpenCLDev::Copyout(BufferHost &buf, const IntArray &offset,
const IntArray &size) {
Copyin(buf.Get(), offset, size);
}
void BufferOpenCLDev::Copyout(BufferOpenCLHost &buf, const IntArray &offset,
const IntArray &size) {
PSAssert(offset == 0);
PSAssert(offset + size <= this->size());
buf.EnsureCapacity(num_dims_, elm_size_, size);
// CUDA code waits here until stream completes all operations
CLbaseinfo *clinfo_use = stream_clinfo();
if (!clinfo_use) {
LOG_DEBUG() << "Currently stream not set\n";
clinfo_use = base_clinfo();
}
PSAssert(clinfo_use);
cl_command_queue buf_queue_ = clinfo_use->get_queue();
PSAssert(buf_queue_ != 0);
cl_int status = clEnqueueReadBuffer(
buf_queue_, Get_buf_mem(), CL_FALSE, /* Once no block */
0, GetLinearSize(size), buf.Get(),
0, NULL, NULL);
if (status != CL_SUCCESS) {
LOG_DEBUG() << "Calling clEnqueueReadBuffer() failed.\n";
}
// And block
clFinish(buf_queue_);
}
#if 0
void BufferOpenCLDev::Copyout(BufferOpenCLHost &buf, const IntArray &offset,
const IntArray &size, const IntArray &total_size) {
if ((size[1] == 0) && (size[2] == 0)) {
Copyout(buf, offset, size);
return;
}
buf.EnsureCapacity(num_dims_, elm_size_, size);
// CUDA code waits here until stream completes all operations
CLbaseinfo *clinfo_use = stream_clinfo();
if (!clinfo_use) {
LOG_DEBUG() << "Currently stream not set\n";
clinfo_use = base_clinfo();
}
PSAssert(clinfo_use);
cl_command_queue buf_queue_ = clinfo_use->get_queue();
PSAssert(buf_queue_ != 0);
int zord = 0;
int yord = 0;
char *ptr_pos = (char *)(buf.Get());
size_t size_read_once = size[0] * elm_size_;
for (zord = offset[2]; zord < offset[2] + size[2]; zord++){
for (yord = offset[1]; yord < offset[1] + size[1]; yord++) {
int offset_pos = offset[0] + yord * total_size[0]
+ zord * total_size[0] * total_size[1];
offset_pos *= elm_size_;
cl_int status = clEnqueueReadBuffer(
buf_queue_, Get_buf_mem(), CL_FALSE, /* Once no block */
offset_pos, size_read_once, ptr_pos,
0, NULL, NULL);
ptr_pos += size_read_once; /* DONT FORGET TO SHIFT BUFFER!! */
if (status != CL_SUCCESS) {
LOG_DEBUG() << "Calling clEnqueueReadBuffer() failed"
<< " for yord, zord:" << yord << ", " << zord <<"\n";
} // if (status != CL_SUCCESS)
} // for (yord = offset[1]; yord < offset[1] + size[1]; yord++)
} // for (zord = offset[2]; zord < offset[2] + size[2]; zord++)
// And block
clFinish(buf_queue_);
}
#else
void BufferOpenCLDev::Copyout(BufferOpenCLHost &buf, const IntArray &offset,
const IntArray &size, const IntArray &total_size) {
if ((size[1] == 0) && (size[2] == 0)) {
Copyout(buf, offset, size);
return;
}
buf.EnsureCapacity(num_dims_, elm_size_, size);
// CUDA code waits here until stream completes all operations
CLbaseinfo *clinfo_use = stream_clinfo();
if (!clinfo_use) {
LOG_DEBUG() << "Currently stream not set\n";
clinfo_use = base_clinfo();
}
PSAssert(clinfo_use);
cl_command_queue buf_queue_ = clinfo_use->get_queue();
PSAssert(buf_queue_ != 0);
int zord = 0;
int yord = 0;
char *ptr_pos = (char *)(buf.Get());
// First copyout buffer including offset region
size_t offset_begin =
offset[0] + offset[1] * total_size[0]
+ offset[2] * total_size[0] * total_size[1];
size_t offset_end =
(offset[0] + size[0] -1) + (offset[1] + size[1] -1) * total_size[0]
+ (offset[2] + size[2] -1) * total_size[0] * total_size[1]
+ 1;
size_t offset_all_bytes = offset_end - offset_begin;
offset_all_bytes *= elm_size_;
if (tmpbuf_size() < offset_all_bytes) {
free(tmpbuf());
tmpbuf() = calloc(1, offset_all_bytes);
if (!tmpbuf()) {
LOG_ERROR() << "Calloc failed\n";
PSAbort(1);
}
tmpbuf_size() = offset_all_bytes;
}
cl_int status = clEnqueueReadBuffer(
buf_queue_, Get_buf_mem(), CL_TRUE, /* block */
offset_begin * elm_size_, offset_all_bytes,
tmpbuf(),
0, NULL, NULL);
if (status != CL_SUCCESS) {
LOG_ERROR() << "Calling clEnqueueBuffer failed\n";
}
// Now copy to buf
size_t size_read_once = size[0] * elm_size_;
for (zord = 0; zord < size[2]; zord++){
for (yord = 0; yord < size[1]; yord++) {
size_t offset_pos = yord * total_size[0]
+ zord * total_size[0] * total_size[1];
offset_pos *= elm_size_;
intptr_t read_pos = (intptr_t) tmpbuf() + offset_pos;
memcpy(ptr_pos, (void *) read_pos, size_read_once);
ptr_pos += size_read_once; /* DONT FORGET TO SHIFT BUFFER!! */
} // for (yord = offset[1]; yord < offset[1] + size[1]; yord++)
} // for (zord = offset[2]; zord < offset[2] + size[2]; zord++)
}
#endif
void BufferOpenCLDev::MPIRecv(int src, MPI_Comm comm, const IntArray &offset,
const IntArray &size) {
// First, recv with the host pinned buffer (which also performs
// internal copying between MPI and OpenCL buffers.
pinned_buf_->Buffer::MPIRecv(src, comm, size);
// Then use CLEnqueueWriteBuffer to copy into the device memory
Copyin(*pinned_buf_, offset, size);
}
void BufferOpenCLDev::MPISend(int dst, MPI_Comm comm, const IntArray &offset,
const IntArray &size) {
Copyout(*pinned_buf_, offset, size);
pinned_buf_->Buffer::MPISend(dst, comm, size);
}
void BufferOpenCLDev::GetChunk_CL(
const IntArray &size, cl_mem *ret_p_mem,
size_t *ret_pitch
)
{
*ret_p_mem = 0;
// FIXME
// FIXME
// NEEDCHECK
*ret_pitch = size[0] * elm_size_;
if (size.accumulate(num_dims_) >0) {
cl_int status;
CLbaseinfo *clinfo_use = stream_clinfo();
if (!clinfo_use)
clinfo_use = base_clinfo();
PSAssert(clinfo_use);
cl_context buf_context_ = clinfo_use->get_context();
PSAssert(buf_context_);
*ret_p_mem = clCreateBuffer(
buf_context_, CL_MEM_READ_WRITE, GetLinearSize(size), NULL, &status);
if (status != CL_SUCCESS)
LOG_DEBUG() << "Calling clCreateBuffer failed\n";
}
}
void *BufferOpenCLDev::GetChunk(const IntArray &size) {
LOG_ERROR() << "Never use BufferOpenCLDev::GetChunk!!!\n";
PSAbort(1);
return 0;
}
void BufferOpenCLDev::DeleteChunk_CL(cl_mem p_mem) {
if (p_mem) {
clReleaseMemObject(p_mem);
}
p_mem = 0;
}
void BufferOpenCLDev::DeleteChunk(void *ptr) {
LOG_ERROR() << "Never use BufferOpenCLDev::DeleteChunk!!!\n";
PSAbort(1);
}
void BufferOpenCLDev::Allocate(int num_dims, size_t elm_size, const IntArray &size){
Delete();
if (size.accumulate(num_dims)) {
num_dims_ = num_dims;
elm_size_ = elm_size;
GetChunk_CL(size, &buf_mem, &pitch);
if (!buf_mem) {
LOG_ERROR() << "Buffer allocation failure\n";
PSAbort(1);
}
}
size_ = size;
}
void BufferOpenCLDev::Delete() {
if (buf_mem) {
DeleteChunk_CL(buf_mem);
}
buf_mem = 0;
size_.assign(0);
}
//
// BufferOpenCLDev3D
//
#if 0
#endif
} // namespace runtime
} // namespace physis