Newer
Older
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
/*
* integrade.c
*
* A prgram integrate the function y=x^2 from 0 to 2.
*
*/
#include <time.h>
#include <stdio.h>
#include <omp.h>
/* you can adjust this to get reasonable running time on your computer */
/* #define NUM_STEPS (1<<29) */
/*#define NUM_STEPS 1000000000 */
int main(int argc, char *argv[])
{
double start, stop; /* for keeping track of running time */
double sum;
double x;
long i;
long NUM_STEPS=1000000000;
/* time starts now */
start = clock();
printf("thread number %d\n",omp_get_thread_num());
printf("current threads %d\n",omp_get_num_threads());
printf("max threads %d\n",omp_get_max_threads());
printf("number procs %d\n",omp_get_num_procs());
sum = 0;
#pragma omp parallel for reduction(+: sum)
for (i = 0; i<NUM_STEPS; i++) {
x = 2.0 * (double)i / (double)(NUM_STEPS); /* value of x */
sum += x * x;
}
sum=sum*2./NUM_STEPS;
/* we're done so stop the timer */
stop = clock();
printf("found result %f in %.3f seconds\n", sum, (stop-start)/1000000);
return 0;
}