-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15_矩阵转置.cu
349 lines (286 loc) · 9.33 KB
/
15_矩阵转置.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
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
#include "common.h"
#include <cuda_runtime.h>
#include <stdio.h>
/*
* Various memory access pattern optimizations applied to a matrix transpose
* kernel.
*/
#define BDIMX 16
#define BDIMY 16
void initialData(float *in, const int size)
{
for (int i = 0; i < size; i++)
{
in[i] = (float)( rand() & 0xFF ) / 10.0f; //100.0f;
}
return;
}
void printData(float *in, const int size)
{
for (int i = 0; i < size; i++)
{
printf("%dth element: %f\n", i, in[i]);
}
return;
}
void checkResult(float *hostRef, float *gpuRef, const int size, int showme)
{
double epsilon = 1.0E-8;
bool match = 1;
for (int i = 0; i < size; i++)
{
if (abs(hostRef[i] - gpuRef[i]) > epsilon)
{
match = 0;
printf("different on %dth element: host %f gpu %f\n", i, hostRef[i],
gpuRef[i]);
break;
}
if (showme && i > size / 2 && i < size / 2 + 5)
{
// printf("%dth element: host %f gpu %f\n",i,hostRef[i],gpuRef[i]);
}
}
if (!match) printf("Arrays do not match.\n\n");
}
void transposeHost(float *out, float *in, const int nx, const int ny)
{
for( int iy = 0; iy < ny; ++iy)
{
for( int ix = 0; ix < nx; ++ix)
{
out[ix * ny + iy] = in[iy * nx + ix];
}
}
}
__global__ void warmup(float *out, float *in, const int nx, const int ny)
{
unsigned int ix = blockDim.x * blockIdx.x + threadIdx.x;
unsigned int iy = blockDim.y * blockIdx.y + threadIdx.y;
if (ix < nx && iy < ny)
{
out[iy * nx + ix] = in[iy * nx + ix];
}
}
// case 0 copy kernel: access data in rows
__global__ void copyRow(float *out, float *in, const int nx, const int ny)
{
unsigned int ix = blockDim.x * blockIdx.x + threadIdx.x;
unsigned int iy = blockDim.y * blockIdx.y + threadIdx.y;
if (ix < nx && iy < ny)
{
out[iy * nx + ix] = in[iy * nx + ix];
}
}
// case 1 copy kernel: access data in columns
__global__ void copyCol(float *out, float *in, const int nx, const int ny)
{
unsigned int ix = blockDim.x * blockIdx.x + threadIdx.x;
unsigned int iy = blockDim.y * blockIdx.y + threadIdx.y;
if (ix < nx && iy < ny)
{
out[ix * ny + iy] = in[ix * ny + iy];
}
}
// case 2 transpose kernel: read in rows and write in columns
__global__ void transposeNaiveRow(float *out, float *in, const int nx,
const int ny)
{
unsigned int ix = blockDim.x * blockIdx.x + threadIdx.x;
unsigned int iy = blockDim.y * blockIdx.y + threadIdx.y;
if (ix < nx && iy < ny)
{
out[ix * ny + iy] = in[iy * nx + ix];
}
}
// case 3 transpose kernel: read in columns and write in rows
__global__ void transposeNaiveCol(float *out, float *in, const int nx,
const int ny)
{
unsigned int ix = blockDim.x * blockIdx.x + threadIdx.x;
unsigned int iy = blockDim.y * blockIdx.y + threadIdx.y;
if (ix < nx && iy < ny)
{
out[iy * nx + ix] = in[ix * ny + iy];
}
}
// case 4 transpose kernel: read in rows and write in columns + unroll 4 blocks
__global__ void transposeUnroll4Row(float *out, float *in, const int nx,
const int ny)
{
unsigned int ix = blockDim.x * blockIdx.x * 4 + threadIdx.x;
unsigned int iy = blockDim.y * blockIdx.y + threadIdx.y;
unsigned int ti = iy * nx + ix; // access in rows
unsigned int to = ix * ny + iy; // access in columns
if (ix + 3 * blockDim.x < nx && iy < ny)
{
out[to] = in[ti];
out[to + ny * blockDim.x] = in[ti + blockDim.x];
out[to + ny * 2 * blockDim.x] = in[ti + 2 * blockDim.x];
out[to + ny * 3 * blockDim.x] = in[ti + 3 * blockDim.x];
}
}
// case 5 transpose kernel: read in columns and write in rows + unroll 4 blocks
__global__ void transposeUnroll4Col(float *out, float *in, const int nx,
const int ny)
{
unsigned int ix = blockDim.x * blockIdx.x * 4 + threadIdx.x;
unsigned int iy = blockDim.y * blockIdx.y + threadIdx.y;
unsigned int ti = iy * nx + ix; // access in rows
unsigned int to = ix * ny + iy; // access in columns
if (ix + 3 * blockDim.x < nx && iy < ny)
{
out[ti] = in[to];
out[ti + blockDim.x] = in[to + blockDim.x * ny];
out[ti + 2 * blockDim.x] = in[to + 2 * blockDim.x * ny];
out[ti + 3 * blockDim.x] = in[to + 3 * blockDim.x * ny];
}
}
/*
* case 6 : transpose kernel: read in rows and write in colunms + diagonal
* coordinate transform
*/
__global__ void transposeDiagonalRow(float *out, float *in, const int nx,
const int ny)
{
unsigned int blk_y = blockIdx.x;
unsigned int blk_x = (blockIdx.x + blockIdx.y) % gridDim.x;
unsigned int ix = blockDim.x * blk_x + threadIdx.x;
unsigned int iy = blockDim.y * blk_y + threadIdx.y;
if (ix < nx && iy < ny)
{
out[ix * ny + iy] = in[iy * nx + ix];
}
}
/*
* case 7 : transpose kernel: read in columns and write in row + diagonal
* coordinate transform.
*/
__global__ void transposeDiagonalCol(float *out, float *in, const int nx,
const int ny)
{
unsigned int blk_y = blockIdx.x;
unsigned int blk_x = (blockIdx.x + blockIdx.y) % gridDim.x;
unsigned int ix = blockDim.x * blk_x + threadIdx.x;
unsigned int iy = blockDim.y * blk_y + threadIdx.y;
if (ix < nx && iy < ny)
{
out[iy * nx + ix] = in[ix * ny + iy];
}
}
// main functions
int main(int argc, char **argv)
{
// set up device
int dev = 0;
cudaDeviceProp deviceProp;
CHECK(cudaGetDeviceProperties(&deviceProp, dev));
printf("%s starting transpose at ", argv[0]);
printf("device %d: %s ", dev, deviceProp.name);
CHECK(cudaSetDevice(dev));
// set up array size 2048
int nx = 1 << 11;
int ny = 1 << 11;
// select a kernel and block size
int iKernel = 0;
int blockx = 16;
int blocky = 16;
if (argc > 1) iKernel = atoi(argv[1]);
if (argc > 2) blockx = atoi(argv[2]);
if (argc > 3) blocky = atoi(argv[3]);
if (argc > 4) nx = atoi(argv[4]);
if (argc > 5) ny = atoi(argv[5]);
printf(" with matrix nx %d ny %d with kernel %d\n", nx, ny, iKernel);
size_t nBytes = nx * ny * sizeof(float);
// execution configuration
dim3 block (blockx, blocky);
dim3 grid ((nx + block.x - 1) / block.x, (ny + block.y - 1) / block.y);
// allocate host memory
float *h_A = (float *)malloc(nBytes);
float *hostRef = (float *)malloc(nBytes);
float *gpuRef = (float *)malloc(nBytes);
// initialize host array
initialData(h_A, nx * ny);
// transpose at host side
transposeHost(hostRef, h_A, nx, ny);
// allocate device memory
float *d_A, *d_C;
CHECK(cudaMalloc((float**)&d_A, nBytes));
CHECK(cudaMalloc((float**)&d_C, nBytes));
// copy data from host to device
CHECK(cudaMemcpy(d_A, h_A, nBytes, cudaMemcpyHostToDevice));
// warmup to avoide startup overhead
double iStart = seconds();
warmup<<<grid, block>>>(d_C, d_A, nx, ny);
CHECK(cudaDeviceSynchronize());
double iElaps = seconds() - iStart;
printf("warmup elapsed %f sec\n", iElaps);
CHECK(cudaGetLastError());
// kernel pointer and descriptor
void (*kernel)(float *, float *, int, int);
char *kernelName;
// set up kernel
switch (iKernel)
{
case 0:
kernel = ©Row;
kernelName = "CopyRow ";
break;
case 1:
kernel = ©Col;
kernelName = "CopyCol ";
break;
case 2:
kernel = &transposeNaiveRow;
kernelName = "NaiveRow ";
break;
case 3:
kernel = &transposeNaiveCol;
kernelName = "NaiveCol ";
break;
case 4:
kernel = &transposeUnroll4Row;
kernelName = "Unroll4Row ";
grid.x = (nx + block.x * 4 - 1) / (block.x * 4);
break;
case 5:
kernel = &transposeUnroll4Col;
kernelName = "Unroll4Col ";
grid.x = (nx + block.x * 4 - 1) / (block.x * 4);
break;
case 6:
kernel = &transposeDiagonalRow;
kernelName = "DiagonalRow ";
break;
case 7:
kernel = &transposeDiagonalCol;
kernelName = "DiagonalCol ";
break;
}
// run kernel
iStart = seconds();
kernel<<<grid, block>>>(d_C, d_A, nx, ny);
CHECK(cudaDeviceSynchronize());
iElaps = seconds() - iStart;
// calculate effective_bandwidth
float ibnd = 2 * nx * ny * sizeof(float) / 1e9 / iElaps;
printf("%s elapsed %f sec <<< grid (%d,%d) block (%d,%d)>>> effective "
"bandwidth %f GB\n", kernelName, iElaps, grid.x, grid.y, block.x,
block.y, ibnd);
CHECK(cudaGetLastError());
// check kernel results
if (iKernel > 1)
{
CHECK(cudaMemcpy(gpuRef, d_C, nBytes, cudaMemcpyDeviceToHost));
checkResult(hostRef, gpuRef, nx * ny, 1);
}
// free host and device memory
CHECK(cudaFree(d_A));
CHECK(cudaFree(d_C));
free(h_A);
free(hostRef);
free(gpuRef);
// reset device
CHECK(cudaDeviceReset());
return EXIT_SUCCESS;
}