-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelloworld.c
More file actions
50 lines (45 loc) · 1.35 KB
/
Copy pathhelloworld.c
File metadata and controls
50 lines (45 loc) · 1.35 KB
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
#include "omp.h"
#include <stdio.h>
#include <time.h>
static long num_steps = 100000;
double step;
#define NUM_THREADS 12
int main(int argc, char *argv[])
{
int nthreads;
double sum;
step = 1.0/(double)num_steps;
double firstTime = omp_get_wtime();
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel
{
//printf("num threads: (%d) \n", omp_get_num_threads());
int numThreads = omp_get_num_threads();
// printf("num threads now: (%d) \n", numThreads);
int threadNum = omp_get_thread_num();
int threadStep = num_steps/numThreads;
int localMax = threadStep + (threadNum * threadStep);
int start = threadStep * threadNum;
int i;
double disSum;
//printf("initial: %d\n going to: %d\n", start, localMax);
if (threadNum == 0)
{
nthreads = numThreads;
// printf("num of threads: %d %d\n", nthreads, threadNum);
}
#pragma omp for schedule(staic , 100)
// for (i=start;i<localMax;i++)
for (i=0;i<num_steps;i++)
{
double x = (i+0.5)*step;
disSum += 4.0/(1.0+x*x);
}
#pragma omp atomic
sum += disSum * step;
}
double secondTime = omp_get_wtime() - firstTime;
printf("time: %f \n", secondTime);
printf("pi: (%f)\n", sum);
return 0;
}