forked from davemus/parallel-computings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fourth.c
57 lines (44 loc) · 1.14 KB
/
fourth.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
#include <unistd.h>
#define THREADS 3
#define N 1E9
double start=0;
double finish=1;
double step;
typedef struct {double A,B;} pth;//replaced `int by `double
double atandiff(double x) {
return 1.0 / (1 + x*x);
}
void* thread_func(void* arg) {
pth *init = (pth*) arg;
double *S = (double*)malloc(sizeof(double));
for (double x = init->A; x < init->B; x = x + step) {
*S = *S +0.5 * step * (atandiff(x) + atandiff(x+step));
}
printf("Sum is: %.10f\n",*S);
return S;
}
int main() {
step = (finish - start) / N;
double sum = 0;
pth args[THREADS];
pthread_t th[THREADS];
void *thread_return[THREADS];
for (int i = 0; i < THREADS; i++) {
args[i].A = start + (finish - start) / THREADS * i;
args[i].B = start + (finish - start) / THREADS * (i + 1);
pthread_create(&th[i], NULL, thread_func, (void*) &args[i]);
}
for(int i = 0; i < THREADS; i++) {
pthread_join(th[i], (&thread_return[i]));
}
for (int i = 0; i < THREADS; i++) {
sum += *(double *) thread_return[i];
}
printf("SUM=%16.15f\n", sum);
printf("Precision=%16.15f\n", 4 * sum - M_PI);
return 0;
}