-
Notifications
You must be signed in to change notification settings - Fork 2
/
02-flexible-soln.c
59 lines (48 loc) · 1.31 KB
/
02-flexible-soln.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
#include "timing.h"
#include <stdio.h>
#include <stdlib.h>
extern void add_doubles(void *tgt, void *op1, void *op2);
typedef void (*operation_t)(void *, void *, void *);
void do_three_operand_loop(operation_t op, void *x, void *y, void *z,
long item_size, long n)
{
for (int i = 0; i < n; ++i)
{
op(z+i*item_size, x+i*item_size, y+i*item_size);
}
}
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "need two arguments!\n");
abort();
}
const long n = atol(argv[1]);
double *x = (double *) malloc(sizeof(double) * n);
if (!x) { perror("alloc x"); abort(); }
double *y = (double *) malloc(sizeof(double) * n);
if (!y) { perror("alloc y"); abort(); }
double *z = (double *) malloc(sizeof(double) * n);
if (!z) { perror("alloc z"); abort(); }
for (int i = 0; i < n; ++i)
{
x[i] = i;
y[i] = 2*i;
}
const int ntrips = atoi(argv[2]);
printf("doing %d trips...\n", ntrips);
timestamp_type time1, time2;
get_timestamp(&time1);
for (int trip = 0; trip < ntrips; ++trip)
{
do_three_operand_loop(add_doubles, x, y, z, sizeof(double), n);
}
get_timestamp(&time2);
double elapsed = timestamp_diff_in_seconds(time1,time2)/ntrips;
printf("%f GB/s\n",
3*n*sizeof(double)/1e9/elapsed);
printf("%f GFlops/s\n",
n/1e9/elapsed);
return 0;
}