forked from rgayatri23/OpenMPTargetTestCases
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRedn.cpp
More file actions
70 lines (61 loc) · 1.94 KB
/
TestRedn.cpp
File metadata and controls
70 lines (61 loc) · 1.94 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <omp.h>
#define N 100
#define nT 64
int main(int argc, char **argv) {
int scalar = 0;
int vector[N];
size_t scratch_size = N * nT * sizeof(int);
int *scratch = static_cast<int *>(
omp_target_alloc(scratch_size, omp_get_default_device()));
auto lambda1 = [=](const int i, int *scratch, int& scalar) {
int *my_scratch = scratch + (omp_get_team_num() * nT);
#pragma omp for reduction(+ : my_scratch [0:1])
for (int j = 0; j < N; ++j) {
my_scratch[0] += i + 1;
} // end j
scalar += my_scratch[0];
};
auto lambda2 = [=](const int i, int *scratch) {
int *my_scratch = scratch + (omp_get_team_num() * nT);
#pragma omp for reduction(+ : my_scratch [0:1])
for (int j = 0; j < N; ++j) {
my_scratch[0] += i + 1;
} // end j
};
// Case 1 - reduction is over teams and parallel-for level.
#pragma omp target teams distribute reduction(+ : scalar) thread_limit(nT) \
is_device_ptr(scratch)
for (int i = 0; i < N; ++i) {
#pragma omp parallel num_threads(nT)
{
lambda1(i, scratch, scalar);
} // end parallel
} // end teams
// Case 2 - reduction is over parallel-for level.
#pragma omp target teams distribute thread_limit(nT) \
map(tofrom: vector[0:N]) \
is_device_ptr(scratch)
for (int i = 0; i < N; ++i) {
#pragma omp parallel num_threads(nT)
{
int *my_scratch = scratch + (omp_get_team_num() * nT);
lambda2(i, scratch);
vector[i] = my_scratch[0];
} // end parallel
} // end teams
size_t result = 0;
for(int i = 0; i < N; ++i)
result += vector[i];
if(result != 2*scalar)
{
printf("Failure : scalar!=vector, scalar = %d, vector = %zu\n",2*scalar, result);
}
if (scalar == ((N * N * (N + 1)) / 2)) {
std::cout << "success" << std::endl;
} else {
std::cout << "failure" << std::endl;
}
std::cout << "expected = " << ((N * N * (N + 1)) / 2) << std::endl;
std::cout << "scalar = " << scalar << std::endl;
}