flow
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
mac_reductions.hpp
Go to the documentation of this file.
1
9#ifndef PECLET_FLOW_MAC_REDUCTIONS_HPP
10#define PECLET_FLOW_MAC_REDUCTIONS_HPP
11
12#include <cstddef>
13#include <Kokkos_Core.hpp>
14#include <Kokkos_MathematicalFunctions.hpp>
15
16namespace peclet::flow {
17
18using Exec = Kokkos::DefaultExecutionSpace;
19using Mem = Exec::memory_space;
20using DField = Kokkos::View<double*, Mem>; // flat extended-block cell field (x-fastest)
21using DConst = Kokkos::View<const double*, Mem>;
22
23struct Ext3 {
24 int x, y, z;
25};
26
28struct SumMax {
29 double sum = 0.0;
30 double maxabs = 0.0;
31 KOKKOS_INLINE_FUNCTION SumMax& operator+=(const SumMax& o) {
32 sum += o.sum;
33 maxabs = (o.maxabs > maxabs) ? o.maxabs : maxabs;
34 return *this;
35 }
36};
37
38// Map an inner-cell linear index c to the extended-block flat index (x-fastest, +ghost offset).
39KOKKOS_INLINE_FUNCTION std::size_t innerToExt(long c, Ext3 ext, int ghost, Ext3 inner) {
40 const int ix = static_cast<int>(c % inner.x);
41 const int iy = static_cast<int>((c / inner.x) % inner.y);
42 const int iz = static_cast<int>(c / (static_cast<long>(inner.x) * inner.y));
43 return static_cast<std::size_t>(ix + ghost) + static_cast<std::size_t>(iy + ghost) * ext.x +
44 static_cast<std::size_t>(iz + ghost) * static_cast<std::size_t>(ext.x) * ext.y;
45}
46
48inline SumMax localSumMax(DConst f, Ext3 ext, int ghost, Ext3 inner) {
49 const long n = static_cast<long>(inner.x) * inner.y * inner.z;
50 SumMax r;
51 if (n <= 0)
52 return r;
53 Kokkos::parallel_reduce(
54 "peclet::flow::sum_max", Kokkos::RangePolicy<Exec>(0, n),
55 KOKKOS_LAMBDA(long c, SumMax& acc) {
56 const double v = f(innerToExt(c, ext, ghost, inner));
57 acc.sum += v;
58 const double a = Kokkos::fabs(v);
59 if (a > acc.maxabs)
60 acc.maxabs = a;
61 },
62 r);
63 return r;
64}
65
67inline double localDot(DConst a, DConst b, Ext3 ext, int ghost, Ext3 inner) {
68 const long n = static_cast<long>(inner.x) * inner.y * inner.z;
69 double s = 0.0;
70 if (n <= 0)
71 return s;
72 Kokkos::parallel_reduce(
73 "peclet::flow::dot", Kokkos::RangePolicy<Exec>(0, n),
74 KOKKOS_LAMBDA(long c, double& acc) {
75 const std::size_t i = innerToExt(c, ext, ghost, inner);
76 acc += a(i) * b(i);
77 },
78 s);
79 return s;
80}
81
83inline void subtractAll(DField f, double m) {
84 const std::size_t n = f.extent(0);
85 Kokkos::parallel_for(
86 "peclet::flow::subtract", Kokkos::RangePolicy<Exec>(0, n),
87 KOKKOS_LAMBDA(std::size_t i) { f(i) -= m; });
88}
89
90} // namespace peclet::flow
91
92#endif // PECLET_FLOW_MAC_REDUCTIONS_HPP
double localDot(DConst a, DConst b, Ext3 ext, int ghost, Ext3 inner)
Local inner product <a,b> over the inner cells.
Kokkos::DefaultExecutionSpace Exec
Kokkos::View< double *, Mem > DField
Exec::memory_space Mem
SumMax localSumMax(DConst f, Ext3 ext, int ghost, Ext3 inner)
Local sum and max|.| over the inner cells.
void subtractAll(DField f, double m)
Subtract a constant from EVERY cell of the extended block (the mean-removal scatter).
Kokkos::View< const double *, Mem > DConst
std::size_t innerToExt(long c, Ext3 ext, int ghost, Ext3 inner)
{sum, max|.|} reduction value.
SumMax & operator+=(const SumMax &o)