peclet-dem 1.1.0
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
solver_fused.hpp
Go to the documentation of this file.
1
20#ifndef DEM_SOLVER_FUSED_HPP
21#define DEM_SOLVER_FUSED_HPP
22
23#include <algorithm>
24#include <cstdlib>
25#include <Kokkos_Core.hpp>
26#include <vector>
27
28#include "contact_preprocessing.hpp" // CpExec/CpMem
29
30namespace peclet::dem {
31
36 Kokkos::View<const int*, CpMem> offsDev;
37 Kokkos::View<unsigned*, CpMem> bar;
38 int maxBucket = 0;
39};
40
46 Kokkos::View<const int*, CpMem> color;
47 int numColors = 0;
48 Kokkos::View<const int*, CpMem> perm;
49 const std::vector<int>* offs = nullptr;
50 const FusedSweepCtx* fused = nullptr;
51};
52
58 float tol;
59 bool strictLess; // position loop breaks on res < tol; velocity loops on res <= tol
60 // Diagnostics only (docs/contact_solve_framework.md §12 S12): a device scalar that receives the
61 // iterations the loop ran (the host loop's count). nullptr = not written; the driver passes it
62 // only with Particles::iterCounters on and reads it back only then (no extra fence or copy
63 // otherwise).
64 int* iters = nullptr;
65};
66
75inline bool demFusedWanted(bool graphReplayAvailable, int mode) {
76#ifdef KOKKOS_ENABLE_CUDA
77 return mode < 0 ? !graphReplayAvailable : mode == 1;
78#else
79 (void)graphReplayAvailable;
80 (void)mode;
81 return false;
82#endif
83}
84
85#ifdef KOKKOS_ENABLE_CUDA
86
97__device__ inline void demGridBarrier(unsigned* count, unsigned k) {
98 __syncthreads(); // this block's work items are done
99 if (threadIdx.x == 0) {
100 __threadfence(); // publish this block's writes before declaring arrival
101 atomicAdd(count, 1u);
102 const unsigned target = (k + 1u) * gridDim.x;
103 while (*reinterpret_cast<volatile unsigned*>(count) < target) {
104 }
105 __threadfence(); // acquire the other blocks' writes
106 }
107 __syncthreads(); // release the block into the next colour
108}
109
112inline constexpr int kFusedBarWords = 32769; // 4096 blocks * 8 + 1
113
118template <class Sweep>
119__global__ void demFusedColorSweepK(Sweep f, Kokkos::View<const int*, CpMem> perm,
120 Kokkos::View<const int*, CpMem> offs, int numColors,
121 unsigned* bar) {
122 const int stride = gridDim.x * blockDim.x;
123 const int tid = blockIdx.x * blockDim.x + threadIdx.x;
124 unsigned k = 0;
125 for (int c = 0; c < numColors; ++c) {
126 const int b = offs(c), e = offs(c + 1);
127 if (b == e)
128 continue;
129 for (int i = b + tid; i < e; i += stride)
130 f.solveOne(perm(i));
131 if (c + 1 < numColors)
132 demGridBarrier(bar, k++);
133 }
134}
135
141template <class Sweep>
142__global__ void demFusedSweepLoopK(Sweep f, Kokkos::View<const int*, CpMem> perm,
143 Kokkos::View<const int*, CpMem> offs, int numColors,
144 int maxIters, float tol, bool strictLess, float* res,
145 unsigned* bar, int* iters) {
146 const int stride = gridDim.x * blockDim.x;
147 const int tid = blockIdx.x * blockDim.x + threadIdx.x;
148 unsigned k = 0;
149 int ran = 0; // the host loop's iteration count (the break is uniform across blocks)
150 for (int it = 0; it < maxIters; ++it) {
151 ran = it + 1;
152 if (tid == 0)
153 *res = 0.0f;
154 demGridBarrier(bar, k++);
155 for (int c = 0; c < numColors; ++c) {
156 const int b = offs(c), e = offs(c + 1);
157 if (b == e)
158 continue;
159 for (int i = b + tid; i < e; i += stride)
160 f.solveOne(perm(i));
161 demGridBarrier(bar, k++);
162 }
163 const float r = *reinterpret_cast<volatile float*>(res);
164 if (strictLess ? (r < tol) : (r <= tol))
165 break;
166 demGridBarrier(bar, k++); // every block has read res before the next zero
167 }
168 if (iters != nullptr && tid == 0)
169 *iters = ran; // diagnostics (§12 S12): a store, no fence; read back only when enabled
170}
171
172inline constexpr int kFusedBlock = 256;
173
177template <class Kernel>
178inline int demFusedMaxGrid(Kernel kernel) {
179 static const int maxGrid = [kernel] {
180 int perSm = 0;
181 if (cudaOccupancyMaxActiveBlocksPerMultiprocessor(&perSm, kernel, kFusedBlock, 0) !=
182 cudaSuccess) {
183 (void)cudaGetLastError();
184 return 0;
185 }
186 int dev = 0, sm = 0;
187 if (cudaGetDevice(&dev) != cudaSuccess ||
188 cudaDeviceGetAttribute(&sm, cudaDevAttrMultiProcessorCount, dev) != cudaSuccess) {
189 (void)cudaGetLastError();
190 return 0;
191 }
192 return perSm * sm;
193 }();
194 return maxGrid;
195}
196
200template <class Sweep>
201inline bool demLaunchFusedColorSweep(CpExec& space, const Sweep& f,
202 Kokkos::View<const int*, CpMem> perm, const FusedSweepCtx& ctx,
203 int numColors) {
204 const int maxGrid = std::min(demFusedMaxGrid(demFusedColorSweepK<Sweep>),
205 (static_cast<int>(ctx.bar.extent(0)) - 1) / 8);
206 if (maxGrid <= 0 || numColors <= 0 || ctx.maxBucket <= 0)
207 return false;
208 const int want = (ctx.maxBucket + kFusedBlock - 1) / kFusedBlock;
209 const int grid = want < maxGrid ? want : maxGrid;
210 cudaStream_t str = space.cuda_stream();
211 cudaMemsetAsync(ctx.bar.data(), 0, (static_cast<std::size_t>(grid) * 8 + 1) * sizeof(unsigned),
212 str);
213 demFusedColorSweepK<Sweep>
214 <<<grid, kFusedBlock, 0, str>>>(f, perm, ctx.offsDev, numColors, ctx.bar.data());
215 return true;
216}
217
221template <class Sweep>
222inline bool demLaunchFusedSweepLoop(CpExec& space, const Sweep& f,
223 Kokkos::View<const int*, CpMem> perm, const FusedSweepCtx& ctx,
224 int numColors, const FusedLoopSpec& spec, float* res) {
225 const int maxGrid = std::min(demFusedMaxGrid(demFusedSweepLoopK<Sweep>),
226 (static_cast<int>(ctx.bar.extent(0)) - 1) / 8);
227 if (maxGrid <= 0 || numColors <= 0 || ctx.maxBucket <= 0 || res == nullptr)
228 return false;
229 const int want = (ctx.maxBucket + kFusedBlock - 1) / kFusedBlock;
230 const int grid = want < maxGrid ? want : maxGrid;
231 cudaStream_t str = space.cuda_stream();
232 cudaMemsetAsync(ctx.bar.data(), 0, (static_cast<std::size_t>(grid) * 8 + 1) * sizeof(unsigned),
233 str);
234 demFusedSweepLoopK<Sweep><<<grid, kFusedBlock, 0, str>>>(f, perm, ctx.offsDev, numColors,
235 spec.maxIters, spec.tol, spec.strictLess,
236 res, ctx.bar.data(), spec.iters);
237 return true;
238}
239
240#endif // KOKKOS_ENABLE_CUDA
241
245inline FusedSweepCtx demMakeFusedCtx(CpExec& space, const std::vector<int>& offs,
246 Kokkos::View<int*, CpMem> offsDev,
247 Kokkos::View<unsigned*, CpMem> bar) {
248 FusedSweepCtx ctx;
249#ifdef KOKKOS_ENABLE_CUDA
250 if (offs.size() < 2 || offs.size() > offsDev.extent(0) + static_cast<std::size_t>(0))
251 return ctx;
252 int maxBucket = 0;
253 for (std::size_t c = 0; c + 1 < offs.size(); ++c)
254 maxBucket = std::max(maxBucket, offs[c + 1] - offs[c]);
255 if (maxBucket <= 0)
256 return ctx;
257 const Kokkos::View<const int*, Kokkos::HostSpace, Kokkos::MemoryUnmanaged> h(offs.data(),
258 offs.size());
259 auto d = Kokkos::subview(offsDev, Kokkos::pair<std::size_t, std::size_t>(0, offs.size()));
260 Kokkos::deep_copy(space, d, h);
261 ctx.offsDev = Kokkos::subview(Kokkos::View<const int*, CpMem>(offsDev),
262 Kokkos::pair<std::size_t, std::size_t>(0, offs.size()));
263 ctx.bar = bar;
264 ctx.maxBucket = maxBucket;
265#else
266 (void)space;
267 (void)offs;
268 (void)offsDev;
269 (void)bar;
270#endif
271 return ctx;
272}
273
274} // namespace peclet::dem
275
276#endif // DEM_SOLVER_FUSED_HPP
dem — portable (Kokkos) contact->manifold reduction, replacing the thrust-based reduce_contacts_to_ma...
bool demFusedWanted(bool graphReplayAvailable, int mode)
Fused-sweep policy.
Kokkos::DefaultExecutionSpace CpExec
FusedSweepCtx demMakeFusedCtx(CpExec &space, const std::vector< int > &offs, Kokkos::View< int *, CpMem > offsDev, Kokkos::View< unsigned *, CpMem > bar)
Fill a FusedSweepCtx from buildColorBuckets's host offsets: async-upload them into the pooled device ...
The colour schedule of one coloured Gauss–Seidel sweep: each item's colour and the colour count,...
Kokkos::View< const int *, CpMem > perm
const FusedSweepCtx * fused
const std::vector< int > * offs
Kokkos::View< const int *, CpMem > color
Device-side iteration loop: run up to maxIters sweeps of the colour classes inside ONE kernel,...
Device-side context for a fused colour sweep: the colour offsets (numColors+1, uploaded from buildCol...
Kokkos::View< const int *, CpMem > offsDev
Kokkos::View< unsigned *, CpMem > bar