-
Notifications
You must be signed in to change notification settings - Fork 0
/
matmul.c
49 lines (39 loc) · 1.21 KB
/
matmul.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
#include <stdio.h>
#include <malloc.h>
#include <time.h>
#define size 5000
#define IDX(x,y) ((x)*size+(y))
int main() {
int *A, *B, *C;
int s;
A = (int *)malloc(sizeof(int)*size*size);
B = (int *)malloc(sizeof(int)*size*size);
C = (int *)malloc(sizeof(int)*size*size);
for (int i = 0; i < size*size; i++) {
A[i] = 1;
B[i] = 2;
}
struct timespec time1, time2;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++) {
s = 0;
for (int k = 0; k < size; k++)
s += A[IDX(i,k)]*B[IDX(k,j)];
C[IDX(i,j)] = s;
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
struct timespec temp;
if (time2.tv_nsec - time1.tv_nsec < 0) {
temp.tv_sec = time2.tv_sec - time1.tv_sec - 1;
temp.tv_nsec = 1000000000 + time2.tv_nsec - time1.tv_nsec;
} else {
temp.tv_sec = time2.tv_sec - time1.tv_sec;
temp.tv_nsec = time2.tv_nsec - time1.tv_nsec;
}
printf("Time: %ld.%ld\n", temp.tv_sec, temp.tv_nsec);
for (int i = 0; i < 32; i++)
printf("C[%d]=%d ", i, C[i]);
printf("\n");
return 0;
}