-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm.cu
149 lines (121 loc) · 3.73 KB
/
mm.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#define LEN 1024
#define BLOCK_SIZE 16
/*
#define CUDA_CHECK(err) { if (err != cudaSuccess) { \
fprintf(stderr, "CUDA error: %s\n", cudaGetErrorString(err)); \
exit(EXIT_FAILURE); }}
*/
//this math is correct
__global__ void gpumatrixmulti(double *a, double *b, double *c, int N){
//printf("here\n");
int row = blockIdx.y * blockDim.y + threadIdx.y;
int col = blockIdx.x * blockDim.x + threadIdx.x;
if(row < N && col < N){
float sum = 0;
for(int i = 0; i < N; i++){
sum += a[row*N + i] * b[i*N + col];
}
//printf("%f\n", sum);
c[row*N +col] = sum;
}
}
//this function works
void cpu_mm(double *a, double *b, double *c){
float sum = 0;
for(int i = 0; i < LEN; i++){
for(int j = 0; j < LEN; j++){
sum = 0;
for(int k = 0; k < LEN; k++){
sum += a[i * LEN + k] * b[k * LEN + j];
}
c[i*LEN+j] = sum;
}
}
}
double CLOCK() {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (t.tv_sec * 1000)+(t.tv_nsec*1e-6);
}
int main(int argc, char* argv[]){
printf("Running");
int N = LEN;
double *h_a;
double *h_b;
double *h_c;
cudaMallocHost((void **) &h_a, sizeof(double)*LEN*LEN);
cudaMallocHost((void **) &h_b, sizeof(double)*LEN*LEN);
cudaMallocHost((void **) &h_c, sizeof(double)*LEN*LEN);
for(int i = 0; i < LEN*LEN; i++){
h_a[i] = 1.0;
h_b[i] = 2.0;
h_c[i] = 5.0;
}
float gpu_time_ms, total_time;
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
//double clock_start, finish;
//cudaEventRecord(start, 0);
//copies data from the host to the GPU device
double *d_a;
double *d_b;
double *d_c;
cudaMalloc((void **) &d_a, sizeof(double)*LEN*LEN);
cudaMalloc((void **) &d_b, sizeof(double)*LEN*LEN);
cudaMalloc((void **) &d_c, sizeof(double)*LEN*LEN);
cudaMemcpy(d_a, h_a, sizeof(double)*LEN*LEN, cudaMemcpyHostToDevice);
cudaMemcpy(d_b, h_b, sizeof(double)*LEN*LEN, cudaMemcpyHostToDevice);
cudaMemcpy(d_c, h_c, sizeof(double)*LEN*LEN, cudaMemcpyHostToDevice);
unsigned int grid_rows = (N + BLOCK_SIZE - 1) / BLOCK_SIZE;
unsigned int grid_cols = (N + BLOCK_SIZE - 1) / BLOCK_SIZE;
dim3 dimGrid(grid_cols, grid_rows);
dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);
printf("HERE\n");
for(int iterations = 0; iterations < 200; iterations++){
memset(h_c, 0.0, sizeof(double)*LEN*LEN);
cudaEventRecord(start, 0);
gpumatrixmulti<<<dimGrid, dimBlock>>>(d_a, d_b, d_c, N);
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
fprintf(stderr, "Kernel launch failed: %s\n", cudaGetErrorString(err));
//return -1; // Handle the error as appropriate
}
cudaDeviceSynchronize();
//printf("DONE\n");
//cudaMemcpy(h_c, d_c, sizeof(float)*LEN*LEN, cudaMemcpyDeviceToHost);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaMemcpy(h_c, d_c, sizeof(double)*LEN*LEN, cudaMemcpyDeviceToHost);
cudaEventElapsedTime(&gpu_time_ms, start, stop);
total_time += gpu_time_ms;
}
printf("Time elapsed: %f \n", total_time);
//double diff = finish - clock_start;
//printf("Clock time elapse: %f \n", diff);
printf("Result at (0,0): %f\n", h_c[5]);
//for (int i = 0; i < 10; i++) {
// for (int j = 0; j < 10; j++) {
// printf("Result at (%d,%d): %f\n", i, j, h_c[i * LEN + j]);
// }
//}
float cpu_time_ms;
cudaEventRecord(start, 0);
cpu_mm(h_a, h_b, h_c);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&cpu_time_ms, start, stop);
printf("CPU Time: %f\n", cpu_time_ms);
printf("Result is %f\n", h_c[5]);
cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_c);
cudaFreeHost(h_a);
cudaFreeHost(h_b);
cudaFreeHost(h_c);
return 0;
}