-
Notifications
You must be signed in to change notification settings - Fork 100
/
forall.hpp
210 lines (166 loc) · 6.25 KB
/
forall.hpp
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
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Copyright (c) 2016-22, Lawrence Livermore National Security, LLC
// and RAJA project contributors. See the RAJA/LICENSE file for details.
//
// SPDX-License-Identifier: (BSD-3-Clause)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
#ifndef RAJA_target_forall_openmp_HPP
#define RAJA_target_forall_openmp_HPP
#include "RAJA/config.hpp"
#if defined(RAJA_ENABLE_TARGET_OPENMP)
#include <omp.h>
#include "RAJA/util/types.hpp"
#include "RAJA/policy/openmp/policy.hpp"
#include "RAJA/pattern/params/forall.hpp"
namespace RAJA
{
namespace policy
{
namespace omp
{
///
/// OpenMP target parallel for policy implementation
///
template <size_t ThreadsPerTeam, typename Iterable, typename Func, typename ForallParam>
RAJA_INLINE
concepts::enable_if_t<
resources::EventProxy<resources::Omp>,
RAJA::expt::type_traits::is_ForallParamPack<ForallParam>,
concepts::negate<RAJA::expt::type_traits::is_ForallParamPack_empty<ForallParam>>
>
forall_impl(resources::Omp omp_res,
const omp_target_parallel_for_exec<ThreadsPerTeam>& p,
Iterable&& iter,
Func&& loop_body,
ForallParam f_params)
{
using EXEC_POL = typename std::decay<decltype(p)>::type;
RAJA::expt::ParamMultiplexer::init<EXEC_POL>(f_params);
RAJA_OMP_DECLARE_REDUCTION_COMBINE;
using Body = typename std::remove_reference<decltype(loop_body)>::type;
Body body = loop_body;
RAJA_EXTRACT_BED_IT(iter);
// Reset if exceed CUDA threads per block limit.
int tperteam = ThreadsPerTeam;
if ( tperteam > omp::MAXNUMTHREADS )
{
tperteam = omp::MAXNUMTHREADS;
}
// calculate number of teams based on user defined threads per team
// datasize is distance between begin() and end() of iterable
auto numteams = RAJA_DIVIDE_CEILING_INT( distance_it, tperteam );
if ( numteams > tperteam )
{
// Omp target reducers will write team # results, into Threads-sized array.
// Need to insure NumTeams <= Threads to prevent array out of bounds access.
numteams = tperteam;
}
// thread_limit(tperteam) unused due to XL seg fault (when tperteam != distance)
auto i = distance_it;
#pragma omp target teams distribute parallel for num_teams(numteams) \
schedule(static, 1) map(to : body,begin_it) reduction(combine: f_params)
for (i = 0; i < distance_it; ++i) {
Body ib = body;
RAJA::expt::invoke_body(f_params, ib, begin_it[i]);
}
RAJA::expt::ParamMultiplexer::resolve<EXEC_POL>(f_params);
return resources::EventProxy<resources::Omp>(omp_res);
}
template <size_t ThreadsPerTeam, typename Iterable, typename Func, typename ForallParam>
RAJA_INLINE
concepts::enable_if_t<
resources::EventProxy<resources::Omp>,
RAJA::expt::type_traits::is_ForallParamPack<ForallParam>,
RAJA::expt::type_traits::is_ForallParamPack_empty<ForallParam>
>
forall_impl(resources::Omp omp_res,
const omp_target_parallel_for_exec<ThreadsPerTeam>&,
Iterable&& iter,
Func&& loop_body,
ForallParam)
{
using Body = typename std::remove_reference<decltype(loop_body)>::type;
Body body = loop_body;
RAJA_EXTRACT_BED_IT(iter);
// Reset if exceed CUDA threads per block limit.
int tperteam = ThreadsPerTeam;
if ( tperteam > omp::MAXNUMTHREADS )
{
tperteam = omp::MAXNUMTHREADS;
}
// calculate number of teams based on user defined threads per team
// datasize is distance between begin() and end() of iterable
auto numteams = RAJA_DIVIDE_CEILING_INT( distance_it, tperteam );
if ( numteams > tperteam )
{
// Omp target reducers will write team # results, into Threads-sized array.
// Need to insure NumTeams <= Threads to prevent array out of bounds access.
numteams = tperteam;
}
// thread_limit(tperteam) unused due to XL seg fault (when tperteam != distance)
auto i = distance_it;
#pragma omp target teams distribute parallel for num_teams(numteams) \
schedule(static, 1) map(to : body,begin_it)
for (i = 0; i < distance_it; ++i) {
Body ib = body;
ib(begin_it[i]);
}
return resources::EventProxy<resources::Omp>(omp_res);
}
template <typename Iterable, typename Func, typename ForallParam>
RAJA_INLINE
concepts::enable_if_t<
resources::EventProxy<resources::Omp>,
RAJA::expt::type_traits::is_ForallParamPack<ForallParam>,
concepts::negate<RAJA::expt::type_traits::is_ForallParamPack_empty<ForallParam>>
>
forall_impl(resources::Omp omp_res,
const omp_target_parallel_for_exec_nt& p,
Iterable&& iter,
Func&& loop_body,
ForallParam f_params)
{
using EXEC_POL = typename std::decay<decltype(p)>::type;
RAJA::expt::ParamMultiplexer::init<EXEC_POL>(f_params);
RAJA_OMP_DECLARE_REDUCTION_COMBINE;
using Body = typename std::remove_reference<decltype(loop_body)>::type;
Body body = loop_body;
RAJA_EXTRACT_BED_IT(iter);
#pragma omp target teams distribute parallel for schedule(static, 1) \
firstprivate(body,begin_it) reduction(combine: f_params)
for (decltype(distance_it) i = 0; i < distance_it; ++i) {
Body ib = body;
RAJA::expt::invoke_body(f_params, ib, begin_it[i]);
}
RAJA::expt::ParamMultiplexer::resolve<EXEC_POL>(f_params);
return resources::EventProxy<resources::Omp>(omp_res);
}
template <typename Iterable, typename Func, typename ForallParam>
RAJA_INLINE
concepts::enable_if_t<
resources::EventProxy<resources::Omp>,
RAJA::expt::type_traits::is_ForallParamPack<ForallParam>,
RAJA::expt::type_traits::is_ForallParamPack_empty<ForallParam>
>
forall_impl(resources::Omp omp_res,
const omp_target_parallel_for_exec_nt&,
Iterable&& iter,
Func&& loop_body,
ForallParam)
{
using Body = typename std::remove_reference<decltype(loop_body)>::type;
Body body = loop_body;
RAJA_EXTRACT_BED_IT(iter);
#pragma omp target teams distribute parallel for schedule(static, 1) \
firstprivate(body,begin_it)
for (decltype(distance_it) i = 0; i < distance_it; ++i) {
Body ib = body;
ib(begin_it[i]);
}
return resources::EventProxy<resources::Omp>(omp_res);
}
} // namespace omp
} // namespace policy
} // namespace RAJA
#endif // closing endif for if defined(RAJA_TARGET_RAJA_ENABLE_OPENMP)
#endif // closing endif for header file include guard