-
Notifications
You must be signed in to change notification settings - Fork 4
/
matrixMultiplication.cu
65 lines (64 loc) · 1.3 KB
/
matrixMultiplication.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
#include<stdio.h>
#define BLOCK_DIM 1
#define N 16
__global__ void matmul(int *a, int *b, int *c, int width)
{
int k, sum=0;
int col=blockIdx.x*blockDim.x+threadIdx.x;
int row=blockIdx.y*blockDim.y+threadIdx.y;
if(col<width && row<width){
for(k=0;k<width;k++){
sum+=a[row*width+k]*b[k*width+col];
c[row*width+col]=sum;
}
}
}
int main(void)
{
int a[N][N],b[N][N],c[N][N];
int *d_a,*d_b,*d_c;
int size = sizeof(int) * N * N;
printf("Elements of matA\n");
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
a[i][j]=i;
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Elements of matB\n");
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
b[i][j]=j;
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n");
cudaMalloc((void **)&d_a,size);
cudaMalloc((void **)&d_b,size);
cudaMalloc((void **)&d_c,size);
cudaMemcpy(d_a,a,size,cudaMemcpyHostToDevice);
cudaMemcpy(d_b,b,size,cudaMemcpyHostToDevice);
dim3 dimBlock(BLOCK_DIM,BLOCK_DIM);
dim3 dimGrid((int)(N/dimBlock.x),(int)(N/dimBlock.y));
matmul<<<dimGrid,dimBlock>>>(d_a,d_b,d_c,N);
cudaMemcpy(c,d_c,size,cudaMemcpyDeviceToHost);
printf("Elements of MatC\n");
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_c);
return 0;
}