Nektar++
Loading...
Searching...
No Matches
AssmbScatrDeviceKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: AssmbScatrDeviceKernels.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Device kernels of the assemble-scatter operator
32///////////////////////////////////////////////////////////////////////////////
33
34/**
35 * @file AssmbScatrDeviceKernels.hpp
36 * @brief Device-side kernels, and their host-side launch wrappers, for
37 * the AssmbScatr assemble--scatter operator (CUDA, HIP and SYCL
38 * back-ends).
39 *
40 * @details
41 * ### What this file provides
42 * The AssmbScatr operator makes a continuous-Galerkin coefficient field
43 * globally consistent: for every global degree of freedom (DOF) shared by
44 * several elements it sums ("assembles") all local contributions and
45 * writes ("scatters") the assembled value back over each local copy, in
46 * place. This file holds the Device execution-space implementation; the
47 * Serial and AVX counterparts live in AssmbScatrSerialAVXKernels.hpp.
48 * Every back-end exposes wrapper functions with identical signatures,
49 * selected by SFINAE on the ExecSpace template argument, so
50 * AssmbScatrOpImpl can call AssembleScatrKernel<ExecSpace>(...) without
51 * knowing which back-end it is driving.
52 *
53 * The whole file is guarded by NEKTAR_ENABLE_DEVICE and
54 * DEVICE_COMPILE_ONLY (see LibUtilities/Backends/Backends.hpp), so its
55 * contents are compiled only when a device back-end is enabled. Under
56 * CUDA and HIP the latter macro is set only in translation units
57 * compiled by the CUDA/HIP compiler; under SYCL it is defined
58 * build-wide, so every translation unit sees the kernels.
59 * Host-only builds see an empty namespace.
60 *
61 * ### The three kernels and the multi-rank flow
62 * On a single device AssembleScatrKernel does the whole job. Across
63 * several ranks AssmbScatrOpImpl::v_Apply overlaps communication with
64 * computation (see AssmbScatrOpImpl.hpp):
65 * -# AssembleScatrBndKernel forms the rank-local partial sum of every
66 * partition-boundary DOF and packs it into the send buffer;
67 * -# while AssemblyComm exchanges buffers (BeginComm()/EndComm()), the
68 * interior AssembleScatrKernel processes the DOFs whose
69 * contributions reside entirely on this device (those the map
70 * builder selected as needing work -- see the kernel's own notes);
71 * -# AssembleFromBndKernel folds the received partial sums into the
72 * local one, in a globally agreed rank order, and scatters the
73 * result.
74 *
75 * ### Map layout
76 * All kernels are driven by integer maps built by
77 * MultiRegions::LocalToGlobalDataCreator (see
78 * MultiRegions/DataWarehouse/LocalToGlobalDataWarehouseDef.hpp) and
79 * cached in the data warehouse. Two layouts are in use:
80 * - The interior index/sign maps are interleaved with stride
81 * NektarSpaces::Device::warpSize so that consecutive threads of a warp
82 * read consecutive memory locations (coalesced access). The kernel
83 * bakes the same constant in, so kernel and map builder agree by
84 * construction. The DOFs are ordered by the position of their first
85 * local copy in the field, at its interleave widths, so consecutive
86 * threads also address neighbouring coefficients whether the field is
87 * interleaved or not.
88 * - The boundary maps are contiguous: each DOF owns a run of nassemble
89 * local-coefficient indices followed by nbndvals communication-buffer
90 * slot indices.
91 *
92 * ### Sign entries
93 * Sign entries take the values -1, 0 or +1. The +/-1 factors come from
94 * the assembly map's local-to-global sign data (AssemblyMapCG) and
95 * reconcile the differing sign conventions of coincident local DOFs on
96 * neighbouring elements; they are applied on gather and again on
97 * scatter, so each local coefficient's own convention is restored. A 0
98 * masks an entry completely -- it contributes nothing when gathered and
99 * zeroes the local value when scattered -- which is how the
100 * zero-Dirichlet operator variant annihilates Dirichlet DOFs and how
101 * padding entries of the interleaved layout are kept harmless.
102 */
103
104#pragma once
105
107
109{
110
111#if defined(NEKTAR_ENABLE_DEVICE) && defined(DEVICE_COMPILE_ONLY)
112/**
113 * @brief Assemble and scatter, in place, the global DOFs whose local
114 * contributions all reside on this device and which need work.
115 *
116 * One grid-stride iteration handles one assembly target -- one global
117 * DOF of one field component. Not every device-interior DOF appears:
118 * the map builder omits targets with a single local contribution
119 * (assemble--scatter is the identity on them) and keeps targets with
120 * more than one, plus -- when the maps are built for the zero-Dirichlet
121 * variant -- global Dirichlet targets of any valence, included so that
122 * their local copies are zeroed. The first inner loop gathers the
123 * signed sum of the target's nassemble[idx] local coefficients; the
124 * second overwrites each of those coefficients with the sign-weighted
125 * assembled value, i.e. the sum expressed in that coefficient's own
126 * sign convention. Distinct targets own disjoint sets of local
127 * coefficients, so threads never write to the same location.
128 *
129 * The index and sign arrays are interleaved: target idx's j-th entry
130 * sits at offset[idx] + j * warpSize, where warpSize is the compile-time
131 * constant NektarSpaces::Device::warpSize. Consecutive targets map to
132 * consecutive threads, so a warp reads index/sign contiguously
133 * (coalesced). The maps must have been built with the same interleave
134 * width; AssmbScatrOpImpl guarantees this by keying the data warehouse
135 * with NektarSpaces::vector_width<ExecSpace, TData>, which equals
136 * warpSize for the Device space.
137 *
138 * @tparam TthreadBlock Back-end thread-block handle type (appended by
139 * the launcher macro, e.g. hipcudaBlock<1>).
140 * @tparam TData Coefficient data type (float or double).
141 *
142 * @param nvals Number of assembly targets, i.e. device-interior
143 * (global DOF, component) pairs selected by the
144 * map builder as needing work (see above).
145 * @param nassemble Per-target number of local coefficients mapping
146 * to the target (its valence).
147 * @param index Interleaved positions of those coefficients in
148 * @p inoutptr.
149 * @param offset Per-target start position of its entries in
150 * @p index and @p sign.
151 * @param sign Interleaved factors in {-1, 0, +1}; see the
152 * file-level notes on sign entries.
153 * @param inoutptr Coefficient array in the layout @p index
154 * addresses; read and overwritten in place.
155 * @param threadBlock Thread-block handle used by getGlobalIdx() and
156 * getGlobalRange() to form the grid-stride loop.
157 */
158template <typename TthreadBlock, typename TData>
160 const unsigned nvals, const unsigned *nassemble, const unsigned *index,
161 const unsigned *offset, const int8_t *sign, TData *inoutptr,
162 const TthreadBlock &threadBlock)
163{
164 constexpr unsigned int warpSize = NektarSpaces::Device::warpSize;
165
166 const unsigned idx0 = getGlobalIdx(threadBlock);
167 const unsigned stride = getGlobalRange(threadBlock);
168
169 for (unsigned idx = idx0; idx < nvals; idx += stride)
170 {
171 TData ass = 0;
172 const unsigned ioffset = offset[idx];
173 const unsigned nassemb = nassemble[idx];
174 for (unsigned j = 0; j < nassemb; ++j)
175 {
176 const unsigned ind = ioffset + j * warpSize;
177 ass += inoutptr[index[ind]] * sign[ind];
178 }
179
180 for (unsigned j = 0; j < nassemb; ++j)
181 {
182 const unsigned ind = ioffset + j * warpSize;
183 inoutptr[index[ind]] = ass * sign[ind];
184 }
185 }
186}
187
188/**
189 * @brief Form the rank-local partial sum of each partition-boundary DOF
190 * and pack it into the communication send buffer.
191 *
192 * This is stage one of the multi-rank assembly (see the file-level
193 * overview). For each boundary target the kernel gathers the signed sum
194 * of its nassemble[idx] local coefficients, then:
195 * - parks the partial sum in the first local coefficient only, stored
196 * in that coefficient's own sign convention: sign[offset[idx]] is
197 * applied on park and re-applied on retrieval, which restores the sum
198 * for the +/-1 factors, while a 0 factor (zero-Dirichlet variant)
199 * masks a partial sum that is itself already 0, so the round trip
200 * stays consistent. AssembleFromBndKernel picks the parked value up
201 * from there after communication;
202 * - writes the plain (globally sign-corrected) partial sum into each of
203 * the target's nbndvals[idx] send-buffer slots, one per exchange entry
204 * involving this DOF.
205 * The remaining local coefficients keep their raw values and are only
206 * made consistent by AssembleFromBndKernel, so the field is in a mixed
207 * state between the two kernels. That is safe because the interior
208 * AssembleScatrKernel, which runs in between, touches a disjoint set of
209 * DOFs.
210 *
211 * Unlike the interior kernel, the boundary maps are contiguous rather
212 * than warp-interleaved: target idx owns entries [offset[idx],
213 * offset[idx] + nassemble[idx] + nbndvals[idx]) of @p index, with the
214 * local-coefficient entries first and the buffer-slot entries after.
215 *
216 * @tparam TthreadBlock Back-end thread-block handle type (appended by
217 * the launcher macro, e.g. hipcudaBlock<1>).
218 * @tparam TData Coefficient data type (float or double).
219 *
220 * @param nvals Number of partition-boundary assembly targets,
221 * i.e. (global DOF, component) pairs shared with
222 * other ranks.
223 * @param nassemble Per-target number of local coefficient
224 * contributions; the map builder always provides at
225 * least one (the parking slot).
226 * @param nbndvals Per-target number of send-buffer slots to fill.
227 * @param index Contiguous map: per target, nassemble positions
228 * in @p inoutptr followed by nbndvals positions in
229 * @p bndptr.
230 * @param offset Per-target start position within @p index and
231 * @p sign.
232 * @param sign Factors in {-1, 0, +1} for the local-coefficient
233 * entries; the trailing buffer-slot entries are
234 * zero-filled and never read.
235 * @param inoutptr Coefficient array; only the
236 * first local coefficient per target is updated.
237 * @param bndptr Send buffer of the assembly communicator; write
238 * only.
239 * @param threadBlock Thread-block handle used by getGlobalIdx() and
240 * getGlobalRange() to form the grid-stride loop.
241 */
242template <typename TthreadBlock, typename TData>
244 const unsigned nvals, const unsigned *nassemble, const unsigned *nbndvals,
245 const unsigned *index, const unsigned *offset, const int8_t *sign,
246 TData *inoutptr, TData *bndptr, const TthreadBlock &threadBlock)
247{
248 const unsigned idx0 = getGlobalIdx(threadBlock);
249 const unsigned stride = getGlobalRange(threadBlock);
250
251 for (unsigned idx = idx0; idx < nvals; idx += stride)
252 {
253 TData ass = 0;
254 const unsigned ioffset = offset[idx];
255 const unsigned nidx = nassemble[idx];
256 const unsigned nbnd = nbndvals[idx];
257
258 // assemble values
259 for (unsigned j = 0; j < nidx; ++j)
260 {
261 const unsigned ind = ioffset + j;
262 ass += inoutptr[index[ind]] * sign[ind];
263 }
264
265 // copy one assembled values back to local values
266 inoutptr[index[ioffset]] = ass * sign[ioffset];
267
268 // put assembled values into boudnary array
269 for (unsigned j = 0; j < nbnd; ++j)
270 {
271 const unsigned ind = ioffset + nidx + j;
272 bndptr[index[ind]] = ass;
273 }
274 }
275}
276
277/**
278 * @brief Complete the assembly of partition-boundary DOFs from received
279 * partial sums and scatter the result to all local coefficients.
280 *
281 * Runs after AssemblyComm::EndComm() has filled @p bndptr with the
282 * partial sums computed by the other sharing ranks. For each target the
283 * summation follows a globally agreed rank order: the norder[idx]
284 * received values originating from ranks numbered below this one are
285 * added first, then this rank's own partial sum (parked in the first
286 * local coefficient by AssembleScatrBndKernel and recovered by
287 * re-applying its sign), then the remaining received values. The map
288 * builder stores the buffer slots in ascending source-rank order
289 * precisely so that every rank folds the same values in the same
290 * sequence and therefore computes an identical result for a shared DOF,
291 * not merely one that agrees to round-off. Finally the assembled value
292 * is scattered, with signs, over all nassemble[idx] local coefficients.
293 *
294 * The map layout is the contiguous boundary layout described in
295 * AssembleScatrBndKernel; the same nassemble/nbndvals/index/offset/sign
296 * arrays are passed to both kernels.
297 *
298 * @tparam TthreadBlock Back-end thread-block handle type (appended by
299 * the launcher macro, e.g. hipcudaBlock<1>).
300 * @tparam TData Coefficient data type (float or double).
301 *
302 * @param nvals Number of partition-boundary assembly targets.
303 * @param nassemble Per-target number of local coefficients to
304 * scatter to.
305 * @param nbndvals Per-target number of receive-buffer slots to
306 * fold in.
307 * @param index Contiguous map: per target, nassemble positions
308 * in @p inoutptr followed by nbndvals positions in
309 * @p bndptr.
310 * @param offset Per-target start position within @p index and
311 * @p sign.
312 * @param sign Factors in {-1, 0, +1} for the local-coefficient
313 * entries.
314 * @param norder Per-target position of this rank's own partial
315 * sum in the rank-ordered summation, i.e. the
316 * number of sharing ranks with a lower rank id.
317 * @param bndptr Receive buffer holding the other ranks' partial
318 * sums; read only.
319 * @param inoutptr Coefficient array; the parked
320 * partial sum is read from it and the final values
321 * are written back.
322 * @param threadBlock Thread-block handle used by getGlobalIdx() and
323 * getGlobalRange() to form the grid-stride loop.
324 */
325template <typename TthreadBlock, typename TData>
327 const unsigned nvals, const unsigned *nassemble, const unsigned *nbndvals,
328 const unsigned *index, const unsigned *offset, const int8_t *sign,
329 const unsigned *norder, const TData *bndptr, TData *inoutptr,
330 const TthreadBlock &threadBlock)
331{
332 const unsigned idx0 = getGlobalIdx(threadBlock);
333 const unsigned stride = getGlobalRange(threadBlock);
334
335 for (unsigned idx = idx0; idx < nvals; idx += stride)
336 {
337 TData ass = 0;
338 const unsigned ioffset = offset[idx];
339 const unsigned nidx = nassemble[idx];
340 const unsigned nbnd = nbndvals[idx];
341 const unsigned nord = norder[idx];
342
343 // assemble bndptr components wtih local ids
344 for (unsigned j = 0; j < nord; ++j)
345 {
346 const unsigned ind = ioffset + nidx + j;
347 ass += bndptr[index[ind]];
348 }
349
350 // add in local point in rank ordered assembly
351 ass += inoutptr[index[ioffset]] * sign[ioffset];
352
353 // assemble rest of points from where we left off
354 for (unsigned j = nord; j < nbnd; ++j)
355 {
356 const unsigned ind = ioffset + nidx + j;
357 ass += bndptr[index[ind]];
358 }
359
360 // copy rank assembled values back to local values
361 for (unsigned j = 0; j < nidx; ++j)
362 {
363 const unsigned ind = ioffset + j;
364 inoutptr[index[ind]] = ass * sign[ind];
365 }
366 }
367}
368
369/**
370 * @brief Launch the interior assemble--scatter kernel on the device.
371 *
372 * Device execution-space overload, selected by SFINAE against the Serial
373 * and AVX overloads in AssmbScatrSerialAVXKernels.hpp. Launches a 1-D
374 * grid of Device::defaultBlockSize-thread blocks, sized to give one
375 * thread per assembly target, on stream @p streamID. The other arguments are
376 * forwarded unchanged to the device kernel above, which documents their
377 * meanings and the required interleaved map layout.
378 */
379template <
380 typename ExecSpace, typename TData,
381 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>, bool>
382 Enable = true>
384 const unsigned nvals, const unsigned *nassemble, const unsigned *index,
385 const unsigned *offset, const int8_t *sign, TData *inoutptr,
386 const unsigned int streamID)
387{
388 const unsigned blockSize = NektarSpaces::Device::defaultBlockSize;
389 const unsigned gridSize = (nvals + blockSize - 1) / blockSize;
390
391 DEVICE_1DGRID_KERNEL_LAUNCHER_NOSHMEM(AssembleScatrKernel, gridSize,
392 blockSize, streamID, nvals, nassemble,
393 index, offset, sign, inoutptr);
394}
395
396/**
397 * @brief Launch the boundary send-buffer kernel on the device.
398 *
399 * Device execution-space overload, selected by SFINAE against the
400 * Serial/AVX overload in AssmbScatrSerialAVXKernels.hpp. Launches a 1-D
401 * grid of Device::defaultBlockSize-thread blocks, sized to give one
402 * thread per boundary target, on stream @p streamID. The other arguments are
403 * forwarded unchanged to the device kernel above, which documents their
404 * meanings and the contiguous boundary map layout. The caller is
405 * responsible for making @p bndptr visible to the communication layer
406 * afterwards (see AssmbScatrOpImpl::v_Apply).
407 */
408template <
409 typename ExecSpace, typename TData,
410 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>, bool>
411 Enable = true>
413 const unsigned nvals, const unsigned *nassemble, const unsigned *nbndvals,
414 const unsigned *index, const unsigned *offset, const int8_t *sign,
415 TData *inoutptr, TData *bndptr, const unsigned int streamID)
416{
417 const unsigned blockSize = NektarSpaces::Device::defaultBlockSize;
418 const unsigned gridSize = (nvals + blockSize - 1) / blockSize;
419
420 DEVICE_1DGRID_KERNEL_LAUNCHER_NOSHMEM(
421 AssembleScatrBndKernel<>, gridSize, blockSize, streamID, nvals,
422 nassemble, nbndvals, index, offset, sign, inoutptr, bndptr);
423}
424
425/**
426 * @brief Launch the boundary receive-side assembly kernel on the device.
427 *
428 * Device execution-space overload, selected by SFINAE against the
429 * Serial/AVX overload in AssmbScatrSerialAVXKernels.hpp. Launches a 1-D
430 * grid of Device::defaultBlockSize-thread blocks, sized to give one
431 * thread per boundary target, on stream @p streamID. The other arguments are
432 * forwarded unchanged to the device kernel above. Must only run once
433 * communication has completed and @p bndptr holds the received partial
434 * sums in the memory space the kernel executes in (see
435 * AssmbScatrOpImpl::v_Apply for the synchronisation logic).
436 */
437template <
438 typename ExecSpace, typename TData,
439 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>, bool>
440 Enable = true>
442 const unsigned nvals, const unsigned *nassemble, const unsigned *nbndvals,
443 const unsigned *index, const unsigned *offset, const int8_t *sign,
444 const unsigned *norder, const TData *bndptr, TData *inoutptr,
445 const unsigned int streamID)
446{
447 const unsigned blockSize = NektarSpaces::Device::defaultBlockSize;
448 const unsigned gridSize = (nvals + blockSize - 1) / blockSize;
449
450 DEVICE_1DGRID_KERNEL_LAUNCHER_NOSHMEM(
451 AssembleFromBndKernel<>, gridSize, blockSize, streamID, nvals,
452 nassemble, nbndvals, index, offset, sign, norder, bndptr, inoutptr);
453}
454#endif
455
456} // namespace Nektar::MultiRegions::detail
#define NEK_DEVICE_KERNEL
Definition Backends.hpp:297
#define NEK_FORCE_INLINE
Definition NekInline.hpp:46
#define sign(a, b)
return the sign(b)*a
Definition Polylib.cpp:47
static NEK_FORCE_INLINE void AssembleScatrBndKernel(const unsigned nvals, const unsigned *nassemble, const unsigned *nbndvals, const unsigned *index, const unsigned *offset, const int8_t *sign, TData *inoutptr, TData *bndptr, const unsigned int streamID)
Partially assemble the parallel-boundary global DOFs and pack the partial sums into the communication...
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT const TData *NEK_RESTRICT TData *NEK_RESTRICT const bool const bool unsigned char const TthreadBlock & threadBlock
static NEK_FORCE_INLINE void AssembleFromBndKernel(const unsigned nvals, const unsigned *nassemble, const unsigned *nbndvals, const unsigned *index, const unsigned *offset, const int8_t *sign, const unsigned *norder, const TData *bndptr, TData *inoutptr, const unsigned int streamID)
Complete the assembly of the parallel-boundary global DOFs from the received partial sums and scatter...
static NEK_FORCE_INLINE void AssembleScatrKernel(const unsigned nvals, const unsigned *nassemble, const unsigned *index, const unsigned *offset, const int8_t *sign, TData *inoutptr, const unsigned int streamID)
Assemble and scatter the device-interior global DOFs; AVX variant reading SIMD-interleaved map data.
static constexpr unsigned int defaultBlockSize
Definition Backends.hpp:141
static constexpr unsigned int warpSize
Definition Backends.hpp:143