-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemoryTests.cu
More file actions
498 lines (401 loc) · 14.3 KB
/
memoryTests.cu
File metadata and controls
498 lines (401 loc) · 14.3 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
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
#include "arithmeticTests.h"
#include <stdio.h>
//------------------ L1 CACHE KERNELS -----------
template <typename T>
__global__
void l1MemKernel1(int n, int iterateNum, const T *x, T *y) {
int thread = blockIdx.x*blockDim.x + threadIdx.x;
__shared__ volatile int index[256];
index[threadIdx.x] = thread;
T tot = 0;
T var;
for (int i = 0; i < iterateNum; i++) {
const T *loc = &x[index[threadIdx.x]];
var = __ldg(loc+n);
var += var;
tot += var;
}
y[thread] = tot;
return;
}
template <typename T>
__global__
void l1MemKernel2(int n, int iterateNum, const T *x, T *y) {
int thread = blockIdx.x*blockDim.x + threadIdx.x;
__shared__ volatile int index[256];
index[threadIdx.x] = thread;
T tot = 0;
T var;
for (int i = 0; i < iterateNum; i++) {
const T *loc = &x[index[threadIdx.x]];
var = __ldg(loc+n);
var += __ldg(loc);
tot += var;
}
y[thread] = tot;
return;
}
//------------------ L2 CACHE KERNEL -----------
template <typename T>
__global__
void l2MemReadKernel1(int n, int iterateNum, volatile T *x) {
int thread = blockIdx.x*blockDim.x + threadIdx.x;
T val = 0;
for (int i = 0; i < iterateNum; i++) {
val += x[thread];
val += x[thread];
val += val;
}
x[thread] = val;
return;
}
template <typename T>
__global__
void l2MemReadKernel2(int n, int iterateNum, volatile T *x) {
int thread = blockIdx.x*blockDim.x + threadIdx.x;
T val = 0;
for (int i = 0; i < iterateNum; i++) {
val += x[thread];
val += x[thread];
val += x[thread];
}
x[thread] = val;
return;
}
//------------------ GLOBAL MEMORY KERNELS -----------
__host__ __device__ unsigned reverse(unsigned nbits, unsigned val) {
//From the bit twiddling hacks page http://graphics.stanford.edu/~seander/bithacks.html
//Assuming unsigned is a 32-bit type, the most straightforward way is to flip the 32-bit value,
// than shift the flipped value by 32-nbits
val = (((val & 0xaaaaaaaa) >> 1) | ((val & 0x55555555) << 1));
val = (((val & 0xcccccccc) >> 2) | ((val & 0x33333333) << 2));
val = (((val & 0xf0f0f0f0) >> 4) | ((val & 0x0f0f0f0f) << 4));
val = (((val & 0xff00ff00) >> 8) | ((val & 0x00ff00ff) << 8));
val = ((val >> 16) | (val << 16));
return val >> (32-nbits);
}
template <typename T>
__global__
void globalMemKernel1(unsigned log2nblocksX, unsigned memblockelems, T *C, T *A, T x) {
unsigned memblockIdx = blockIdx.x ^ reverse(log2nblocksX, blockIdx.y);
unsigned idx = memblockIdx * memblockelems + threadIdx.x;
float sum = 0.0f;
for (unsigned i = 0; i < memblockelems/blockDim.x; i++) {
sum = A[idx] * x + sum;
idx += blockDim.x;
}
C[blockIdx.x*blockDim.x+threadIdx.x] = sum;
}
template <typename T>
__global__
void globalMemKernel2(unsigned log2nblocksX, unsigned memblockelems, T *C, T *A, T *B) {
unsigned memblockIdx = blockIdx.x ^ reverse(log2nblocksX, blockIdx.y);
unsigned idx = memblockIdx * memblockelems + threadIdx.x;
float sum = 0.0f;
for (unsigned i = 0; i < memblockelems/blockDim.x; i++) {
sum = A[idx] * B[idx] + sum;
idx += blockDim.x;
}
C[blockIdx.x*blockDim.x+threadIdx.x] = sum;
}
//------------------ SHARED MEMORY KERNEL -----------
template <typename T>
__global__
void sharedMemReadKernel1(int n, int iterateNum, volatile T *x) {
//extern __shared__ volatile T s[]; //volatile to prevent optimization
__shared__ volatile T s[1024];
int thread = blockIdx.x*blockDim.x + threadIdx.x;
s[threadIdx.x] = x[thread];
volatile T val = 0;
for (int i = 0; i < iterateNum; i++) {
val += s[threadIdx.x];
val += s[threadIdx.x];
val += s[threadIdx.x];
val += val;
}
x[thread] = val;
return;
}
template <typename T>
__global__
void sharedMemReadKernel2(int n, int iterateNum, volatile T *x) {
//extern __shared__ volatile T s[];
__shared__ volatile T s[1024];
int thread = blockIdx.x*blockDim.x + threadIdx.x;
s[threadIdx.x] = x[thread];
volatile T val = 0;
for (int i = 0; i < iterateNum; i++) {
val += s[threadIdx.x];
val += s[threadIdx.x];
val += s[threadIdx.x];
val += s[threadIdx.x];
}
x[thread] = val;
return;
}
//------------------ INITIALIZE ARRAY FOR KERNEL -----------
template <typename T>
__global__
void createData(int n, T *x) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
// T a = 1.0;
if (i < n) {
//x[i] = i;
x[i] = 0.0f;
}
}
template <typename T>
class MemoryTestBase {
public:
T *d_x;
int n;
int iterNum;
int numBlocks;
int blockSize;
int numBlockScale;
int opsPerIteration; //number of operations in one iteration. Not including loop calculations
MemoryTestBase(int blockSize, int iterNum)
: iterNum(iterNum), blockSize(blockSize), numBlockScale(360)
{ opsPerIteration = 0;}
MemoryTestBase(int blockSize, int iterNum, int numBlockScale)
: iterNum(iterNum), blockSize(blockSize), numBlockScale(numBlockScale)
{ opsPerIteration = 0;}
~MemoryTestBase() {
CUDA_ERROR( cudaFree(d_x) );
}
void kernelSetup(cudaDeviceProp deviceProp) {
numBlocks = deviceProp.multiProcessorCount * numBlockScale;
n = numBlocks * blockSize;
printf("n: %d\n", n);
CUDA_ERROR( cudaMalloc(&d_x, 2*n*sizeof(T)) );
createData<T><<<numBlocks, blockSize>>>(2*n, d_x);
}
//get the number of threads launched in the kernel. Must be
//called after kernelSetup() or the neccisary fields may not be initialized
int getNumThreads() {
return numBlocks * blockSize;
}
//return the number of operations that are executed in the kernel's loop
//for the specified number of operations.
//Ex: 6 operations per iteration * 1000000 iterations = 6000000 operations
int getOpsPerThread() {
return opsPerIteration * iterNum;
}
void runKernel();
void CUDA_ERROR(cudaError_t e) {
if (e != cudaSuccess) {
printf("cuda error in test class: \"%s\"\n", cudaGetErrorString(e));
}
}
};
//----------------------------------------------------------------
//---------------------- MEMORY TEST IMPLEMENTATIONS -------------
//----------------------------------------------------------------
//---------------------- L1 CACHE TESTING CLASSES -------------
template <typename T>
class L1MemTest1 : public MemoryTestBase<T> {
public:
T *d_y;
L1MemTest1(int blockSize, int iterNum)
: MemoryTestBase<T>(blockSize, iterNum)
{this->opsPerIteration = 1;}
L1MemTest1(int blockSize, int iterNum, int numBlockScale)
: MemoryTestBase<T>(blockSize, iterNum, numBlockScale)
{this->opsPerIteration = 1;}
//should call base destructor after executing this destructor
~L1MemTest1() {
this->CUDA_ERROR(cudaFree(d_y));
}
void kernelSetup(cudaDeviceProp deviceProp) {
MemoryTestBase<T>::kernelSetup(deviceProp);
this->CUDA_ERROR( cudaMalloc(&d_y, this->n*sizeof(T)) );
createData<T><<<this->numBlocks, this->blockSize>>>(this->n, d_y);
printf("numblocks %d, blockSize %d, n %d, iterNum %d\n",this->numBlocks, this->blockSize, this->n, this->iterNum);
}
void runKernel() {
l1MemKernel1<T><<<this->numBlocks, this->blockSize>>>(this->n, this->iterNum, this->d_x, d_y);
}
};
template <typename T>
class L1MemTest2 : public MemoryTestBase<T> {
public:
T *d_y;
L1MemTest2(int blockSize, int iterNum)
: MemoryTestBase<T>(blockSize, iterNum)
{this->opsPerIteration = 2;}
L1MemTest2(int blockSize, int iterNum, int numBlockScale)
: MemoryTestBase<T>(blockSize, iterNum, numBlockScale)
{this->opsPerIteration = 2;}
//should call base destructor after executing this destructor
~L1MemTest2() {
this->CUDA_ERROR(cudaFree(d_y));
}
void kernelSetup(cudaDeviceProp deviceProp) {
MemoryTestBase<T>::kernelSetup(deviceProp);
this->CUDA_ERROR( cudaMalloc(&d_y, this->n*sizeof(T)) );
createData<T><<<this->numBlocks, this->blockSize>>>(this->n, d_y);
printf("numblocks %d, blockSize %d, n %d, iterNum %d\n",this->numBlocks, this->blockSize, this->n, this->iterNum);
}
void runKernel() {
l1MemKernel2<T><<<this->numBlocks, this->blockSize>>>(this->n, this->iterNum, this->d_x, d_y);
}
};
//---------------------- L2 CACHE TESTING CLASSES -------------
template <typename T>
class L2MemReadTest1 : public MemoryTestBase<T> {
public:
L2MemReadTest1(int blockSize, int iterNum)
: MemoryTestBase<T>(blockSize, iterNum)
{this->opsPerIteration = 2;}
L2MemReadTest1(int blockSize, int iterNum, int numBlockScale)
: MemoryTestBase<T>(blockSize, iterNum, numBlockScale)
{this->opsPerIteration = 2;}
void runKernel() {
l2MemReadKernel1<T><<<this->numBlocks, this->blockSize>>>(this->n, this->iterNum, this->d_x);
}
};
template <typename T>
class L2MemReadTest2 : public MemoryTestBase<T> {
public:
L2MemReadTest2(int blockSize, int iterNum)
: MemoryTestBase<T>(blockSize, iterNum)
{this->opsPerIteration = 3;}
L2MemReadTest2(int blockSize, int iterNum, int numBlockScale)
: MemoryTestBase<T>(blockSize, iterNum, numBlockScale)
{this->opsPerIteration = 3;}
void runKernel() {
l2MemReadKernel2<T><<<this->numBlocks, this->blockSize>>>(this->n, this->iterNum, this->d_x);
}
};
//---------------------- GLOBAL MEMORY TESTING CLASSES -------------
template <typename T>
class GlobalMemTest1 : public MemoryTestBase<T> {
public:
unsigned nblocksX;
unsigned memblocksize;
unsigned log2nblocksX;
unsigned memblockelems;
T *A;
T *B;
T *C;
T x = 0.0f; //constant passed into control kernel
GlobalMemTest1(int blockSize, int iterNum)
: MemoryTestBase<T>(blockSize, iterNum)
{this->opsPerIteration = 2;}
GlobalMemTest1(int blockSize, int iterNum, int numBlockScale)
: MemoryTestBase<T>(blockSize, iterNum, numBlockScale)
{this->opsPerIteration = 2;}
void kernelSetup(cudaDeviceProp deviceProp) {
MemoryTestBase<T>::kernelSetup(deviceProp);
size_t memcapacity = deviceProp.totalGlobalMem;
memblockelems = this->blockSize*this->iterNum;
memblocksize = memblockelems * sizeof(T);
nblocksX = memcapacity / (4*memblocksize);
//Round number of blocks down to next power of two
log2nblocksX = 1;
while ( (nblocksX >> (log2nblocksX+1)) > 0 ) {
log2nblocksX++;
}
nblocksX = 1 << log2nblocksX;
size_t in_buffer_size = nblocksX*memblocksize;
this->CUDA_ERROR(cudaMalloc(&A, in_buffer_size));
this->CUDA_ERROR(cudaMemset(A, 0, in_buffer_size));
this->CUDA_ERROR(cudaMalloc(&B, in_buffer_size));
this->CUDA_ERROR(cudaMemset(B, 0, in_buffer_size));
this->CUDA_ERROR(cudaMalloc(&C, this->blockSize * nblocksX * sizeof(T)));
this->CUDA_ERROR(cudaMemset(C, 0, this->blockSize * nblocksX * sizeof(T)));
printf("elems %d\n", memblockelems);
}
void runKernel() {
//printf("blockSize %d, log2nblocksX %d, memblocksize %d, C %p, A %p, x %f\n", this->blockSize,log2nblocksX, memblocksize, C, A, x);
globalMemKernel1<T><<<dim3(nblocksX, nblocksX), this->blockSize>>>(log2nblocksX, memblockelems, C, A, x);
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess)
printf(" Starting kernel failed. Error: %s\n", cudaGetErrorString(err));
}
};
template <typename T>
class GlobalMemTest2 : public MemoryTestBase<T> {
public:
unsigned memblocksize;
unsigned log2nblocksX;
unsigned memblockelems;
unsigned nblocksX;
T *A;
T *B;
T *C;
GlobalMemTest2(int blockSize, int iterNum)
: MemoryTestBase<T>(blockSize, iterNum)
{this->opsPerIteration = 4;}
GlobalMemTest2(int blockSize, int iterNum, int numBlockScale)
: MemoryTestBase<T>(blockSize, iterNum, numBlockScale)
{this->opsPerIteration = 4;}
void kernelSetup(cudaDeviceProp deviceProp) {
MemoryTestBase<T>::kernelSetup(deviceProp);
size_t memcapacity = deviceProp.totalGlobalMem;
memblockelems = this->blockSize*this->iterNum;
memblocksize = memblockelems * sizeof(T);
nblocksX = memcapacity / (4*memblocksize);
//Round number of blocks down to next power of two
log2nblocksX = 1;
while ( (nblocksX >> (log2nblocksX+1)) > 0 ) {
log2nblocksX++;
}
nblocksX = 1 << log2nblocksX;
size_t in_buffer_size = nblocksX*memblocksize;
this->CUDA_ERROR(cudaMalloc(&A, in_buffer_size));
this->CUDA_ERROR(cudaMemset(A, 0, in_buffer_size));
this->CUDA_ERROR(cudaMalloc(&B, in_buffer_size));
this->CUDA_ERROR(cudaMemset(B, 0, in_buffer_size));
this->CUDA_ERROR(cudaMalloc(&C, this->blockSize * nblocksX * sizeof(T)));
this->CUDA_ERROR(cudaMemset(C, 0, this->blockSize * nblocksX * sizeof(T)));
printf("elems %d\n", memblockelems);
}
void runKernel() {
globalMemKernel2<T><<<dim3(nblocksX, nblocksX), this->blockSize>>>(log2nblocksX, memblockelems, C, A, B);
}
};
//---------------------- SHARED MEMORY TESTING CLASSES -------------
template <typename T>
class SharedMemReadTest1 : public MemoryTestBase<T> {
public:
unsigned int sharedMemRequest;
SharedMemReadTest1(int blockSize, int iterNum)
: MemoryTestBase<T>(blockSize, iterNum)
{this->opsPerIteration = 3;}
SharedMemReadTest1(int blockSize, int iterNum, int numBlockScale)
: MemoryTestBase<T>(blockSize, iterNum, numBlockScale)
{this->opsPerIteration = 3;}
//in addition to normal setup, figure out how much shared memory to request
void kernelSetup(cudaDeviceProp deviceProp) {
MemoryTestBase<T>::kernelSetup(deviceProp);
sharedMemRequest = (unsigned int) (this->n * sizeof(T));
}
void runKernel() {
sharedMemReadKernel1<T><<<this->numBlocks, this->blockSize>>>(this->n, this->iterNum, this->d_x);
}
};
template <typename T>
class SharedMemReadTest2 : public MemoryTestBase<T> {
public:
unsigned int sharedMemRequest;
SharedMemReadTest2(int blockSize, int iterNum)
: MemoryTestBase<T>(blockSize, iterNum)
{this->opsPerIteration = 4;}
SharedMemReadTest2(int blockSize, int iterNum, int numBlockScale)
: MemoryTestBase<T>(blockSize, iterNum, numBlockScale)
{this->opsPerIteration = 4;}
//in addition to normal setup, figure out how much shared memory to request
void kernelSetup(cudaDeviceProp deviceProp) {
MemoryTestBase<T>::kernelSetup(deviceProp);
sharedMemRequest = (unsigned int) (this->n * sizeof(T));
//printf(" numBlocks %d, n %d \n", this->numBlocks, this->n);
//printf(" sharedMemRequest %d\n", sharedMemRequest);
}
void runKernel() {
sharedMemReadKernel2<T><<<this->numBlocks, this->blockSize>>>(this->n, this->iterNum, this->d_x);
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess)
printf(" Error: %s\n", cudaGetErrorString(err));
}
};