-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixMulKernel.cu
154 lines (124 loc) · 3.75 KB
/
MatrixMulKernel.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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <cuda.h>
#define TILE_WIDTH 16
#define cudaCheckError() { \
cudaError_t e = cudaGetLastError(); \
if (e != cudaSuccess) { \
printf("CUDA error %s:%d: %s\n", __FILE__, __LINE__, \
cudaGetErrorString(e)); \
exit(1); \
} \
}
__global__ void MatrixMulKernel (float* Nd, float* Pd, int width, int height)
{
__shared__ float Mds[TILE_WIDTH][TILE_WIDTH];
__shared__ float Nds[TILE_WIDTH][TILE_WIDTH];
int bx = blockIdx.x; int by = blockIdx.y;
int tx = threadIdx.x; int ty = threadIdx.y;
int Row = by * TILE_WIDTH + ty;
int Col = bx * TILE_WIDTH + tx;
float Pvalue = 0;
float tempM, tempN;
if ((0*TILE_WIDTH + ty) < height && Row < width)
tempM = Nd[(0*TILE_WIDTH + ty)*width + Col];
else
tempM = 0.0;
if ((0*TILE_WIDTH + tx) < height && Col < width)
tempN = Nd[(0*TILE_WIDTH + tx)*width + Row];
else
tempN = 0.0;
for (int m=1; m <= (TILE_WIDTH + height - 1)/TILE_WIDTH; ++m)
{
Mds[ty][tx] = tempM;
Nds[tx][ty] = tempN;
__syncthreads();
if ((m*TILE_WIDTH + ty) < height && Row < width)
tempM = Nd[(m*TILE_WIDTH + ty)*width + Col];
else
tempM = 0.0;
if ((m*TILE_WIDTH + tx) < height && Col < width)
tempN = Nd[(m*TILE_WIDTH + tx)*width + Row];
else
tempN = 0.0;
for (int k=0; k<TILE_WIDTH; ++k)
Pvalue+=Mds[k][ty] * Nds[k][tx];
__syncthreads();
}
Pd[Row*width + Col] = Pvalue;
}
int main(int argc, char* argv[])
{
float *A_h, *C_h;
float *A_d, *C_d;
int i, width, height, size_A, size_C;
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
srand(time(NULL));
if (argc != 3)
{
printf("Provide the problem size.\n");
return -1;
}
height = atoi(argv[1]);
width = atoi(argv[2]);
size_A = width * height * sizeof(float);
size_C = width* width * sizeof(float);
//memory allocation for host matrixes
A_h = (float *)malloc(size_A);
C_h = (float *)malloc(size_C);
if ((A_h == NULL) || (C_h == NULL))
{
printf("Could not allocate memory.\n");
return -2;
}
//initialization of matrixes
for (i = 0; i < width*height; i++) {
A_h[i] = (rand() % 100) / 100.00;
}
//memory allocation of device matrixes
cudaMalloc((void**) &A_d, size_A); cudaCheckError();
cudaMalloc((void**) &C_d, size_C); cudaCheckError();
//copy Host matrixes to Device matrixes
cudaMemcpy(A_d, A_h, size_A, cudaMemcpyHostToDevice); cudaCheckError();
//dimensions of device
dim3 dimGrid(((width-1)/TILE_WIDTH)+1, ((width-1)/TILE_WIDTH)+1, 1);
dim3 dimBLock(TILE_WIDTH,TILE_WIDTH,1);
cudaEventRecord(start);
//calculation of multiplication
MatrixMulKernel<<<dimGrid, dimBLock>>>(A_d, C_d, width, height);
cudaCheckError();
cudaEventRecord(stop);
//copy device results to host
cudaMemcpy(C_h, C_d, size_C, cudaMemcpyDeviceToHost); cudaCheckError();
cudaEventSynchronize(stop);
float milliseconds = 0;
cudaEventElapsedTime(&milliseconds, start, stop);
printf("Milliseconds: %f\n", milliseconds);
//free device memory
cudaFree(A_d); cudaCheckError();
cudaFree(C_d); cudaCheckError();
//print results
// for (i = 0; i<width*height; i++)
// {
// if(i % width == 0)
// {
// printf("\n");
// }
// printf("%f, ", A_h[i]);
// }
// printf("\n\n");
// printf("\n");
// for (i = 0; i<width*width; i++)
// {
// if(i % width == 0)
// {
// printf("\n");
// }
// printf("%f, ", C_h[i]);
// }
// printf("\n\n");
// printf("\n");
}