Mallard 0.8.0
Unstructured, GPU accelerated, high-order finite volume CFD solver
Mallard website
Loading...
Searching...
No Matches
lanes.h
Go to the documentation of this file.
1
14#ifndef CHEMISTRY_LANES_H
15#define CHEMISTRY_LANES_H
16
17#include <cstdint>
18
19#include <Kokkos_Core.hpp>
20
21namespace chemistry {
22
24template <bool ColumnMajor>
26 return ColumnMajor ? c * n + r : r * n + c;
27}
28
39 static constexpr uint32_t lanes = 1;
40 static constexpr bool column_major = false; // dense matrices: rows contiguous for one thread's inner loops
41 static constexpr bool parallel = false; // loops that balance work across lanes are not worth it
42
43 template <typename F>
44 KOKKOS_INLINE_FUNCTION void for_each(const uint32_t n, const F & f) const {
45 for (uint32_t i = 0; i < n; i++) f(i);
46 }
47
49
50 template <typename F>
51 KOKKOS_INLINE_FUNCTION void single(const F & f) const {
52 f();
53 }
54
56 template <typename F>
57 KOKKOS_INLINE_FUNCTION double sum(const uint32_t n, const F & f) const {
58 double s = 0.0;
59 for (uint32_t i = 0; i < n; i++) s += f(i);
60 return s;
61 }
62
64 template <typename F>
65 KOKKOS_INLINE_FUNCTION double argmax_abs(const uint32_t first, const uint32_t n, const F & f,
66 uint32_t & index) const {
67 index = first;
68 double largest = Kokkos::fabs(f(first));
69 for (uint32_t i = first + 1; i < n; i++) {
70 const double v = Kokkos::fabs(f(i));
71 if (v > largest) {
72 largest = v;
73 index = i;
74 }
75 }
76 return largest;
77 }
78};
79
88template <typename Member>
89struct TeamLanes {
90 static constexpr bool column_major = true; // dense matrices: a column's rows contiguous across lanes
91 static constexpr bool parallel = true; // balance work across lanes (e.g. Jacobian entries, not rows)
92 static constexpr uint32_t SUM_SHARES = 32;
93 const Member & member;
95 double * partial; // (lanes)
96 uint32_t * where; // (lanes)
97
98 KOKKOS_INLINE_FUNCTION static constexpr uint32_t shares(const uint32_t L) { return L > SUM_SHARES ? L : SUM_SHARES; }
99 static constexpr size_t scratch_bytes(const uint32_t L) {
100 return shares(L) * sizeof(double) + L * sizeof(uint32_t) + 16;
101 }
102
103 KOKKOS_INLINE_FUNCTION TeamLanes(const Member & m, const uint32_t L) : member(m), lanes(L) {
104 partial = static_cast<double *>(m.team_scratch(0).get_shmem(shares(L) * sizeof(double)));
105 where = static_cast<uint32_t *>(m.team_scratch(0).get_shmem(L * sizeof(uint32_t)));
106 }
107
108 template <typename F>
109 KOKKOS_INLINE_FUNCTION void for_each(const uint32_t n, const F & f) const {
110 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, n), f);
111 }
112
113 KOKKOS_INLINE_FUNCTION void sync() const { member.team_barrier(); }
114
115 template <typename F>
116 KOKKOS_INLINE_FUNCTION void single(const F & f) const {
117 Kokkos::single(Kokkos::PerTeam(member), f);
118 }
119
120 template <typename F>
121 KOKKOS_INLINE_FUNCTION double sum(const uint32_t n, const F & f) const {
122 constexpr uint32_t S = SUM_SHARES;
123 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, S), [&](const uint32_t l) {
124 double s = 0.0;
125 for (uint32_t i = l; i < n; i += S) s += f(i);
126 partial[l] = s;
127 });
128 member.team_barrier();
129 double s = 0.0;
130 for (uint32_t l = 0; l < S; l++) s += partial[l];
131 member.team_barrier();
132 return s;
133 }
134
135 template <typename F>
136 KOKKOS_INLINE_FUNCTION double argmax_abs(const uint32_t first, const uint32_t n, const F & f,
137 uint32_t & index) const {
138 const uint32_t L = lanes;
139 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, L), [&](const uint32_t l) {
140 double largest = -1.0;
141 uint32_t at = n;
142 for (uint32_t i = first + l; i < n; i += L) {
143 const double v = Kokkos::fabs(f(i));
144 if (v > largest) {
145 largest = v;
146 at = i;
147 }
148 }
149 partial[l] = largest;
150 where[l] = at;
151 });
152 member.team_barrier();
153 double largest = -1.0;
154 index = n;
155 for (uint32_t l = 0; l < L; l++) {
156 if (partial[l] > largest || (partial[l] == largest && where[l] < index)) {
157 largest = partial[l];
158 index = where[l];
159 }
160 }
161 member.team_barrier();
162 return largest;
163 }
164};
165
166} // namespace chemistry
167
168#endif // CHEMISTRY_LANES_H
KOKKOS_INLINE_FUNCTION rtype dot(const rtype *a, const rtype *b)
Compute the dot product of two arrays.
Definition common_math.h:32
Definition kinetics.h:25
KOKKOS_INLINE_FUNCTION constexpr uint32_t dense_index(const uint32_t n, const uint32_t r, const uint32_t c)
Index of entry (r, c) of an n x n matrix stored by rows, or by columns.
Definition lanes.h:25
One thread does all the work of a cell.
Definition lanes.h:38
static constexpr bool column_major
Definition lanes.h:40
KOKKOS_INLINE_FUNCTION void single(const F &f) const
Definition lanes.h:51
static constexpr bool parallel
Definition lanes.h:41
static constexpr uint32_t lanes
Definition lanes.h:39
KOKKOS_INLINE_FUNCTION void for_each(const uint32_t n, const F &f) const
Definition lanes.h:44
KOKKOS_INLINE_FUNCTION double argmax_abs(const uint32_t first, const uint32_t n, const F &f, uint32_t &index) const
The largest |f(i)| for first <= i < n and the first i that has it.
Definition lanes.h:65
KOKKOS_INLINE_FUNCTION double sum(const uint32_t n, const F &f) const
sum_i f(i), in order.
Definition lanes.h:57
KOKKOS_INLINE_FUNCTION void sync() const
Definition lanes.h:48
All threads and vector lanes of a team (TeamPolicy(cells, T, V), T * V lanes) share the work of a cel...
Definition lanes.h:89
uint32_t lanes
Definition lanes.h:94
double * partial
Definition lanes.h:95
static constexpr uint32_t SUM_SHARES
Definition lanes.h:92
uint32_t * where
Definition lanes.h:96
static KOKKOS_INLINE_FUNCTION constexpr uint32_t shares(const uint32_t L)
Definition lanes.h:98
static constexpr bool column_major
Definition lanes.h:90
static constexpr size_t scratch_bytes(const uint32_t L)
Definition lanes.h:99
KOKKOS_INLINE_FUNCTION double argmax_abs(const uint32_t first, const uint32_t n, const F &f, uint32_t &index) const
Definition lanes.h:136
KOKKOS_INLINE_FUNCTION double sum(const uint32_t n, const F &f) const
Definition lanes.h:121
KOKKOS_INLINE_FUNCTION void single(const F &f) const
Definition lanes.h:116
KOKKOS_INLINE_FUNCTION void for_each(const uint32_t n, const F &f) const
Definition lanes.h:109
KOKKOS_INLINE_FUNCTION TeamLanes(const Member &m, const uint32_t L)
Definition lanes.h:103
static constexpr bool parallel
Definition lanes.h:91
KOKKOS_INLINE_FUNCTION void sync() const
Definition lanes.h:113
const Member & member
Definition lanes.h:93
CellSetup< ExecSpace > c
Definition teno_setup.cpp:1810
uint8_t r
Definition teno_setup.cpp:687