-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpu.cu
299 lines (242 loc) · 10.4 KB
/
gpu.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
/*********************************************************************************/
/* Matrix product program for a multi-core CPU and for a many-core GPU */
/* S. Vialle - November 2021 */
/*********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include "main.h"
#include "gpu.h"
/*-------------------------------------------------------------------------------*/
/* GPU symbols and global vars */
/*-------------------------------------------------------------------------------*/
// Symbols used by all kernels
__device__ T_real GPU_A[SIZE][SIZE];
__device__ T_real GPU_B[SIZE][SIZE];
__device__ T_real GPU_C[SIZE][SIZE];
// New Symbol and vars to call Cublas lib.
__device__ T_real GPU_Ctmp[SIZE][SIZE]; // New matrix buffer
T_real *AdrGPU_A = NULL; // Adresses of the symbols
T_real *AdrGPU_B = NULL;
T_real *AdrGPU_C = NULL;
T_real *AdrGPU_Ctmp = NULL;
cublasHandle_t cublasHandle; // Handle on the Cublas lib.
/*-------------------------------------------------------------------------------*/
/* Init and finalize the GPU device. */
/*-------------------------------------------------------------------------------*/
void gpuInit(void)
{
cuInit(0);
// Extract address of GPU matrix "symbols"
CHECK_CUDA_SUCCESS(cudaGetSymbolAddress((void **)&AdrGPU_A,GPU_A),"GPU_A adr extraction");
CHECK_CUDA_SUCCESS(cudaGetSymbolAddress((void **)&AdrGPU_B,GPU_B),"GPU_B adr extraction");
CHECK_CUDA_SUCCESS(cudaGetSymbolAddress((void **)&AdrGPU_C,GPU_C),"GPU_C adr extraction");
CHECK_CUDA_SUCCESS(cudaGetSymbolAddress((void **)&AdrGPU_Ctmp,GPU_Ctmp),"GPU_Ctmp adr extraction");
// Turn CPU arrays A, B and C into "pinned" memory areas
CHECK_CUDA_SUCCESS(cudaHostRegister(A,SIZE*SIZE*sizeof(T_real),
cudaHostRegisterPortable),
"Turning into pinned memory the A CPU array");
CHECK_CUDA_SUCCESS(cudaHostRegister(B,SIZE*SIZE*sizeof(T_real),
cudaHostRegisterPortable),
"Turning into pinned memory the B CPU array");
CHECK_CUDA_SUCCESS(cudaHostRegister(C,SIZE*SIZE*sizeof(T_real),
cudaHostRegisterPortable),
"Turning into pinned memory the C CPU array");
// Initialize CUBLAS lib usage
CHECK_CUBLAS_SUCCESS(cublasCreate(&cublasHandle), "Init of the CUBLAS lib handle");
}
void gpuFinalize(void)
{
// Turn "pinned" CPU arrays into std array
CHECK_CUDA_SUCCESS(cudaHostUnregister(A),
"Turning into std memory the A CPU array");
CHECK_CUDA_SUCCESS(cudaHostUnregister(B),
"Turning into std memory the B CPU array");
CHECK_CUDA_SUCCESS(cudaHostUnregister(C),
"Turning into std memory the C CPU array");
// Free CUBLAS lib usage
CHECK_CUBLAS_SUCCESS(cublasDestroy(cublasHandle), "Free the CUBLAS lib");
}
/*-------------------------------------------------------------------------------*/
/* Transfer of CPU input data into GPU symbols */
/*-------------------------------------------------------------------------------*/
void gpuSetDataOnGPU(void)
{
// Set GPU_A symbol
CHECK_CUDA_SUCCESS(cudaMemcpyToSymbol(GPU_A, A, sizeof(T_real) *SIZE*SIZE, 0, cudaMemcpyHostToDevice),
"[ERROR] Transfer A-->GPU_A");
// Set GPU_B symbol
CHECK_CUDA_SUCCESS(cudaMemcpyToSymbol(GPU_B, B, sizeof(T_real) *SIZE*SIZE, 0, cudaMemcpyHostToDevice),
"[ERROR] Transfer B-->GPU_B");
}
/*-------------------------------------------------------------------------------*/
/* Transfer of GPU results into CPU array */
/*-------------------------------------------------------------------------------*/
void gpuGetResultOnCPU(void)
{
// Get GPU_C symbol
cudaMemcpyFromSymbol(C,GPU_C,sizeof(T_real)*SIZE*SIZE,0,cudaMemcpyDeviceToHost);
}
/*-------------------------------------------------------------------------------*/
/* Transposition kernel using global memory and registers. */
/*-------------------------------------------------------------------------------*/
__global__ void TransposeKernel_v0(T_real *MT, T_real *M, int mLig, int nCol)
{
int lig = threadIdx.y + blockIdx.y*BLOCK_SIZE_XY_KT0;
int col = threadIdx.x + blockIdx.x*BLOCK_SIZE_XY_KT0;
if (lig < mLig && col < nCol)
MT[col*mLig + lig] = M[lig*nCol + col];
}
/*-------------------------------------------------------------------------------*/
/* Small matrix product on the local GPU - 1D & generic matrix size */
/*-------------------------------------------------------------------------------*/
__global__ void MatrixProductKernel_v0(void)
{
// Index computations
int col = threadIdx.y + blockIdx.y*BLOCK_SIZE_Y_K0;
int lig = threadIdx.x + blockIdx.x*BLOCK_SIZE_X_K0;
T_real res = 0.0;
// Matrix product computation
if (col < SIZE ) {
for (int i=0; i<SIZE; i++) {
res += GPU_A[lig][i] * GPU_B[i][col];
}
GPU_C[lig][col] = res;
}
}
/*-------------------------------------------------------------------------------*/
/* Small matrix product on the local GPU - 2D & generic matrix size */
/*-------------------------------------------------------------------------------*/
__global__ void MatrixProductKernel_v1(void)
{
// Index computations
int lig = threadIdx.y + blockIdx.y*BLOCK_SIZE_Y_K1;
int col = threadIdx.x + blockIdx.x*BLOCK_SIZE_X_K1;
T_real res = 0.0;
// Matrix product computation
if (col < SIZE && lig < SIZE){
for (int i = 0; i < SIZE; i++){
res += GPU_A[lig][i] * GPU_B[i][col];
}
GPU_C[lig][col] = res;
}
}
/*-------------------------------------------------------------------------------*/
/* Small matrix product on the local GPU - 2D SHARED MEMORY & fixed matrix size */
/*-------------------------------------------------------------------------------*/
__global__ void MatrixProductKernel_v2(void)
{
__shared__ T_real sh_gpu_a[BLOCK_SIZE_XY_K2][BLOCK_SIZE_XY_K2];
__shared__ T_real sh_gpu_b[BLOCK_SIZE_XY_K2][BLOCK_SIZE_XY_K2];
T_real res = 0;
int lig = threadIdx.y + blockIdx.y*BLOCK_SIZE_XY_K2;
int col = threadIdx.x + blockIdx.x*BLOCK_SIZE_XY_K2;
for (int step = 0; step < SIZE / BLOCK_SIZE_XY_K2; step++) {
int lig_inter = threadIdx.y + step * BLOCK_SIZE_XY_K2;
int col_inter = threadIdx.x + step * BLOCK_SIZE_XY_K2;
sh_gpu_a[threadIdx.y][threadIdx.x] = GPU_A[lig][col_inter];
sh_gpu_b[threadIdx.y][threadIdx.x] = GPU_B[lig_inter][col];
__syncthreads();
for (int i = 0; i < BLOCK_SIZE_XY_K2; i++) {
res += sh_gpu_a[threadIdx.y][i] * sh_gpu_b[i][threadIdx.x];
}
__syncthreads();
}
GPU_C[lig][col] = res;
}
/*-------------------------------------------------------------------------------*/
/* Small matrix product on the local GPU - 2D SHARED MEMORY & generic matrix size */
/*-------------------------------------------------------------------------------*/
__global__ void MatrixProductKernel_v3(void)
{
__shared__ T_real sh_gpu_a[BLOCK_SIZE_XY_K3][BLOCK_SIZE_XY_K3];
__shared__ T_real sh_gpu_b[BLOCK_SIZE_XY_K3][BLOCK_SIZE_XY_K3];
T_real res = 0;
int lig = threadIdx.y + blockIdx.y*BLOCK_SIZE_XY_K3;
int col = threadIdx.x + blockIdx.x*BLOCK_SIZE_XY_K3;
float step_max = (SIZE/BLOCK_SIZE_XY_K3);
for (int step = 0; step < step_max; step++) {
int lig_inter = threadIdx.y + step * BLOCK_SIZE_XY_K3;
int col_inter = threadIdx.x + step * BLOCK_SIZE_XY_K3;
if (step>(int)step_max) {
sh_gpu_a[threadIdx.y][threadIdx.x] = 0;
sh_gpu_b[threadIdx.y][threadIdx.x] = 0;
}
else {
sh_gpu_a[threadIdx.y][threadIdx.x] = GPU_A[lig][col_inter];
sh_gpu_b[threadIdx.y][threadIdx.x] = GPU_B[lig_inter][col];
}
__syncthreads();
for (int i = 0; i < BLOCK_SIZE_XY_K3; i++) {
res += sh_gpu_a[threadIdx.y][i] * sh_gpu_b[i][threadIdx.x];
}
GPU_C[lig][col] = res;
__syncthreads();
}
}
/*-------------------------------------------------------------------------------*/
/* Small matrix product on the local GPU. */
/*-------------------------------------------------------------------------------*/
void gpuProduct(gkid_t kid)
{
dim3 Dg = {0,0,0}; // Grid descriptor
dim3 Db = {0,0,0}; // Block descriptor
//T_real alpha; // When using CUBLAS
//T_real beta; // When using CUBLAS
switch(kid) {
case GK0 : // Kernel v0 - 1D kernel using only resgisters and cache with generic matrix size
// - init the grid of blocs
Db.x = BLOCK_SIZE_X_K0;
Db.y = BLOCK_SIZE_Y_K0;
Db.z = 1;
Dg.x = SIZE/BLOCK_SIZE_X_K0 + ( SIZE % BLOCK_SIZE_X_K0 ? 1 : 0 );
Dg.y = SIZE/BLOCK_SIZE_Y_K0 + ( SIZE % BLOCK_SIZE_Y_K0 ? 1 : 0 );
Dg.z = 1;
// - run the Grid of Blocs of threads
MatrixProductKernel_v0<<<Dg,Db>>>();
break;
case GK1 : // kernel v1 : 2D kernel using only registers and cache with generic matrix size
Db.x = BLOCK_SIZE_X_K1;
Db.y = BLOCK_SIZE_Y_K1;
Db.z = 1;
Dg.x = (SIZE-1)/BLOCK_SIZE_X_K1 + 1;
Dg.y = (SIZE-1)/BLOCK_SIZE_Y_K1 + 1;
Dg.z = 1;
// - run the Grid of Blocs of threads
MatrixProductKernel_v1<<<Dg,Db>>>();
break;
case GK2 : // kernel v2 : 2D kernel using the shared memories
Db.x = BLOCK_SIZE_XY_K2;
Db.y = BLOCK_SIZE_XY_K2;
Db.z = 1;
Dg.x = (SIZE-1)/BLOCK_SIZE_XY_K2 + 1;
Dg.y = (SIZE-1)/BLOCK_SIZE_XY_K2 + 1;
Dg.z = 1;
MatrixProductKernel_v2<<<Dg,Db>>>();
break;
case GK3 : // kernel v3 : 2D kernel using the shared memories with generic matrix size
Db.x = BLOCK_SIZE_XY_K3;
Db.y = BLOCK_SIZE_XY_K3;
Db.z = 1;
Dg.x = (SIZE-1)/BLOCK_SIZE_XY_K3 + 1;
Dg.y = (SIZE-1)/BLOCK_SIZE_XY_K3 + 1;
Dg.z = 1;
MatrixProductKernel_v3<<<Dg,Db>>>();
break;
case GK4 : // calling cublas gemm & user-defined transpose kernel
break;
case GK5 : // Calling cublas gemm & cublas geam kernels
break;
case GK6 : // Calling cublas gemm, using matrix math properties
break;
case GK7 : // Calling cublas gemmEx, using Tensor cores
break;
case GK8 : // Free
break;
default :
fprintf(stderr,"Unknown GPU kernel!");
exit(EXIT_FAILURE);
} // End of switch
}