-
Notifications
You must be signed in to change notification settings - Fork 2
/
mmult_omp_timing.c
52 lines (45 loc) · 1.41 KB
/
mmult_omp_timing.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
#include <stdio.h>
#include <time.h>
#include <sys/times.h>
#include <stdlib.h>
int mmult(double *c,
double *a, int aRows, int aCols,
double *b, int bRows, int bCols);
int mmult_omp(double *c,
double *a, int aRows, int aCols,
double *b, int bRows, int bCols);
double* gen_matrix(int n, int m);
void compare_matrices(double* a, double* b, int nRows, int nCols);
double deltaTime(struct timespec* start, struct timespec* end) {
double delta = (end->tv_sec - start->tv_sec) + (end->tv_nsec - start->tv_nsec)/1e9;
return delta;
}
int main(int argc, char* argv[]) {
struct timespec start;
struct timespec end;
struct timespec res;
double *a, *b, *c1, *c2;
int n;
double times[2];
if (argc > 1) {
n = atoi(argv[1]);
a = gen_matrix(n, n);
b = gen_matrix(n, n);
c1 = malloc(sizeof(double) * n * n);
c2 = malloc(sizeof(double) * n * n);
clock_gettime(CLOCK_REALTIME, &start);
mmult(c1, a, n, n, b, n, n);
clock_gettime(CLOCK_REALTIME, &end);
times[0] = deltaTime(&start, &end);
printf("%d %f", n, times[0]);
clock_gettime(CLOCK_REALTIME, &start);
mmult_omp(c2, a, n, n, b, n, n);
clock_gettime(CLOCK_REALTIME, &end);
times[1] = deltaTime(&start, &end);
printf(" %f", times[1]);
printf("\n");
compare_matrices(c1, c2, n, n);
} else {
fprintf(stderr, "Usage %s <n>\n", argv[0]);
}
}