-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsumvec_openmp.c
68 lines (59 loc) · 1.25 KB
/
sumvec_openmp.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
60
61
62
63
64
65
66
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "common.h"
#define M_PI 3.14159265358979323846
#ifdef _OPENMP
#include <omp.h>
#else
#define omp_get_num_threads() 0
#define omp_get_thread_num() 0
#endif
Vector generateVector(int length)
{
Vector sumVector = createVector(length);
for (int i = 0; i < length; i++)
{
sumVector -> data[i] = 1.0/((i+1)*(i+1));
}
return sumVector;
}
void printVector2(int length, Vector vec)
{
printf("Here comes the vector: \n");
for (int i = 0; i < length; i++)
{
printf(" %lf ", vec -> data[i]);
}
printf("\n");
}
double sumVector(Vector vec)
{
double sum = 0;
#pragma omp parallel for schedule(static) reduction(+:sum)
for (unsigned int i = 0; i < vec -> len; i++)
{
sum += vec -> data[i];
}
return sum;
}
int main(void)
{
double sum, t1, t2, dt;
double actualSum = M_PI*M_PI/6;
for (int k = 3; k < 15; k++)
{
t1 = WallTime();
int length = pow(2.0, 1.0*k);
Vector vec = generateVector(length);
sum = sumVector(vec);
double error = (double) ((M_PI * M_PI / 6) - sum);
t2 = WallTime();
dt = t2 - t1;
printf("Number of elements: \t%d\t", length);
printf("sum: %0.10f\t", sum);
printf("time: %0.10f\t", dt);
printf("error: %0.10f\n", error);
}
return 0;
}