-
Notifications
You must be signed in to change notification settings - Fork 0
/
testFor3.c
59 lines (44 loc) · 1.11 KB
/
testFor3.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 <stdio.h>
#include <stdlib.h>
#include "timing.h"
#include <omp.h>
#include <math.h>
int main(int argc, char *argv[]) {
int 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 \n",n);
}
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 (i=0;i<n;i++){
x[i] = i;
y[i] = 2*i;
}
get_timestamp(&time1);
#pragma omp parallel for default(none) private(j) shared(x,y,z,n)
for(i=0;i<n;i++){
double a = x[i];
double b = y[i];
for ( j = 0; j < 20; ++j){
a = sin(a) + b;
b = cos(b) + a;
}
z[i] = a + b;
}
get_timestamp(&time2);
double elapsed = timestamp_diff_in_seconds(time1,time2);
printf("%f s\n", elapsed);
return 0;
}