3#include <AMReX_Config.H>
9#if defined(AMREX_USE_CUDA)
21 using U = std::conditional_t<std::is_const_v<T>,
Long const,
Long>;
29template <
typename T,
template <
typename>
class V>
41 mat.resize(num_non_zeros);
67template <
typename C,
typename T,
template<
typename>
class AD,
template<
typename>
class AS,
68 std::enable_if_t<std::is_same_v<C,Gpu::HostToDevice> ||
69 std::is_same_v<C,Gpu::DeviceToHost> ||
70 std::is_same_v<C,Gpu::DeviceToDevice>,
int> = 0>
73 dst.
mat.resize(src.
mat.size());
91template <
typename T,
template <
typename>
class V>
94 if (nnz <= 0) {
return; }
98#if defined(AMREX_USE_CUDA) || defined(AMREX_USE_HIP)
103 constexpr int nthreads = 256;
108 auto nr =
int(nrows());
109 int nblocks = (nr + nwarps_per_block-1) / nwarps_per_block;
112 auto* pmat = mat.data();
113 auto* pcol = col_index.data();
114 auto* prow = row_offset.data();
117 auto* d_needs_fallback = needs_fallback.
data();
119 amrex::launch_global<nthreads><<<nblocks, nthreads, 0, stream>>>
123 int r =
int(blockIdx.x)*nwarps_per_block + wid;
126 Long const b = prow[r];
127 Long const e = prow[r+1];
128 auto const len =
int(e - b);
130 if (len <= 1)
return;
136 sorted = sorted && (pcol[b+i-1] <= pcol[b+i]);
138#if defined(AMREX_USE_CUDA)
139 if (__all_sync(0xffffffff, sorted)) {
return; }
141 if (__all(sorted)) {
return; }
147 if (len <= ITEMS_PER_WARP)
149#if defined(AMREX_USE_CUDA)
150 using WarpSort = cub::WarpMergeSort<Long, ITEMS_PER_THREAD, Gpu::Device::warp_size, T>;
151 __shared__
typename WarpSort::TempStorage temp_storage[nwarps_per_block];
152#elif defined(AMREX_USE_HIP)
153 using WarpSort = rocprim::warp_sort<Long, Gpu::Device::warp_size, T>;
154 __shared__
typename WarpSort::storage_type temp_storage[nwarps_per_block];
157 Long keys[ITEMS_PER_THREAD];
158 T values[ITEMS_PER_THREAD];
161 for (
int i = 0; i < ITEMS_PER_THREAD; ++i) {
162 int idx = lane * ITEMS_PER_THREAD + i;
164 keys[i] = pcol[b + idx];
165 values[i] = pmat[b + idx];
167 keys[i] = std::numeric_limits<Long>::max();
173 WarpSort{}.sort(keys, values, temp_storage[wid]),
174 WarpSort(temp_storage[wid]).Sort(
178 for (
int i = 0; i < ITEMS_PER_THREAD; ++i) {
179 int idx = lane * ITEMS_PER_THREAD + i;
181 pcol[b + idx] = keys[i];
182 pmat[b + idx] = values[i];
192 auto* h_needs_fallback = needs_fallback.copyToHost();
194 if (*h_needs_fallback)
196 V<Long> col_index_out(col_index.size());
197 V<T> mat_out(mat.size());
198 auto* d_col_out = col_index_out.data();
199 auto* d_val_out = mat_out.data();
201 std::size_t temp_bytes = 0;
204 rocprim::segmented_radix_sort_pairs,
205 cub::DeviceSegmentedRadixSort::SortPairs)
206 (
nullptr, temp_bytes, pcol, d_col_out, pmat, d_val_out,
207 nnz, nr, prow, prow+1, 0,
int(
sizeof(
Long)*CHAR_BIT),
213 rocprim::segmented_radix_sort_pairs,
214 cub::DeviceSegmentedRadixSort::SortPairs)
215 (d_temp, temp_bytes, pcol, d_col_out, pmat, d_val_out,
216 nnz, nr, prow, prow+1, 0,
int(
sizeof(
Long)*CHAR_BIT),
219 std::swap(col_index, col_index_out);
220 std::swap(mat, mat_out);
230#elif defined(AMREX_USE_SYCL)
249template <
typename T,
template <
typename>
class V>
252 if (nnz <= 0) {
return; }
254 constexpr int SMALL = 128;
272 for (
Long r = 0; r < nr; ++r) {
273 Long const b = row_offset[r ];
274 Long const e = row_offset[r+1];
275 auto const len =
int(e - b);
277 if (len <= 1) {
continue; }
280 for (
int i = 1; i < len; ++i) {
281 if (col_index[b+i-1] > col_index[b+i]) {
286 if (sorted) {
continue; }
290 for (
int i = 0; i < len; ++i) {
291 scols[i] = col_index[b+i];
292 svals[i] = mat [b+i];
294 for (
int i = 1; i < len; ++i) {
298 while (j > 0 && scols[j-1] > c) {
299 scols[j] = scols[j-1];
300 svals[j] = svals[j-1];
306 for (
int i = 0; i < len; ++i) {
307 col_index[b+i] = scols[i];
308 mat [b+i] = svals[i];
315 for (
int i = 0; i < len; ++i) {
316 lcols[i] = col_index[b+i];
317 lvals[i] = mat [b+i];
321 std::sort(perm.begin(), perm.end(),
322 [&] (
int i0,
int i1) {
323 return lcols[i0] < lcols[i1];
326 for (
int out = 0; out < len; ++out) {
327 auto const in = perm[out];
328 col_index[b+out] = lcols[in];
329 mat [b+out] = lvals[in];
#define AMREX_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
#define AMREX_RESTRICT
Definition AMReX_Extension.H:32
#define AMREX_HIP_OR_CUDA(a, b)
Definition AMReX_GpuControl.H:21
#define AMREX_GPU_SAFE_CALL(call)
Definition AMReX_GpuError.H:63
#define AMREX_GPU_ERROR_CHECK()
Definition AMReX_GpuError.H:151
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
virtual void free(void *pt)=0
A pure virtual function for deleting the arena pointed to by pt.
virtual void * alloc(std::size_t sz)=0
Definition AMReX_GpuBuffer.H:18
T const * data() const noexcept
Definition AMReX_GpuBuffer.H:45
static constexpr int warp_size
Definition AMReX_GpuDevice.H:197
amrex_long Long
Definition AMReX_INT.H:30
Arena * The_Arena()
Definition AMReX_Arena.cpp:783
__host__ __device__ AMREX_FORCE_INLINE void AddNoRet(T *sum, T value) noexcept
Definition AMReX_GpuAtomic.H:283
void copyAsync(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
A host-to-device copy routine. Note this is just a wrapper around memcpy, so it assumes contiguous st...
Definition AMReX_GpuContainers.H:228
static constexpr DeviceToHost deviceToHost
Definition AMReX_GpuContainers.H:106
static constexpr HostToDevice hostToDevice
Definition AMReX_GpuContainers.H:105
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:263
gpuStream_t gpuStream() noexcept
Definition AMReX_GpuDevice.H:244
Definition AMReX_Amr.cpp:49
void duplicateCSR(C c, CSR< T, AD > &dst, CSR< T, AS > const &src)
Definition AMReX_CSR.H:71
const int[]
Definition AMReX_BLProfiler.cpp:1664
Definition AMReX_CSR.H:30
V< Long > row_offset
Definition AMReX_CSR.H:33
Long nrows() const
Definition AMReX_CSR.H:36
Long nnz
Definition AMReX_CSR.H:34
void sort()
Definition AMReX_CSR.H:92
CsrView< T > view()
Definition AMReX_CSR.H:47
void sort_on_host()
Definition AMReX_CSR.H:250
CsrView< T const > view() const
Definition AMReX_CSR.H:52
CsrView< T const > const_view() const
Definition AMReX_CSR.H:57
void resize(Long num_rows, Long num_non_zeros)
Definition AMReX_CSR.H:40
V< Long > col_index
Definition AMReX_CSR.H:32
V< T > mat
Definition AMReX_CSR.H:31
Definition AMReX_CSR.H:20
std::conditional_t< std::is_const_v< T >, Long const, Long > U
Definition AMReX_CSR.H:21
T *__restrict__ mat
Definition AMReX_CSR.H:22
Long nrows
Definition AMReX_CSR.H:26
Long nnz
Definition AMReX_CSR.H:25
U *__restrict__ row_offset
Definition AMReX_CSR.H:24
U *__restrict__ col_index
Definition AMReX_CSR.H:23