-
Notifications
You must be signed in to change notification settings - Fork 0
/
race3.c
53 lines (38 loc) · 1 KB
/
race3.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
#include <stdio.h>
#include <stdlib.h>
#include "timing.h"
#include <omp.h>
#include <math.h>
int main(int argc, char *argv[]) {
double sum = 0;
int i,j;
size_t n;
timestamp_type time1, time2;
if (argc != 2){
printf("Must enter integer for array size on command line\n");
exit(-1);
}
else {
n = atoi(argv[1]);
printf("Using array size %ld and %d threads\n",n,omp_get_num_threads());
}
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(); }
for (i=0;i<n;i++){
x[i] = i;
y[i] = 2*i;
}
get_timestamp(&time1);
#pragma omp parallel for default(none) shared(x,y,n) \
reduction(+:sum)
for(i=0;i<n;i++){
sum += x[i] * y[i];
}
get_timestamp(&time2);
double elapsed = timestamp_diff_in_seconds(time1,time2);
printf("%f s\n", elapsed);
printf("sum = %15.7e\n",sum);
return 0;
}