-
Notifications
You must be signed in to change notification settings - Fork 1
/
MatrixGPUShared.c
executable file
·126 lines (104 loc) · 2.11 KB
/
MatrixGPUShared.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
* Author - Shalitha Suranga
* shalithasuranga@gmail.com
*/
int **A;
int **B;
int **C;
int N;
double runtime=0;
void freeMemory(){
for(int i=0; i<N; i++){
free(A[i]);
free(B[i]);
free(C[i]);
}
free(A);
free(B);
free(C);
}
int randomNumber(){
return rand()%10 +1 ;
}
void fillMatrix(int **S){
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
S[i][j]=randomNumber();
}
}
}
void setupMatrices(int n){
N=n;
A=(int**)malloc(N*sizeof(int*));
for(int i=0; i<N; i++) A[i]=(int*)malloc(N*sizeof (int));
B=(int**)malloc(N*sizeof(int*));
for(int i=0; i<N; i++) B[i]=(int*)malloc(N*sizeof (int));
C=(int**)malloc(N*sizeof(int*));
for(int i=0; i<N; i++) C[i]=(int*)malloc(N*sizeof (int));
fillMatrix(A);
fillMatrix(B);
}
void multiplication(){
clock_t begin = clock();
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
int total=0;
for(int k=0; k<N; k++){
total+=*(*(A+i)+k) * *(*(B+k)+j);
}
C[i][j]=total;
}
}
clock_t end = clock();
runtime = (double)(end - begin) / CLOCKS_PER_SEC;
}
int ** showMatrix(int ** S){
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
printf("%d ",*(*(S+i)+j));
}
printf("\n");
}
}
void saveMatrices(int exTime){
char filename[100];
sprintf(filename,"output/file%d-%d.txt",N,exTime);
FILE *f=fopen(filename,"w");
if(f!=NULL){
fprintf(f, "INPUT A\n");
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
fprintf(f,"%d ",*(*(A+i)+j));
}
fprintf(f,"\n");
}
fprintf(f, "INPUT B \n");
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
fprintf(f,"%d ",*(*(B+i)+j));
}
fprintf(f,"\n");
}
fprintf(f, "OUTPUT C\n");
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
fprintf(f,"%d ",*(*(C+i)+j));
}
fprintf(f,"\n");
}
}
fclose(f);
}
int main(int arg0, char **arg1){
srand(time(NULL));
int size = atoi(arg1[1]);
setupMatrices(size);
multiplication();
printf("%0.8lf\n",runtime);
//saveMatrices(rt);
freeMemory();
return 0;
}