version GIT relicensing-7278-g091132d5eb 2026-12-02 03:10:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
portable_hanging_nodes_internal.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2021 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_portable_hanging_nodes_internal_h
14#define dealii_portable_hanging_nodes_internal_h
15
16#include <deal.II/base/config.h>
17
19
20#include <Kokkos_Core.hpp>
21
22
24namespace Portable
25{
26 namespace internal
27 {
28 //------------------------------------------------------------------------//
29 // Functions for resolving the hanging node constraints on the GPU //
30 //------------------------------------------------------------------------//
31 template <unsigned int size>
32 DEAL_II_HOST_DEVICE inline unsigned int
33 index2(unsigned int i, unsigned int j)
34 {
35 return i + size * j;
36 }
37
38
39
40 template <unsigned int size>
41 DEAL_II_HOST_DEVICE inline unsigned int
42 index3(unsigned int i, unsigned int j, unsigned int k)
43 {
44 return i + size * j + size * size * k;
45 }
46
47
48
49 template <unsigned int fe_degree, unsigned int direction>
50 DEAL_II_HOST_DEVICE inline bool
52 const ::internal::MatrixFreeFunctions::ConstraintKinds
53 &constraint_mask,
54 const unsigned int x_idx,
55 const unsigned int y_idx)
56 {
57 return ((direction == 0) &&
58 (((constraint_mask & ::internal::MatrixFreeFunctions::
59 ConstraintKinds::subcell_y) !=
61 unconstrained) ?
62 (y_idx == 0) :
63 (y_idx == fe_degree))) ||
64 ((direction == 1) &&
65 (((constraint_mask & ::internal::MatrixFreeFunctions::
68 unconstrained) ?
69 (x_idx == 0) :
70 (x_idx == fe_degree)));
71 }
72
73 template <unsigned int fe_degree, unsigned int direction>
74 DEAL_II_HOST_DEVICE inline bool
76 const ::internal::MatrixFreeFunctions::ConstraintKinds
77 &constraint_mask,
78 const unsigned int x_idx,
79 const unsigned int y_idx,
80 const unsigned int z_idx,
81 const ::internal::MatrixFreeFunctions::ConstraintKinds face1_type,
82 const ::internal::MatrixFreeFunctions::ConstraintKinds face2_type,
83 const ::internal::MatrixFreeFunctions::ConstraintKinds face1,
84 const ::internal::MatrixFreeFunctions::ConstraintKinds face2,
85 const ::internal::MatrixFreeFunctions::ConstraintKinds edge)
86 {
87 const unsigned int face1_idx = (direction == 0) ? y_idx :
88 (direction == 1) ? z_idx :
89 x_idx;
90 const unsigned int face2_idx = (direction == 0) ? z_idx :
91 (direction == 1) ? x_idx :
92 y_idx;
93
94 const bool on_face1 = ((constraint_mask & face1_type) !=
95 ::internal::MatrixFreeFunctions::
96 ConstraintKinds::unconstrained) ?
97 (face1_idx == 0) :
98 (face1_idx == fe_degree);
99 const bool on_face2 = ((constraint_mask & face2_type) !=
100 ::internal::MatrixFreeFunctions::
101 ConstraintKinds::unconstrained) ?
102 (face2_idx == 0) :
103 (face2_idx == fe_degree);
104 return (
105 (((constraint_mask & face1) != ::internal::MatrixFreeFunctions::
106 ConstraintKinds::unconstrained) &&
107 on_face1) ||
108 (((constraint_mask & face2) != ::internal::MatrixFreeFunctions::
109 ConstraintKinds::unconstrained) &&
110 on_face2) ||
111 (((constraint_mask & edge) != ::internal::MatrixFreeFunctions::
112 ConstraintKinds::unconstrained) &&
113 on_face1 && on_face2));
114 }
115
116
117
133 template <int dim,
134 int fe_degree,
135 int direction,
136 bool transpose,
137 typename Number,
138 typename ViewType>
139 DEAL_II_HOST_DEVICE inline void
141 const Kokkos::TeamPolicy<
142 MemorySpace::Default::kokkos_space::execution_space>::member_type
143 &team_member,
144 Kokkos::View<Number *, MemorySpace::Default::kokkos_space>
145 constraint_weights,
146 const ::internal::MatrixFreeFunctions::ConstraintKinds
147 constraint_mask,
148 ViewType values)
149 {
150 static_assert(dim == 2 || dim == 3, "Only implemented for 2d and 3d");
151 static_assert(direction >= 0 && direction < dim,
152 "direction must be in [0, dim)");
153
154 using ConstraintKinds =
156
157 constexpr unsigned int n_dofs_1d = fe_degree + 1;
158
159 // lines along the direction per cell, and the stride along a line
160 constexpr unsigned int n_lines = Utilities::pow(n_dofs_1d, dim - 1);
161 constexpr unsigned int stride = Utilities::pow(n_dofs_1d, direction);
162
163 // The subcell bit of this direction selects between the weights and
164 // their point-reflected variant, the face (and edge) bits of the other
165 // directions whether the lines on those faces are constrained at all.
166 // face1_type and face2_type are only needed in 3d.
167 const auto this_type = (direction == 0) ? ConstraintKinds::subcell_x :
168 (direction == 1) ? ConstraintKinds::subcell_y :
169 ConstraintKinds::subcell_z;
170 [[maybe_unused]] const auto face1_type =
171 (direction == 0) ? ConstraintKinds::subcell_y :
172 (direction == 1) ? ConstraintKinds::subcell_z :
173 ConstraintKinds::subcell_x;
174 [[maybe_unused]] const auto face2_type =
175 (direction == 0) ? ConstraintKinds::subcell_z :
176 (direction == 1) ? ConstraintKinds::subcell_x :
177 ConstraintKinds::subcell_y;
178
179 const auto face1 = (direction == 0) ? ConstraintKinds::face_y :
180 (direction == 1) ? ConstraintKinds::face_z :
181 ConstraintKinds::face_x;
182 const auto face2 = (direction == 0) ? ConstraintKinds::face_z :
183 (direction == 1) ? ConstraintKinds::face_x :
184 ConstraintKinds::face_y;
185 const auto edge = (direction == 0) ? ConstraintKinds::edge_x :
186 (direction == 1) ? ConstraintKinds::edge_y :
187 ConstraintKinds::edge_z;
188 // in 2d the lines along x lie on the y faces and vice versa
189 const auto constrained_faces =
190 (dim == 2) ? ((direction == 0) ? ConstraintKinds::face_y :
191 ConstraintKinds::face_x) :
192 (face1 | face2 | edge);
193
194 // the same for all threads of the team, so they all skip the barrier
195 if ((constraint_mask & constrained_faces) ==
196 ConstraintKinds::unconstrained)
197 return;
198
199 Kokkos::parallel_for(
200 Kokkos::TeamThreadRange(team_member, n_lines),
201 [&](const unsigned int line) {
202 // indices of the line: zero along the direction, the others
203 // follow from the line index in increasing coordinate order
204 unsigned int x_idx = 0, y_idx = 0, z_idx = 0;
205 if constexpr (dim == 2)
206 {
207 if (direction == 0)
208 y_idx = line;
209 else
210 x_idx = line;
211 }
212 else
213 {
214 const unsigned int i1 = line % n_dofs_1d;
215 const unsigned int i2 = line / n_dofs_1d;
216 if (direction == 0)
217 {
218 y_idx = i1;
219 z_idx = i2;
220 }
221 else if (direction == 1)
222 {
223 x_idx = i1;
224 z_idx = i2;
225 }
226 else
227 {
228 x_idx = i1;
229 y_idx = i2;
230 }
231 }
232
233 bool constrained_line;
234 if constexpr (dim == 2)
235 constrained_line =
236 is_constrained_dof_2d<fe_degree, direction>(constraint_mask,
237 x_idx,
238 y_idx);
239 else
240 constrained_line =
241 is_constrained_dof_3d<fe_degree, direction>(constraint_mask,
242 x_idx,
243 y_idx,
244 z_idx,
245 face1_type,
246 face2_type,
247 face1,
248 face2,
249 edge);
250 if (!constrained_line)
251 return;
252
253 const unsigned int offset = (dim == 2) ?
254 index2<n_dofs_1d>(x_idx, y_idx) :
255 index3<n_dofs_1d>(x_idx, y_idx, z_idx);
256
257 // the line is read completely before it is written
258 Number line_values[n_dofs_1d];
259 for (unsigned int i = 0; i < n_dofs_1d; ++i)
260 line_values[i] = values(offset + i * stride);
261
262 const bool type =
263 (constraint_mask & this_type) != ConstraintKinds::unconstrained;
264
265 for (unsigned int j = 0; j < n_dofs_1d; ++j)
266 {
267 Number sum = 0.;
268 if (type)
269 for (unsigned int i = 0; i < n_dofs_1d; ++i)
270 {
271 const Number w = transpose ?
272 constraint_weights[i * n_dofs_1d + j] :
273 constraint_weights[j * n_dofs_1d + i];
274 sum += w * line_values[i];
275 }
276 else
277 for (unsigned int i = 0; i < n_dofs_1d; ++i)
278 {
279 const Number w =
280 transpose ?
281 constraint_weights[(fe_degree - i) * n_dofs_1d +
282 fe_degree - j] :
283 constraint_weights[(fe_degree - j) * n_dofs_1d +
284 fe_degree - i];
285 sum += w * line_values[i];
286 }
287 values(offset + j * stride) = sum;
288 }
289 });
290
291 team_member.team_barrier();
292 }
293
294
295
305 template <int dim,
306 int fe_degree,
307 bool transpose,
308 typename Number,
309 typename ViewType>
312 const Kokkos::TeamPolicy<
313 MemorySpace::Default::kokkos_space::execution_space>::member_type
314 &team_member,
315 Kokkos::View<Number *, MemorySpace::Default::kokkos_space>
316 constraint_weights,
317 const ::internal::MatrixFreeFunctions::ConstraintKinds
318 constraint_mask,
319 ViewType values)
320 {
321 if constexpr (dim == 2)
322 {
323 interpolate_boundary<2, fe_degree, 0, transpose>(team_member,
324 constraint_weights,
325 constraint_mask,
326 values);
327 interpolate_boundary<2, fe_degree, 1, transpose>(team_member,
328 constraint_weights,
329 constraint_mask,
330 values);
331 }
332 else if constexpr (dim == 3)
333 {
334 // Interpolate y and z faces (x-direction)
335 interpolate_boundary<3, fe_degree, 0, transpose>(team_member,
336 constraint_weights,
337 constraint_mask,
338 values);
339 // Interpolate x and z faces (y-direction)
340 interpolate_boundary<3, fe_degree, 1, transpose>(team_member,
341 constraint_weights,
342 constraint_mask,
343 values);
344 // Interpolate x and y faces (z-direction)
345 interpolate_boundary<3, fe_degree, 2, transpose>(team_member,
346 constraint_weights,
347 constraint_mask,
348 values);
349 }
350 }
351
352
353
354 namespace batched
355 {
361 using ConstraintMaskView = Kokkos::View<
362 const ::internal::MatrixFreeFunctions::ConstraintKinds *,
364
369 template <typename Number>
371 Kokkos::View<Number *,
372 MemorySpace::Default::kokkos_space::execution_space::
373 scratch_memory_space,
374 Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
375
376
377
391 template <int dim,
392 int fe_degree,
393 int direction,
394 bool transpose,
395 typename Number,
396 typename ViewType>
397 DEAL_II_HOST_DEVICE inline void
399 const Kokkos::TeamPolicy<
400 MemorySpace::Default::kokkos_space::execution_space>::member_type
401 &team_member,
402 const ConstraintWeightsView<Number> constraint_weights,
403 const ConstraintMaskView constraint_masks,
404 ViewType values,
405 const int batch_size = 1)
406 {
407 static_assert(dim == 2 || dim == 3, "Only implemented for 2d and 3d");
408 static_assert(direction >= 0 && direction < dim,
409 "direction must be in [0, dim)");
410
411 using ConstraintKinds =
413
414 constexpr unsigned int n_dofs_1d = fe_degree + 1;
415 constexpr unsigned int n_dofs = Utilities::pow(n_dofs_1d, dim);
416
417 // lines along the direction per cell, and the stride along a line
418 constexpr unsigned int n_lines = Utilities::pow(n_dofs_1d, dim - 1);
419 constexpr unsigned int stride = Utilities::pow(n_dofs_1d, direction);
420
421 Assert(batch_size >= 0, ExcInternalError());
422 Assert(constraint_masks.extent(0) >=
423 static_cast<std::size_t>(batch_size),
425 Assert(values.size() >= static_cast<std::size_t>(batch_size) * n_dofs,
427
428 // see interpolate_boundary() for the meaning of these flags
429 const auto this_type = (direction == 0) ? ConstraintKinds::subcell_x :
430 (direction == 1) ? ConstraintKinds::subcell_y :
431 ConstraintKinds::subcell_z;
432 [[maybe_unused]] const auto face1_type =
433 (direction == 0) ? ConstraintKinds::subcell_y :
434 (direction == 1) ? ConstraintKinds::subcell_z :
435 ConstraintKinds::subcell_x;
436 [[maybe_unused]] const auto face2_type =
437 (direction == 0) ? ConstraintKinds::subcell_z :
438 (direction == 1) ? ConstraintKinds::subcell_x :
439 ConstraintKinds::subcell_y;
440
441 const auto face1 = (direction == 0) ? ConstraintKinds::face_y :
442 (direction == 1) ? ConstraintKinds::face_z :
443 ConstraintKinds::face_x;
444 const auto face2 = (direction == 0) ? ConstraintKinds::face_z :
445 (direction == 1) ? ConstraintKinds::face_x :
446 ConstraintKinds::face_y;
447 const auto edge = (direction == 0) ? ConstraintKinds::edge_x :
448 (direction == 1) ? ConstraintKinds::edge_y :
449 ConstraintKinds::edge_z;
450 // in 2d the lines along x lie on the y faces and vice versa
451 const auto constrained_faces =
452 (dim == 2) ? ((direction == 0) ? ConstraintKinds::face_y :
453 ConstraintKinds::face_x) :
454 (face1 | face2 | edge);
455
456 // the same for all threads of the team, so they all skip the barrier
457 ConstraintKinds batch_mask = ConstraintKinds::unconstrained;
458 for (int cell = 0; cell < batch_size; ++cell)
459 batch_mask |= constraint_masks(cell);
460 if ((batch_mask & constrained_faces) == ConstraintKinds::unconstrained)
461 return;
462
463 Assert(constraint_weights.size() >= n_dofs_1d * n_dofs_1d,
465
466 Kokkos::parallel_for(
467 Kokkos::TeamVectorRange(team_member, batch_size * n_lines),
468 [&](const int tid) {
469 const int cell = tid / n_lines;
470 const unsigned int line = tid % n_lines;
471
472 const ConstraintKinds constraint_mask = constraint_masks(cell);
473 if ((constraint_mask & constrained_faces) ==
474 ConstraintKinds::unconstrained)
475 return;
476
477 // indices of the line: zero along the direction, the others
478 // follow from the line index in increasing coordinate order
479 unsigned int x_idx = 0, y_idx = 0, z_idx = 0;
480 if constexpr (dim == 2)
481 {
482 if (direction == 0)
483 y_idx = line;
484 else
485 x_idx = line;
486 }
487 else
488 {
489 const unsigned int i1 = line % n_dofs_1d;
490 const unsigned int i2 = line / n_dofs_1d;
491 if (direction == 0)
492 {
493 y_idx = i1;
494 z_idx = i2;
495 }
496 else if (direction == 1)
497 {
498 x_idx = i1;
499 z_idx = i2;
500 }
501 else
502 {
503 x_idx = i1;
504 y_idx = i2;
505 }
506 }
507
508 bool constrained_line;
509 if constexpr (dim == 2)
510 constrained_line =
511 is_constrained_dof_2d<fe_degree, direction>(constraint_mask,
512 x_idx,
513 y_idx);
514 else
515 constrained_line =
516 is_constrained_dof_3d<fe_degree, direction>(constraint_mask,
517 x_idx,
518 y_idx,
519 z_idx,
520 face1_type,
521 face2_type,
522 face1,
523 face2,
524 edge);
525 if (!constrained_line)
526 return;
527
528 const unsigned int offset =
529 cell * n_dofs + ((dim == 2) ?
530 index2<n_dofs_1d>(x_idx, y_idx) :
531 index3<n_dofs_1d>(x_idx, y_idx, z_idx));
532
533 // the line is read completely before it is written
534 Number line_values[n_dofs_1d];
535 for (unsigned int i = 0; i < n_dofs_1d; ++i)
536 line_values[i] = values(offset + i * stride);
537
538 const bool type =
539 (constraint_mask & this_type) != ConstraintKinds::unconstrained;
540
541 for (unsigned int j = 0; j < n_dofs_1d; ++j)
542 {
543 Number sum = 0.;
544 if (type)
545 for (unsigned int i = 0; i < n_dofs_1d; ++i)
546 {
547 const Number w = transpose ?
548 constraint_weights[i * n_dofs_1d + j] :
549 constraint_weights[j * n_dofs_1d + i];
550 sum += w * line_values[i];
551 }
552 else
553 for (unsigned int i = 0; i < n_dofs_1d; ++i)
554 {
555 const Number w =
556 transpose ?
557 constraint_weights[(fe_degree - i) * n_dofs_1d +
558 fe_degree - j] :
559 constraint_weights[(fe_degree - j) * n_dofs_1d +
560 fe_degree - i];
561 sum += w * line_values[i];
562 }
563 values(offset + j * stride) = sum;
564 }
565 });
566
567 team_member.team_barrier();
568 }
569
570
571
583 template <int dim,
584 int fe_degree,
585 bool transpose,
586 typename Number,
587 typename ViewType>
590 const Kokkos::TeamPolicy<
591 MemorySpace::Default::kokkos_space::execution_space>::member_type
592 &team_member,
593 const ConstraintWeightsView<Number> constraint_weights,
594 const ConstraintMaskView constraint_masks,
595 ViewType values,
596 const int batch_size = 1)
597 {
598 if constexpr (dim == 2)
599 {
600 interpolate_boundary<2, fe_degree, 0, transpose>(team_member,
601 constraint_weights,
602 constraint_masks,
603 values,
604 batch_size);
605 interpolate_boundary<2, fe_degree, 1, transpose>(team_member,
606 constraint_weights,
607 constraint_masks,
608 values,
609 batch_size);
610 }
611 else if constexpr (dim == 3)
612 {
613 // Interpolate y and z faces (x-direction)
614 interpolate_boundary<3, fe_degree, 0, transpose>(team_member,
615 constraint_weights,
616 constraint_masks,
617 values,
618 batch_size);
619 // Interpolate x and z faces (y-direction)
620 interpolate_boundary<3, fe_degree, 1, transpose>(team_member,
621 constraint_weights,
622 constraint_masks,
623 values,
624 batch_size);
625 // Interpolate x and y faces (z-direction)
626 interpolate_boundary<3, fe_degree, 2, transpose>(team_member,
627 constraint_weights,
628 constraint_masks,
629 values,
630 batch_size);
631 }
632 }
633 } // namespace batched
634 } // namespace internal
635} // namespace Portable
636
638#endif
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_HOST_DEVICE
Definition config.h:171
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
DerivativeForm< 1, spacedim, dim, Number > transpose(const DerivativeForm< 1, dim, spacedim, Number > &DF)
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
std::size_t size
Definition mpi.cc:733
void resolve_hanging_nodes(const Kokkos::TeamPolicy< MemorySpace::Default::kokkos_space::execution_space >::member_type &team_member, const ConstraintWeightsView< Number > constraint_weights, const ConstraintMaskView constraint_masks, ViewType values, const int batch_size=1)
Kokkos::View< const ::internal::MatrixFreeFunctions::ConstraintKinds *, MemorySpace::Default::kokkos_space > ConstraintMaskView
Kokkos::View< Number *, MemorySpace::Default::kokkos_space::execution_space::scratch_memory_space, Kokkos::MemoryTraits< Kokkos::Unmanaged > > ConstraintWeightsView
void interpolate_boundary(const Kokkos::TeamPolicy< MemorySpace::Default::kokkos_space::execution_space >::member_type &team_member, const ConstraintWeightsView< Number > constraint_weights, const ConstraintMaskView constraint_masks, ViewType values, const int batch_size=1)
bool is_constrained_dof_2d(const ::internal::MatrixFreeFunctions::ConstraintKinds &constraint_mask, const unsigned int x_idx, const unsigned int y_idx)
bool is_constrained_dof_3d(const ::internal::MatrixFreeFunctions::ConstraintKinds &constraint_mask, const unsigned int x_idx, const unsigned int y_idx, const unsigned int z_idx, const ::internal::MatrixFreeFunctions::ConstraintKinds face1_type, const ::internal::MatrixFreeFunctions::ConstraintKinds face2_type, const ::internal::MatrixFreeFunctions::ConstraintKinds face1, const ::internal::MatrixFreeFunctions::ConstraintKinds face2, const ::internal::MatrixFreeFunctions::ConstraintKinds edge)
unsigned int index3(unsigned int i, unsigned int j, unsigned int k)
void resolve_hanging_nodes(const Kokkos::TeamPolicy< MemorySpace::Default::kokkos_space::execution_space >::member_type &team_member, Kokkos::View< Number *, MemorySpace::Default::kokkos_space > constraint_weights, const ::internal::MatrixFreeFunctions::ConstraintKinds constraint_mask, ViewType values)
void interpolate_boundary(const Kokkos::TeamPolicy< MemorySpace::Default::kokkos_space::execution_space >::member_type &team_member, Kokkos::View< Number *, MemorySpace::Default::kokkos_space > constraint_weights, const ::internal::MatrixFreeFunctions::ConstraintKinds constraint_mask, ViewType values)
unsigned int index2(unsigned int i, unsigned int j)
constexpr T pow(const T base, const int iexp)
Definition utilities.h:966
::Kokkos::DefaultExecutionSpace::memory_space kokkos_space