-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquad.c
87 lines (61 loc) · 2.17 KB
/
quad.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Prevajanje programa:
// gcc -O2 quad.c -fopenmp -lm -o quad
// Zagon programa:
// srun -n1 --reservation=fri --cpus-per-task=32 ./quad
#include <stdio.h>
#include <math.h>
#include <omp.h>
#define TOL 1e-8
#define THREADS 32
double func(double x) {
return sin(x*x);
}
// funkcija za integracijo, spodnja meja, zgornja meja, dovoljena napaka
double quad(double (*f)(double), double lower, double upper, double tol) {
double quad_res; // rezultat
double h; // dolzina intervala
double middle; // sredina intervala
double quad_coarse; // groba aproksimacija
double quad_fine; // fina aproksimacija (two trapezoids)
double quad_lower; // rezultat na spodnjem intervalu
double quad_upper; // rezultat na zgornjem intervalu
double eps; // razlika
h = upper - lower;
middle = (lower + upper) / 2;
// izracunaj integral z obema aproksimacijama trapezoidnega pravila
quad_coarse = h * (f(lower) + f(upper)) / 2.0; // na celem intervalu
quad_fine = h/2 * (f(lower) + f(middle)) / 2.0 + h/2 * (f(middle) + f(upper)) / 2.0; // seštevek dveh polovic
eps = fabs(quad_coarse - quad_fine);
// ce se nismo dosegli zelene natancnosti, razdelimo interval na pol in ponovimo
if (eps > tol) {
#pragma omp task shared(quad_lower) final(h < 1.0)
quad_lower = quad(f, lower, middle, tol / 2);
quad_upper = quad(f, middle, upper, tol / 2);
#pragma omp taskwait
quad_res = quad_lower + quad_upper;
} else {
quad_res = quad_fine;
}
return quad_res;
}
int main(int argc, char* argv[]) {
double quadrature;
double dt = omp_get_wtime();
omp_set_num_threads(THREADS);
#pragma omp parallel
#pragma omp master
quadrature = quad(func, 0.0, 50.0, TOL);
dt = omp_get_wtime() - dt;
printf("Integral: %lf\nCas: %lf s\n", quadrature, dt);
return 0;
}
/*
Rezultat naloge:
ŠT. NITI ČAS POHITRITEV
1 23.715206 s 1.00
2 12.777247 s 1.86
4 6.903310 s 3.44
8 4.487764 s 5.29
16 2.441063 s 9.72
32 1.563141 s 15.20
*/