-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_multiplication_seq.c
68 lines (59 loc) · 1.43 KB
/
matrix_multiplication_seq.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
int size=1000;
double **x;
double **m;
double **res;
void init(){
x = malloc(size * sizeof(double *));
m = malloc(size * sizeof(double *));
res = malloc(size * sizeof(double *));
for(int i=0;i<size; i++){
x[i] = malloc(size * sizeof(double));
m[i] = malloc(size * sizeof(double));
res[i] = malloc(size * sizeof(double));
}
}
void randd(){
time_t t;
srand((unsigned) time(&t));
//randomly assign values to the 2 matrices
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
x[i][j]=rand() % 5;
m[i][j]=rand() % 5;
}
}
}
void print(double **matrix){
for(int i=0;i<size;i++){
printf("\nRow No.%d\n",i+1);
for(int j=0;j<size;j++){
printf("%f\t",matrix[i][j]);
}
}
printf("\n");
}
int main(){
int i, j, k;
struct timeval tstart, tend;
double exectime;
init();
randd();
gettimeofday( &tstart, NULL );
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
res[i][j] = 0;
for (k = 0; k < size; k++) {
res[i][j] += x[i][k] * m[k][j];
}
}
}
gettimeofday( &tend, NULL );
exectime = (tend.tv_sec - tstart.tv_sec) * 1000.0; // sec to ms
exectime += (tend.tv_usec - tstart.tv_usec) / 1000.0; // us to ms
printf( "Matrix size: %dx%d, Execution time:%.3lf sec\n",size, size, exectime/1000.0);
return 0;
}