-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdrange.cxx
154 lines (127 loc) · 5.43 KB
/
mdrange.cxx
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <string>
#include <Kokkos_Core.hpp>
#include <Kokkos_OffsetView.hpp>
#include "YAKL.h"
using namespace std;
template <typename ExecutionSpace=Kokkos::DefaultExecutionSpace>
using MDRangeP2 = Kokkos::MDRangePolicy<ExecutionSpace, Kokkos::Rank<2, Kokkos::Iterate::Right, Kokkos::Iterate::Left> >;
template <typename ExecutionSpace=Kokkos::DefaultExecutionSpace>
using MDRangeP3RL = Kokkos::MDRangePolicy<ExecutionSpace, Kokkos::Rank<3, Kokkos::Iterate::Right, Kokkos::Iterate::Left> >;
template <typename ExecutionSpace=Kokkos::DefaultExecutionSpace>
using MDRangeP3LR = Kokkos::MDRangePolicy<ExecutionSpace, Kokkos::Rank<3, Kokkos::Iterate::Left, Kokkos::Iterate::Right> >;
template <typename ExecutionSpace=Kokkos::DefaultExecutionSpace>
using MDRangeP3D = Kokkos::MDRangePolicy<ExecutionSpace, Kokkos::Rank<3> >;
template <typename ExecutionSpace=Kokkos::DefaultExecutionSpace>
using MDRangeP3R = Kokkos::MDRangePolicy<ExecutionSpace, Kokkos::Rank<3, Kokkos::Iterate::Right> >;
template <typename ExecutionSpace=Kokkos::DefaultExecutionSpace>
using MDRangeP3L = Kokkos::MDRangePolicy<ExecutionSpace, Kokkos::Rank<3, Kokkos::Iterate::Left> >;
using DefaultDevice =
Kokkos::Device<Kokkos::DefaultExecutionSpace, Kokkos::DefaultExecutionSpace::memory_space>;
using HostDevice =
Kokkos::Device<Kokkos::DefaultHostExecutionSpace, Kokkos::DefaultHostExecutionSpace::memory_space>;
template <typename T>
void print_iteration(T policy, const std::string& name, int dim1, int dim2, int dim3)
{
std::cout << "Iterating " << name << std::endl;
Kokkos::parallel_for(policy, KOKKOS_LAMBDA(int i, int j, int k) {
cout << " Iterating (" << i << ", " << j << ", " << k << ")" << endl;
});
}
KOKKOS_INLINE_FUNCTION
void unflatten_idx(const int idx, const Kokkos::Array<int, 2>& dims, int& i, int& j)
{
i = idx / dims[1];
j = idx % dims[1];
}
KOKKOS_INLINE_FUNCTION
void unflatten_idx(const int idx, const Kokkos::Array<int, 3>& dims, int& i, int& j, int& k)
{
i = (idx / dims[2]) / dims[1];
j = (idx / dims[2]) % dims[1];
k = idx % dims[2];
}
KOKKOS_INLINE_FUNCTION
void unflatten_idx(const int idx, const Kokkos::Array<int, 4>& dims, int& i, int& j, int& k, int& l)
{
i = idx / (dims[3]*dims[2]*dims[1]);
j = (idx / (dims[3]*dims[2])) % dims[1];
k = (idx / dims[3]) % dims[2];
l = idx % dims[3];
}
KOKKOS_INLINE_FUNCTION
void unflatten_idx_rev(const int idx, const Kokkos::Array<int, 3>& dims, int& i, int& j, int& k)
{
i = idx % dims[0];
j = (idx / dims[0]) % dims[1];
k = (idx / dims[0]) / dims[1];
}
void print_iteration_flat(int dim1, int dim2, int dim3)
{
const int tot = dim1*dim2*dim3;
Kokkos::Array<int, 3> dims = {dim1, dim2, dim3};
std::cout << "Iterating flat" << std::endl;
Kokkos::parallel_for(tot, KOKKOS_LAMBDA(int idx) {
int i, j, k;
unflatten_idx(idx, dims, i, j, k);
cout << " Iterating (" << i << ", " << j << ", " << k << ")" << endl;
});
}
void print_iteration_flat_rev(int dim1, int dim2, int dim3)
{
const int tot = dim1*dim2*dim3;
Kokkos::Array<int, 3> dims = {dim1, dim2, dim3};
std::cout << "Iterating flat" << std::endl;
Kokkos::parallel_for(tot, KOKKOS_LAMBDA(int idx) {
int i, j, k;
unflatten_idx_rev(idx, dims, i, j, k);
cout << " Iterating (" << i << ", " << j << ", " << k << ")" << endl;
});
}
void print_iteration_flat4(int dim1, int dim2, int dim3, int dim4)
{
const int tot = dim1*dim2*dim3*dim4;
Kokkos::Array<int, 4> dims = {dim1, dim2, dim3, dim4};
std::cout << "Iterating flat" << std::endl;
Kokkos::parallel_for(tot, KOKKOS_LAMBDA(int idx) {
int i, j, k, l;
unflatten_idx(idx, dims, i, j, k, l);
cout << " Iterating (" << i << ", " << j << ", " << k << ", " << l << ")" << endl;
});
}
int main(int argc, char** argv)
{
Kokkos::initialize(argc, argv); {
int dim1 = 2;
int dim2 = 3;
int dim3 = 4;
print_iteration(MDRangeP3RL<>({0,0,0}, {dim1, dim2, dim3}), "MDRangeP3RL", 2, 3, 4);
print_iteration(MDRangeP3LR<>({0,0,0}, {dim1, dim2, dim3}), "MDRangeP3LR", 2, 3, 4);
print_iteration(MDRangeP3L <>({0,0,0}, {dim1, dim2, dim3}), "MDRangeP3L", 2, 3, 4);
print_iteration(MDRangeP3R <>({0,0,0}, {dim1, dim2, dim3}), "MDRangeP3R", 2, 3, 4);
print_iteration(MDRangeP3D <>({0,0,0}, {dim1, dim2, dim3}), "MDRangeP3D", 2, 3, 4);
print_iteration(MDRangeP3RL<>({0,0,0}, {dim1, dim2, dim3}, {1,1,1}), "MDRangeP3RL_tile", 2, 3, 4);
print_iteration(MDRangeP3LR<>({0,0,0}, {dim1, dim2, dim3}, {1,1,1}), "MDRangeP3LR_tile", 2, 3, 4);
print_iteration(MDRangeP3L <>({0,0,0}, {dim1, dim2, dim3}, {1,1,1}), "MDRangeP3L_tile", 2, 3, 4);
print_iteration(MDRangeP3R <>({0,0,0}, {dim1, dim2, dim3}, {1,1,1}), "MDRangeP3R_tile", 2, 3, 4);
print_iteration(MDRangeP3D <>({0,0,0}, {dim1, dim2, dim3}, {1,1,1}), "MDRangeP3D_tile", 2, 3, 4);
print_iteration_flat(2, 3, 4);
print_iteration_flat_rev(2, 3, 4);
std::cout << "Validating" << std::endl;
for (int k = 0; k < dim3; ++k) {
for (int j = 0; j < dim2; ++j) {
for (int i = 0; i < dim1; ++i) {
cout << " Iterating (" << i << ", " << j << ", " << k << ")" << endl;
}
}
}
std::cout << "YAKL" << std::endl;
using yakl::fortran::SimpleBounds;
parallel_for( YAKL_AUTO_LABEL() , SimpleBounds<3>(dim1, dim2, dim3) , YAKL_LAMBDA (int i, int j, int k) {
cout << " Iterating (" << i-1 << ", " << j-1 << ", " << k-1 << ")" << endl;
});
print_iteration_flat4(2, 3, 4, 5);
}
Kokkos::finalize();
return 0;
}