20#ifndef DEM_SOLVER_FUSED_HPP
21#define DEM_SOLVER_FUSED_HPP
25#include <Kokkos_Core.hpp>
37 Kokkos::View<unsigned*, CpMem>
bar;
46 Kokkos::View<const int*, CpMem>
color;
48 Kokkos::View<const int*, CpMem>
perm;
49 const std::vector<int>*
offs =
nullptr;
76#ifdef KOKKOS_ENABLE_CUDA
77 return mode < 0 ? !graphReplayAvailable : mode == 1;
79 (void)graphReplayAvailable;
85#ifdef KOKKOS_ENABLE_CUDA
97__device__
inline void demGridBarrier(
unsigned* count,
unsigned k) {
99 if (threadIdx.x == 0) {
101 atomicAdd(count, 1u);
102 const unsigned target = (k + 1u) * gridDim.x;
103 while (*
reinterpret_cast<volatile unsigned*
>(count) < target) {
112inline constexpr int kFusedBarWords = 32769;
118template <
class Sweep>
119__global__
void demFusedColorSweepK(Sweep f, Kokkos::View<const int*, CpMem> perm,
120 Kokkos::View<const int*, CpMem> offs,
int numColors,
122 const int stride = gridDim.x * blockDim.x;
123 const int tid = blockIdx.x * blockDim.x + threadIdx.x;
125 for (
int c = 0; c < numColors; ++c) {
126 const int b = offs(c), e = offs(c + 1);
129 for (
int i = b + tid; i < e; i += stride)
131 if (c + 1 < numColors)
132 demGridBarrier(bar, k++);
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;
150 for (
int it = 0; it < maxIters; ++it) {
154 demGridBarrier(bar, k++);
155 for (
int c = 0; c < numColors; ++c) {
156 const int b = offs(c), e = offs(c + 1);
159 for (
int i = b + tid; i < e; i += stride)
161 demGridBarrier(bar, k++);
163 const float r = *
reinterpret_cast<volatile float*
>(res);
164 if (strictLess ? (r < tol) : (r <= tol))
166 demGridBarrier(bar, k++);
168 if (iters !=
nullptr && tid == 0)
172inline constexpr int kFusedBlock = 256;
177template <
class Kernel>
178inline int demFusedMaxGrid(Kernel kernel) {
179 static const int maxGrid = [kernel] {
181 if (cudaOccupancyMaxActiveBlocksPerMultiprocessor(&perSm, kernel, kFusedBlock, 0) !=
183 (void)cudaGetLastError();
187 if (cudaGetDevice(&dev) != cudaSuccess ||
188 cudaDeviceGetAttribute(&sm, cudaDevAttrMultiProcessorCount, dev) != cudaSuccess) {
189 (void)cudaGetLastError();
200template <
class Sweep>
201inline bool demLaunchFusedColorSweep(
CpExec& space,
const Sweep& f,
202 Kokkos::View<const int*, CpMem> perm,
const FusedSweepCtx& ctx,
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)
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),
213 demFusedColorSweepK<Sweep>
214 <<<grid, kFusedBlock, 0, str>>>(f, perm, ctx.offsDev, numColors, ctx.bar.data());
221template <
class Sweep>
222inline bool demLaunchFusedSweepLoop(
CpExec& space,
const Sweep& f,
223 Kokkos::View<const int*, CpMem> perm,
const FusedSweepCtx& ctx,
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)
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),
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);
246 Kokkos::View<int*, CpMem> offsDev,
247 Kokkos::View<unsigned*, CpMem> bar) {
249#ifdef KOKKOS_ENABLE_CUDA
250 if (offs.size() < 2 || offs.size() > offsDev.extent(0) +
static_cast<std::size_t
>(0))
253 for (std::size_t c = 0; c + 1 < offs.size(); ++c)
254 maxBucket = std::max(maxBucket, offs[c + 1] - offs[c]);
257 const Kokkos::View<const int*, Kokkos::HostSpace, Kokkos::MemoryUnmanaged> h(offs.data(),
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()));
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