-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3_openmp_offload.cpp
More file actions
52 lines (47 loc) · 1.26 KB
/
3_openmp_offload.cpp
File metadata and controls
52 lines (47 loc) · 1.26 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
51
52
#include <cstdio>
#include <omp.h>
#include "offload.h"
#include <unistd.h>
#include <cmath>
#include <iostream>
#include <sys/times.h>
#define MIC_DEV 0
int main(int argc, char *argv[])
{
const unsigned long numSteps=500000000;
double PI25DT = 3.141592653589793238462643;
double pi=0;
double sum=0.0;
double x;
double step;
clock_t clockStart, clockStop;
tms tmsStart, tmsStop;
step = 1./static_cast<double>(numSteps);
#pragma offload target(mic:MIC_DEV) // first call to device might take some time
{
#pragma omp parallel
{
#pragma omp master
{
printf ("num threads=%d\n", omp_get_num_threads());
}
}
}
clockStart = times(&tmsStart);
#pragma offload target(mic:MIC_DEV) inout(sum) in(step)
{
#pragma omp parallel for private (x), reduction (+:sum)
for (int i=0; i<numSteps; i++)
{
x = (i + .5)*step;
sum = sum + 4.0/(1.+ x*x);
}
}
pi = sum*step;
clockStop = times(&tmsStop);
std::cout << "The value of PI is " << pi << " Error is " << fabs(pi - PI25DT) << std::endl;
std::cout << "The time to calculate PI was " ;
double secs= (clockStop - clockStart)/static_cast<double>(sysconf(_SC_CLK_TCK));
std::cout << secs << " seconds\n" << std::endl;
return 0;
}