-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.h
586 lines (455 loc) · 13.1 KB
/
Common.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// (C) 2012 John Romein/ASTRON
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#if !defined COMMON_H
#define COMMON_H
#include <cstdio>
#include <iostream>
#if defined __SSE__ && !defined __CUDA__ && !defined __OPENCL__
#include <xmmintrin.h>
#endif
#if defined __CUDA__
#include <cuda_runtime.h>
#endif
#if defined __OPENCL__
#define __CL_ENABLE_EXCEPTIONS
#include <CL/cl.hpp>
#endif
#if defined _OPENMP
#include <omp.h>
#else
#define omp_get_num_threads() 1
#define omp_get_thread_num() 0
#endif
#if defined __linux__
#include <sched.h>
#include <sys/time.h>
#endif
#if defined _WIN32 || defined __WIN32__ || defined _WIN64
#include <windows.h>
#endif
#if defined __CUDA__
#elif 0 && defined __OPENCL__
typedef cl_double2 double2;
typedef cl_float2 float2;
typedef union { float s[3]; struct { float x, y, z; }; } float3;
typedef union { double s[3]; struct { double x, y, z; }; } double3;
typedef cl_float4 float4;
typedef cl_double4 double4;
typedef cl_uint2 uint2;
inline float2 make_float2(float x, float y)
{
float2 f2;
f2.x = x;
f2.y = y;
return f2;
}
inline float3 make_float3(float x, float y, float z)
{
float3 f3;
f3.x = x;
f3.y = y;
f3.z = z;
return f3;
}
inline double2 make_double2(double x, double y)
{
double2 d2;
d2.x = x;
d2.y = y;
return d2;
}
inline double3 make_double3(double x, double y, double z)
{
double3 d3;
d3.x = x;
d3.y = y;
d3.z = z;
return d3;
}
//#define make_float2(X,Y) float2((X),(Y))
//#define make_float3(X,Y,Z) (float3) { (X),(Y),(Z) }
//#define make_double2(X,Y) (double2) { (X),(Y) }
#define make_uint2(X,Y) (uint2) { (X),(Y) }
#else
typedef struct { float x, y; } float2;
typedef struct { float x, y, z; } float3;
typedef struct { double x, y; } double2;
typedef struct { double x, y, z; } double3;
typedef struct { unsigned x, y; } uint2;
inline float2 make_float2(float x, float y) { float2 f = { x, y }; return f; }
inline float3 make_float3(float x, float y, float z) { float3 f = { x, y, z}; return f; }
inline double2 make_double2(double x, double y) { double2 f = { x, y }; return f; }
inline double3 make_double3(double x, double y, double z) { double3 f = { x, y, z }; return f; }
inline uint2 make_uint2(unsigned x, unsigned y) { uint2 f = { x, y }; return f; }
#endif
double getTime()
{
static double firstTime = 0.0;
#if defined __linux__
struct timeval tv;
if (gettimeofday(&tv, 0) < 0) {
perror("gettimeofday");
exit(1);
}
double now = tv.tv_sec + tv.tv_usec / 1e6;
#elif defined _WIN32 || defined __WIN32__ || defined _WIN64
static LARGE_INTEGER freq;
if (firstTime == 0 && !QueryPerformanceFrequency(&freq))
std::cerr << "No high-resolution timer available" << std::endl;
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
double now = (double) time.QuadPart / (double) freq.QuadPart;
#endif
if (firstTime == 0.0)
firstTime = now;
return now - firstTime;
}
inline float2 operator + (float2 a, float2 b)
{
return make_float2(a.x + b.x, a.y + b.y);
}
inline float2 operator * (float a, float2 b)
{
return make_float2(a * b.x, a * b.y);
}
inline float2 operator * (float2 a, float2 b)
{
return make_float2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
inline float2 operator += (float2 &a, float2 b)
{
return make_float2(a.x += b.x, a.y += b.y);
}
inline double2 operator += (double2 &a, float2 b)
{
return make_double2(a.x += b.x, a.y += b.y);
}
inline std::ostream &operator << (std::ostream &str, float2 z)
{
return str << '(' << z.x << ", " << z.y << ')';
}
inline std::ostream &operator << (std::ostream &str, double2 z)
{
return str << '(' << z.x << ", " << z.y << ')';
}
#if defined __SSE__ && !defined __CUDA__ && !defined __OPENCL__
inline std::ostream &operator << (std::ostream &str, __m128 v)
{
union {
__m128 m;
float a[4];
} u;
u.m = v;
return str << '[' << u.a[0] << ", " << u.a[1] << ", " << u.a[2] << ", " << u.a[3] << ']';
}
#endif
#if defined __CUDA__
inline void checkCudaCallWithLineNumber(cudaError_t result, unsigned lineNumber)
{
if (result != cudaSuccess) {
#pragma omp critical (cout)
std::cerr << "cuda error (line #" << lineNumber << "): " << cudaGetErrorString(result) << std::endl;
exit(1);
}
}
#define checkCudaCall(X) checkCudaCallWithLineNumber((X), __LINE__)
#endif
#if defined __linux__
inline void set_affinity(unsigned device)
{
#if 0
static const char mapping[1][12] = {
0, 1, 2, 3, 8, 9, 10, 11,
};
#else
static const char mapping[8][12] = {
0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17,
0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17,
0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17,
0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17,
6, 7, 8, 9, 10, 11, 18, 19, 20, 21, 22, 23,
6, 7, 8, 9, 10, 11, 18, 19, 20, 21, 22, 23,
6, 7, 8, 9, 10, 11, 18, 19, 20, 21, 22, 23,
6, 7, 8, 9, 10, 11, 18, 19, 20, 21, 22, 23,
};
#endif
cpu_set_t set;
CPU_ZERO(&set);
for (unsigned coreIndex = 0; coreIndex < 12; coreIndex ++)
CPU_SET(mapping[device][coreIndex], &set);
if (sched_setaffinity(0, sizeof set, &set) < 0)
perror("sched_setaffinity");
}
#endif
#if defined __CUDA__
inline void printDeviceProperties()
{
int deviceCount;
cudaGetDeviceCount(&deviceCount);
std::cout << "deviceCount = " << deviceCount << std::endl;
#if 1
for (int device = 0; device < deviceCount; device ++) {
cudaDeviceProp p;
cudaGetDeviceProperties(&p, device);
std::cout << "device " << device << std::endl
<< " name: " << p.name << std::endl
<< " global mem: " << p.totalGlobalMem << std::endl
<< " sharedMemPerBlock: " << p.sharedMemPerBlock << std::endl
<< " regsPerBlock: " << p.regsPerBlock << std::endl
<< " warpSize: " << p.warpSize << std::endl
<< " memPitch: " << p.memPitch << std::endl
<< " maxThreadsPerBlock: " << p.maxThreadsPerBlock << std::endl
<< " maxThreadsPerMultiProcessor: " << p.maxThreadsPerMultiProcessor << std::endl
<< " maxBlockSize: (" << p.maxThreadsDim[0] << ", "<< p.maxThreadsDim[1] << ", "<< p.maxThreadsDim[2] << ")" << std::endl
<< " maxGridSize: (" << p.maxGridSize[0] << ", "<< p.maxGridSize[1] << ", "<< p.maxGridSize[2] << ")" << std::endl
<< " const mem: " << p.totalConstMem << std::endl
<< " version: " << p.major << "." << p.minor << std::endl
<< " clock rate: " << p.clockRate << std::endl
<< " tex alignment: " << p.textureAlignment << std::endl
<< " multiprocessors: " << p.multiProcessorCount << std::endl;
}
#endif
}
class Stream
{
public:
Stream()
{
checkCudaCall(cudaStreamCreate(&stream));
}
~Stream()
{
checkCudaCall(cudaStreamDestroy(stream));
}
void synchronize()
{
checkCudaCall(cudaStreamSynchronize(stream));
}
operator cudaStream_t & ()
{
return stream;
}
private:
cudaStream_t stream;
};
class Event
{
public:
Event()
{
checkCudaCall(cudaEventCreate(&event));
}
~Event()
{
checkCudaCall(cudaEventDestroy(event));
}
void record()
{
checkCudaCall(cudaEventRecord(event));
}
void record(Stream &stream)
{
checkCudaCall(cudaEventRecord(event, stream));
}
void synchronize()
{
checkCudaCall(cudaEventSynchronize(event));
}
float elapsedTime(Event &start)
{
float time;
checkCudaCall(cudaEventElapsedTime(&time, start.event, this->event));
return time;
}
operator cudaEvent_t & ()
{
return event;
}
private:
cudaEvent_t event;
};
template <typename T> class SharedObject
{
public:
SharedObject(int hostAllocFlags = 0)
{
checkCudaCall(cudaMalloc(reinterpret_cast<void **>(&devPtr), sizeof(T)));
checkCudaCall(cudaHostAlloc(reinterpret_cast<void **>(&hostPtr), sizeof(T), hostAllocFlags));
}
~SharedObject()
{
checkCudaCall(cudaFree(devPtr));
checkCudaCall(cudaFreeHost(hostPtr));
}
void copyHostToDevice()
{
checkCudaCall(cudaMemcpy(devPtr, hostPtr, sizeof(T), cudaMemcpyHostToDevice));
}
void copyHostToDevice(Stream &stream)
{
checkCudaCall(cudaMemcpyAsync(devPtr, hostPtr, sizeof(T), cudaMemcpyHostToDevice, stream));
}
void copyDeviceToHost()
{
checkCudaCall(cudaMemcpy(hostPtr, devPtr, sizeof(T), cudaMemcpyDeviceToHost));
}
void copyDeviceToHost(Stream &stream)
{
checkCudaCall(cudaMemcpyAsync(hostPtr, devPtr, sizeof(T), cudaMemcpyDeviceToHost, stream));
}
T *hostPtr, *devPtr;
};
template <typename T> class WriteCombiningSharedObject : public SharedObject<T>
{
public:
WriteCombiningSharedObject()
:
SharedObject<T>(cudaHostAllocWriteCombined)
{
}
};
template <typename T> class MappedObject
{
public:
MappedObject(int hostAllocFlags = 0)
{
checkCudaCall(cudaHostAlloc(reinterpret_cast<void **>(&hostPtr), sizeof(T), hostAllocFlags | cudaHostAllocMapped));
checkCudaCall(cudaHostGetDevicePointer(reinterpret_cast<void **>(&devPtr), hostPtr, 0));
}
~MappedObject()
{
checkCudaCall(cudaFreeHost(hostPtr));
}
void copyHostToDevice()
{
}
void copyHostToDevice(Stream &stream)
{
}
void copyDeviceToHost()
{
}
void copyDeviceToHost(Stream &stream)
{
checkCudaCall(cudaMemcpyAsync(hostPtr, devPtr, sizeof(T), cudaMemcpyDeviceToHost, stream));
}
T *hostPtr, *devPtr;
};
#endif
#if defined __OPENCL__
template <typename T> class SharedObject
{
public:
SharedObject(cl::Context &context, int flags)
:
hostPtr(static_cast<T *>(malloc(sizeof(T)))),
devPtr(context, flags | CL_MEM_USE_HOST_PTR, sizeof(T), hostPtr)
{
}
~SharedObject()
{
free(hostPtr);
}
void copyHostToDevice(cl::CommandQueue &queue)
{
queue.enqueueWriteBuffer(devPtr, CL_FALSE, 0, sizeof *hostPtr, *hostPtr, 0, 0);
}
void copyDeviceToHost(cl::CommandQueue &queue)
{
queue.enqueueReadBuffer(devPtr, CL_FALSE, 0, sizeof *hostPtr, *hostPtr);
}
T *hostPtr;
cl::Buffer devPtr;
};
template <typename T> class MappedObject
{
public:
MappedObject(cl::Context &context, cl::CommandQueue &queue, bool hostToGPU)
:
pinnedPtr(context, (hostToGPU ? CL_MEM_WRITE_ONLY : CL_MEM_READ_ONLY) | CL_MEM_ALLOC_HOST_PTR, sizeof(T)),
devPtr(context, (hostToGPU ? CL_MEM_READ_ONLY : CL_MEM_WRITE_ONLY), sizeof(T)),
queue(queue)
{
hostPtr = (T *) queue.enqueueMapBuffer(pinnedPtr, CL_TRUE, hostToGPU ? CL_MAP_WRITE : CL_MAP_READ, 0, sizeof(T));
}
~MappedObject()
{
queue.enqueueUnmapMemObject(pinnedPtr, hostPtr);
}
void copyHostToDevice(cl::CommandQueue &queue)
{
queue.enqueueWriteBuffer(devPtr, CL_FALSE, 0, sizeof *hostPtr, *hostPtr, 0, 0);
}
void copyDeviceToHost(cl::CommandQueue &queue)
{
queue.enqueueReadBuffer(devPtr, CL_FALSE, 0, sizeof *hostPtr, *hostPtr);
}
T *hostPtr;
cl::Buffer pinnedPtr, devPtr;
cl::CommandQueue &queue;
};
#if 1
// real mapping, only works on AMD
template <typename T> class MappedAMDObject
{
public:
MappedAMDObject(cl::Context &context, cl::CommandQueue &queue, bool hostToGPU)
:
devPtr(context, (!hostToGPU ? CL_MEM_READ_ONLY : CL_MEM_WRITE_ONLY) | CL_MEM_ALLOC_HOST_PTR, sizeof(T)),
queue(queue)
{
hostPtr = (T *) queue.enqueueMapBuffer(devPtr, CL_TRUE, hostToGPU ? CL_MAP_WRITE : CL_MAP_READ, 0, sizeof(T));
}
~MappedAMDObject()
{
queue.enqueueUnmapMemObject(devPtr, hostPtr);
}
void copyHostToDevice(cl::CommandQueue &queue)
{
}
void copyDeviceToHost(cl::CommandQueue &queue)
{
}
T *hostPtr;
cl::Buffer devPtr;
cl::CommandQueue &queue;
};
#else
// works only on small buffers
//
#if !defined CL_MEM_USE_PERSISTENT_MEM_AMD
#define CL_MEM_USE_PERSISTENT_MEM_AMD (1 << 6)
#endif
template <typename T> class MappedObject
{
public:
MappedObject(cl::Context &context, cl::CommandQueue &queue, bool hostToGPU)
:
devPtr(context, (!hostToGPU ? CL_MEM_READ_ONLY : CL_MEM_WRITE_ONLY) | CL_MEM_USE_PERSISTENT_MEM_AMD, sizeof(T)),
queue(queue)
{
}
void map(bool hostToGPU)
{
hostPtr = (T *) queue.enqueueMapBuffer(devPtr, CL_TRUE, hostToGPU ? CL_MAP_WRITE : CL_MAP_READ, 0, sizeof(T));
}
void unmap()
{
queue.enqueueUnmapMemObject(devPtr, hostPtr);
}
T *hostPtr;
cl::Buffer devPtr;
cl::CommandQueue &queue;
};
#endif
#endif
#endif