FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
idefix/src/loop.hpp at master · idefix-code/idefix · GitHub
idefix-code
idefix
Repository navigation
Code
Issues
3
(3)
Pull requests
9
(9)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
idefix
/
src
/
loop.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
350 lines (320 loc) · 11.5 KB
Breadcrumbs
idefix
/
src
/
loop.hpp
Copy path
File metadata and controls
350 lines (320 loc) · 11.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//
***********************************************************************************
//
Idefix MHD astrophysical code
//
//
Source file src/loop.hpp
//
//
Last modified : 08/2026
//
//
Copyright(C) by :
//
- Geoffroy Lesur <geoffroy.lesur@univ-grenoble-alpes.fr> (IPAG/UGA/CNRS - 2020 - 2026)
//
- Soufiane Baghdadi <soufiane.baghdadi@univ-grenoble-alpes.fr> (IPAG/UGA/CNRS - 2020)
//
- Clément Robert <clement.robert@univ-grenoble-alpes.fr> (IPAG/UGA/CNRS - 2021)
//
and other code contributors
//
//
Licensed under CeCILL 2.1 License, see COPYING for more information
//
***********************************************************************************
//
These shortcuts are largely inspired from K-Athena
//
https://gitlab.com/pgrete/kathena
//
by P. Grete.
#
ifndef
LOOP_HPP_
#
define
LOOP_HPP_
#
include
<
string
>
#
include
"
idefix.hpp
"
#
include
"
global.hpp
"
#
define
KOKKOS_VECTOR_LENGTH
8
#
ifdef
INNER_TTR_LOOP
#
define
TPINNERLOOP
Kokkos::TeamThreadRange
#
elif
defined INNER_TVR_LOOP
#
define
TPINNERLOOP
Kokkos::ThreadVectorRange
#
else
#
define
TPINNERLOOP
Kokkos::TeamThreadRange
#
endif
typedef
Kokkos::TeamPolicy<> team_policy;
typedef
Kokkos::TeamPolicy<>::member_type member_type;
//
Check if the user requested a specific loop unrolling strategy
#
if
defined(LOOP_PATTERN_SIMD)
//
Check that Idefix Arrays can be assigned from SIMD loop
static_assert
(
Kokkos::SpaceAccessibility<Kokkos::DefaultHostExecutionSpace::execution_space,
Kokkos::DefaultExecutionSpace::memory_space>::accessible,
"
Idefix Arrays cannot be accessed from SIMD loop. You should try another loop pattern.
"
);
constexpr
LoopPattern defaultLoop = LoopPattern::
SIMDFOR
;
#
elif
defined(LOOP_PATTERN_1DRANGE)
constexpr
LoopPattern defaultLoop = LoopPattern::
RANGE
;
#
elif
defined(LOOP_PATTERN_MDRANGE)
constexpr
LoopPattern defaultLoop = LoopPattern::
MDRANGE
;
#
elif
defined(LOOP_PATTERN_TPX)
constexpr
LoopPattern defaultLoop = LoopPattern::
TPX
;
#
elif
defined(LOOP_PATTERN_TPTTRTVR)
constexpr
LoopPattern defaultLoop = LoopPattern::
TPTTRTVR
;
#
else
//
no loop strategy has been defined
//
Default loops
#
if
defined(KOKKOS_ENABLE_OPENMP)
constexpr
LoopPattern defaultLoop = LoopPattern::
TPTTRTVR
;
#
elif
defined(KOKKOS_ENABLE_CUDA)
constexpr
LoopPattern defaultLoop = LoopPattern::
RANGE
;
#
elif
defined(KOKKOS_ENABLE_HIP)
constexpr
LoopPattern defaultLoop = LoopPattern::
RANGE
;
#
elif
defined(KOKKOS_ENABLE_SYCL)
constexpr
LoopPattern defaultLoop = LoopPattern::
RANGE
;
#
elif
defined(KOKKOS_ENABLE_SERIAL)
//
Check that Idefix Arrays can be assigned from SIMD loop
static_assert
(
Kokkos::SpaceAccessibility<Kokkos::DefaultHostExecutionSpace::execution_space,
Kokkos::DefaultExecutionSpace::memory_space>::accessible,
"
Idefix Arrays cannot be accessed from Host, but Device is unknown/untested.
"
"
Ask the developers to add support.
"
);
constexpr
LoopPattern defaultLoop = LoopPattern::
SIMDFOR
;
#
else
#
warning
"Unknown target architeture: default to MDrange"
constexpr
LoopPattern defaultLoop = LoopPattern::
MDRANGE
;
#
endif
#
endif
//
1D loop
template
<
typename
Function>
inline
void
idefix_for
(
const
std::string &
NAME
,
const
int
&
IB
,
const
int
&
IE
,
Function function) {
#
ifdef
DEBUG
idfx::pushRegion
(
"
idefix_for(
"
+
NAME
+
"
)
"
);
#
endif
const
int
NI
=
IE
-
IB
;
Kokkos::parallel_for
(
NAME
, Kokkos::RangePolicy<Kokkos::LaunchBounds<
256
>>(
0
,
NI
),
KOKKOS_LAMBDA
(
const
int
&
IDX
) {
int
i =
IDX
;
i +=
IB
;
function
(i);
});
#
ifdef
DEBUG
Kokkos::fence
();
idfx::popRegion
();
#
endif
}
//
2D loop
template
<
typename
Function>
inline
void
idefix_for
(
const
std::string &
NAME
,
const
int
&
JB
,
const
int
&
JE
,
const
int
&
IB
,
const
int
&
IE
,
Function function) {
#
ifdef
DEBUG
idfx::pushRegion
(
"
idefix_for(
"
+
NAME
+
"
)
"
);
#
endif
//
Kokkos 1D Range
if
constexpr
(defaultLoop == LoopPattern::
RANGE
) {
const
int
NJ
=
JE
-
JB
;
const
int
NI
=
IE
-
IB
;
const
int
NJNI
=
NJ
*
NI
;
Kokkos::parallel_for
(
NAME
, Kokkos::RangePolicy<Kokkos::LaunchBounds<
256
>>(
0
,
NJNI
),
KOKKOS_LAMBDA
(
const
int
&
IDX
) {
int
j =
IDX
/
NI
;
int
i =
IDX
- j*
NI
;
j +=
JB
;
i +=
IB
;
function
(j,i);
});
//
MDRange loops
}
else
if
constexpr
(defaultLoop == LoopPattern::
MDRANGE
) {
Kokkos::parallel_for
(
NAME
,
Kokkos::MDRangePolicy<Kokkos::Rank<
2
, Kokkos::Iterate::Right, Kokkos::Iterate::Right>>
({
JB
,
IB
},{
JE
,
IE
}), function);
//
TeamPolicies with single inner loops
}
else
if
constexpr
(defaultLoop == LoopPattern::
TPX
|| defaultLoop == LoopPattern::
TPTTRTVR
) {
const
int
NJ
=
JE
-
JB
;
Kokkos::parallel_for
(
NAME
,
team_policy
(
NJ
, Kokkos::
AUTO
,
KOKKOS_VECTOR_LENGTH
),
KOKKOS_LAMBDA
(member_type team_member) {
const
int
j = team_member.
league_rank
() +
JB
;
Kokkos::parallel_for
(
TPINNERLOOP
<>(team_member,
IB
,
IE
),
[&] (
const
int
i) {
function
(j,i);
});
});
//
SIMD FOR loops
}
else
if
constexpr
(defaultLoop == LoopPattern::
SIMDFOR
) {
for
(
auto
j =
JB
; j <
JE
; j++)
#
pragma
omp simd
for
(
auto
i =
IB
; i <
IE
; i++)
function
(j,i);
}
else
{
throw
std::runtime_error
(
"
Unknown/undefined LoopPattern used.
"
);
}
#
ifdef
DEBUG
Kokkos::fence
();
idfx::popRegion
();
#
endif
}
//
3D loop
template
<
typename
Function>
inline
void
idefix_for
(
const
std::string &
NAME
,
const
int
&
KB
,
const
int
&
KE
,
const
int
&
JB
,
const
int
&
JE
,
const
int
&
IB
,
const
int
&
IE
,
Function function) {
#
ifdef
DEBUG
idfx::pushRegion
(
"
idefix_for(
"
+
NAME
+
"
)
"
);
#
endif
//
Kokkos 1D Range
if
constexpr
(defaultLoop == LoopPattern::
RANGE
) {
const
int
NK
=
KE
-
KB
;
const
int
NJ
=
JE
-
JB
;
const
int
NI
=
IE
-
IB
;
const
int
NKNJNI
=
NK
*
NJ
*
NI
;
const
int
NJNI
=
NJ
*
NI
;
Kokkos::parallel_for
(
NAME
,Kokkos::RangePolicy<Kokkos::LaunchBounds<
256
>>(
0
,
NKNJNI
),
KOKKOS_LAMBDA
(
const
int
&
IDX
) {
int
k =
IDX
/
NJNI
;
int
j = (
IDX
- k*
NJNI
) /
NI
;
int
i =
IDX
- k*
NJNI
- j*
NI
;
k +=
KB
;
j +=
JB
;
i +=
IB
;
function
(k,j,i);
});
//
MDRange loops
}
else
if
constexpr
(defaultLoop == LoopPattern::
MDRANGE
) {
Kokkos::parallel_for
(
NAME
,
Kokkos::MDRangePolicy<Kokkos::Rank<
3
, Kokkos::Iterate::Right, Kokkos::Iterate::Right>>
({
KB
,
JB
,
IB
},{
KE
,
JE
,
IE
}), function);
//
TeamPolicy with single inner loops
}
else
if
constexpr
(defaultLoop == LoopPattern::
TPX
) {
const
int
NK
=
KE
-
KB
;
const
int
NJ
=
JE
-
JB
;
const
int
NKNJ
=
NK
*
NJ
;
Kokkos::parallel_for
(
NAME
,
team_policy
(
NKNJ
, Kokkos::
AUTO
,
KOKKOS_VECTOR_LENGTH
),
KOKKOS_LAMBDA
(member_type team_member) {
const
int
k = team_member.
league_rank
() /
NJ
+
KB
;
const
int
j = team_member.
league_rank
() %
NJ
+
JB
;
Kokkos::parallel_for
(
TPINNERLOOP
<>(team_member,
IB
,
IE
),
[&] (
const
int
i) {
function
(k,j,i);
});
});
//
TeamPolicy with nested TeamThreadRange and ThreadVectorRange
}
else
if
constexpr
(defaultLoop == LoopPattern::
TPTTRTVR
) {
const
int
NK
=
KE
-
KB
;
Kokkos::parallel_for
(
NAME
,
team_policy
(
NK
, Kokkos::
AUTO
,
KOKKOS_VECTOR_LENGTH
),
KOKKOS_LAMBDA
(member_type team_member) {
const
int
k = team_member.
league_rank
() +
KB
;
Kokkos::parallel_for
(
Kokkos::TeamThreadRange<>(team_member,
JB
,
JE
),
[&] (
const
int
j) {
Kokkos::parallel_for
(
Kokkos::ThreadVectorRange<>(team_member,
IB
,
IE
),
[&] (
const
int
i) {
function
(k,j,i);
});
});
});
//
SIMD FOR loops
}
else
if
constexpr
(defaultLoop == LoopPattern::
SIMDFOR
) {
for
(
auto
k =
KB
; k <
KE
; k++)
for
(
auto
j =
JB
; j <
JE
; j++)
#
pragma
omp simd
for
(
auto
i =
IB
; i <
IE
; i++)
function
(k,j,i);
}
else
{
throw
std::runtime_error
(
"
Unknown/undefined LoopPattern used.
"
);
}
#
ifdef
DEBUG
Kokkos::fence
();
idfx::popRegion
();
#
endif
}
//
4D loop
template
<
typename
Function>
inline
void
idefix_for
(
const
std::string &
NAME
,
const
int
NB
,
const
int
NE
,
const
int
KB
,
const
int
KE
,
const
int
JB
,
const
int
JE
,
const
int
IB
,
const
int
IE
,
Function function) {
#
ifdef
DEBUG
idfx::pushRegion
(
"
idefix_for(
"
+
NAME
+
"
)
"
);
#
endif
//
Kokkos 1D Range
if
constexpr
(defaultLoop == LoopPattern::
RANGE
) {
const
int
NN
= (
NE
) - (
NB
);
const
int
NK
= (
KE
) - (
KB
);
const
int
NJ
= (
JE
) - (
JB
);
const
int
NI
= (
IE
) - (
IB
);
const
int
NNNKNJNI
=
NN
*
NK
*
NJ
*
NI
;
const
int
NKNJNI
=
NK
*
NJ
*
NI
;
const
int
NJNI
=
NJ
*
NI
;
Kokkos::parallel_for
(
NAME
,Kokkos::RangePolicy<Kokkos::LaunchBounds<
256
>>(
0
,
NNNKNJNI
),
KOKKOS_LAMBDA
(
const
int
&
IDX
) {
int
n =
IDX
/
NKNJNI
;
int
k = (
IDX
- n*
NKNJNI
) /
NJNI
;
int
j = (
IDX
- n*
NKNJNI
- k*
NJNI
) /
NI
;
int
i =
IDX
- n*
NKNJNI
- k*
NJNI
- j*
NI
;
n += (
NB
);
k += (
KB
);
j += (
JB
);
i += (
IB
);
function
(n,k,j,i);
});
//
MDRange loops
}
else
if
constexpr
(defaultLoop == LoopPattern::
MDRANGE
) {
Kokkos::parallel_for
(
NAME
,
Kokkos::MDRangePolicy<Kokkos::Rank<
4
,Kokkos::Iterate::Right, Kokkos::Iterate::Right>>
({
NB
,
KB
,
JB
,
IB
},{
NE
,
KE
,
JE
,
IE
}), function);
//
TeamPolicy loops
}
else
if
constexpr
(defaultLoop == LoopPattern::
TPX
) {
const
int
NN
=
NE
-
NB
;
const
int
NK
=
KE
-
KB
;
const
int
NJ
=
JE
-
JB
;
const
int
NKNJ
=
NK
*
NJ
;
const
int
NNNKNJ
=
NN
*
NK
*
NJ
;
Kokkos::parallel_for
(
NAME
,
team_policy
(
NNNKNJ
, Kokkos::
AUTO
,
KOKKOS_VECTOR_LENGTH
),
KOKKOS_LAMBDA
(member_type team_member) {
int
n = team_member.
league_rank
() /
NKNJ
;
int
k = (team_member.
league_rank
() - n*
NKNJ
) /
NJ
;
int
j = team_member.
league_rank
() - n*
NKNJ
- k*
NJ
+
JB
;
n +=
NB
;
k +=
KB
;
Kokkos::parallel_for
(
TPINNERLOOP
<>(team_member,
IB
,
IE
),
[&] (
const
int
i) {
function
(n,k,j,i);
});
});
//
TeamPolicy with nested TeamThreadRange and ThreadVectorRange
}
else
if
constexpr
(defaultLoop == LoopPattern::
TPTTRTVR
) {
const
int
NN
=
NE
-
NB
;
const
int
NK
=
KE
-
KB
;
const
int
NNNK
=
NN
*
NK
;
Kokkos::parallel_for
(
NAME
,
team_policy
(
NNNK
, Kokkos::
AUTO
,
KOKKOS_VECTOR_LENGTH
),
KOKKOS_LAMBDA
(member_type team_member) {
int
n = team_member.
league_rank
() /
NK
+
NB
;
int
k = team_member.
league_rank
() %
NK
+
KB
;
Kokkos::parallel_for
(
Kokkos::TeamThreadRange<>(team_member,
JB
,
JE
),
[&] (
const
int
j) {
Kokkos::parallel_for
(
Kokkos::ThreadVectorRange<>(team_member,
IB
,
IE
),
[&] (
const
int
i) {
function
(n,k,j,i);
});
});
});
//
SIMD FOR loops
}
else
if
constexpr
(defaultLoop == LoopPattern::
SIMDFOR
) {
for
(
auto
n =
NB
; n <
NE
; n++)
for
(
auto
k =
KB
; k <
KE
; k++)
for
(
auto
j =
JB
; j <
JE
; j++)
#
pragma
omp simd
for
(
auto
i =
IB
; i <
IE
; i++)
function
(n,k,j,i);
}
else
{
throw
std::runtime_error
(
"
Unknown/undefined LoopPattern used.
"
);
}
#
ifdef
DEBUG
Kokkos::fence
();
idfx::popRegion
();
#
endif
}
#
endif
//
LOOP_HPP_
Back
|
FazBrowse Home
|
New Git URL