forked from amanrai1234/MATRIX-MUL---GPU-SYSTEMS-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_mul_cuda.cu
302 lines (189 loc) · 6.65 KB
/
matrix_mul_cuda.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
/*
Created by: Andrew Sexton (cuda version Implemented by me(Aman Rai Saxena))
Date: March 21nd, 2022
CSC258/458 - Parallel & Distributed Systems.
*/
#include <iostream>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
#include <time.h>
/* Use this macro to catch and print out runtime errors from the GPU */
/* Ex. cudaErrChk(cudaMalloc(...)) */
/* cudaErrChk(cudaDeviceSynchronize()) */
#define cudaErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char* file, int line, bool abort=true) {
if (code != cudaSuccess) {
std::cout << "GPUAssert: " << cudaGetErrorString(code) << " " << file << " line " << line << std::endl;
if (abort) { exit(code); }
}
}
/* Vectorizable version of matrix multiplication for comparison */
void seq_matmul(const float* A, const float* B, float* C, int nsize) {
float temp;
for (int i = 0; i < nsize; i++) {
for (int j = 0; j < nsize; j++) {
temp = 0.0f;
for (int k = 0; k < nsize; k++) {
temp += A[k + (i * nsize)] * B[j + (k * nsize)];
}
C[j + (i * nsize)] = temp;
}
}
}
/* Simple OMP version of matrix multiplication for comparison */
void omp_matmul(const float* A, const float* B, float* C, int nsize) {
# pragma omp parallel
{
float temp;
# pragma omp for private(temp)
for (int i = 0; i < nsize; i++) {
for (int j = 0; j < nsize; j++) {
temp = 0.0f;
for (int k = 0; k < nsize; k++) {
temp += A[k + (i * nsize)] * B[j + (k * nsize)];
}
C[j + (i * nsize)] = temp;
}
}
}
}
// Function for verifying values between two arrays
// by computing abs(x[i] - Y[i]) < EPSILON
void verify(const float* X, const float* Y, int nsize){
float EPSILON = 1E-4;
for(int i = 0; i < nsize; i++) {
for(int j = 0; j < nsize; j++) {
int idx = j + (i * nsize);
if(std::fabs(X[idx] - Y[idx]) > EPSILON) {
std::cout << std::setprecision(15) << "(" << i << ", " << j << "): " << X[idx] << " != " << Y[idx] << std::endl;
}
}
}
}
// Print a comma-separated 2D array to stdout
void print_array(const float* arr, int nsize) {
for(int i = 0; i < nsize; i++) {
for(int j = 0; j < nsize; j++) {
std::cout << arr[j + (i * nsize)];
if(j < nsize) {
std::cout << ", ";
}
}
std::cout << std::endl;
}
}
// GPU Kernel
__global__ void gpu_matmul(float* A, float* B, float* C, int nsize) {
/* Add your code here */
//
// calculate the uid block
// calculates unique thread ID in the block
//
/*===================*/
float r=blockIdx.y*blockDim.y + threadIdx.y;
float c=blockIdx.x*blockDim.x + threadIdx.x;
float result=0;
if(r<m && c<k){
for(int i=0;i<nsize;i++){
result+=A[r*nsize+i] * B[i*nsize+c];
}
C[r*nsize+c]=result;
}
}
int main(int argc, char *argv[]) {
if(argc < 2) {
std::cout << "Invalid number of arguments: usage " << argv[0] << " <array size>" << std::endl;
exit(0);
}
// Array size
int nsize = std::atoi(argv[1]);
// Timing Stuff
timespec seq_start, seq_stop;
timespec omp_start, omp_stop;
timespec gpu_start, gpu_stop;
// CPU side arrays
// Arrays are long 1D, indexing is (i, j) => j + (i * nsize)
// this gives a single index into the array using two loop variables
float* A = new float[nsize * nsize]();
float* B = new float[nsize * nsize]();
float* C = new float[nsize * nsize]();
// Fill CPU side arrays
for(int i = 0; i < nsize; i++) {
for(int j = 0; j < nsize; j++) {
int idx = float(j + (i * nsize));
A[idx] = idx + 1.0f;
B[idx] = 1.0f / (idx + 1.0f);
}
}
// Stop GPU timer
clock_gettime(CLOCK_REALTIME, &gpu_start);
//////////////////////////////////////////////////////////////////////
/* Add your code here */
//
//
// RAM memory allocation
int *l_a, *l_b, *l_c;
cudaMallocHost((void **)&l_a, sizeof(int) * nsize * nsize);
cudaMallocHost((void **)&l_b, sizeof(int) * nsize * nsize);
cudaMallocHost((void **)&l_c, sizeof(int) * nsize * nsize);
// GPU allocation
int *A1, *B1, *C1;
cudaMalloc((void **)&A1, sizeof(int) * nsize * nsize);
cudaMalloc((void **)&B1, sizeof(int) * nsize * nsize);
cudaMalloc((void **)&C1, sizeof(int) * nsize * nsize);
// RAM to GPU
cudaMemcpy(A1, l_a, sizeof(int) * nsize * nsize, cudaMemcpyHostToDevice);
cudaMemcpy(B1, l_b, sizeof(int) * nsize * nsize, cudaMemcpyHostToDevice);
dim3 dimGrid(32, 32,1);
dim3 dimBlock(32, 32,1);
gpu_matmul<<<dimGrid, dimBlock>>>(A1, B1, C1, nsize);
//GPU to RAM
cudaMemcpy(l_c,C1, sizeof(int)*nsize*nsize, cudaMemcpyDeviceToHost);
//
/*=======
for (int i = 0; i <= 3; i++)
{
t = clock();
GPUmatmul<<<dim3(16, 16, 16), dim3(16, 8, 8)>>>(N, A, B, C);
cudaDeviceSynchronize();
t = clock() - t;
if (i)
avg += t; // we will ignore the first run
printf("It took GPU-%d %f ms.\n", i, (((double)t) / CLOCKS_PER_SEC) * 1000);
}
avg /= 3;
avg /= CLOCKS_PER_SEC;
avg *= 1000;
printf("It took %lf ms on avg.\n", avg);
cudaFree(A);
cudaFree(B);
============*/
//////////////////////////////////////////////////////////////////////////////////
// Stop GPU timer
clock_gettime(CLOCK_REALTIME, &gpu_stop);
std::cout << "GPU Time: " << ((gpu_stop.tv_sec - gpu_start.tv_sec) + (gpu_stop.tv_nsec - gpu_start.tv_nsec) / 1E9) << '\n';
// Compute Vectorized version
// Modifies C in place.
clock_gettime(CLOCK_REALTIME, &seq_start);
seq_matmul(A, B, C, nsize);
clock_gettime(CLOCK_REALTIME, &seq_stop);
std::cout << "Seq (vectorized) Time: " << ((seq_stop.tv_sec - seq_start.tv_sec) + (seq_stop.tv_nsec - seq_start.tv_nsec) / 1E9) << '\n';
// Compute OMP version
// Modifies C in place.
clock_gettime(CLOCK_REALTIME, &omp_start);
omp_matmul(A, B, C, nsize);
clock_gettime(CLOCK_REALTIME, &omp_stop);
std::cout << "OMP Time: " << ((omp_stop.tv_sec - omp_start.tv_sec) + (omp_stop.tv_nsec - omp_start.tv_nsec) / 1E9) << '\n';
verify(C,l_c,nsize);
delete[] A;
delete[] B;
delete[] C;
cudaFree(A1);
cudaFree(B1);
cudaFree(C1);
cudaFreeHost(l_a);
cudaFreeHost(l_b);
cudaFreeHost(l_c);
return 0;
}