1#ifndef AMREX_SPMAT_UTIL_H_
2#define AMREX_SPMAT_UTIL_H_
3#include <AMReX_Config.H>
23template <
typename T,
template <
typename>
class V>
47template <
typename T,
template<
typename>
class Allocator>
56 AT.m_partition = col_partition;
57 AT.m_nnz = A.m_csr.
nnz;
61 AT.m_csr = std::move(at_csr);
74 *col = row; *val = T(1);
96 Real lambda,
int nnz_per_row_max)
101 Long lnnz = nlrows * nnz_per_row_max;
105 auto* pmat = mat.
data();
106 auto* pcol = col_index.
data();
107 auto* prow = row_offset.
data();
109 auto ncols_max = (
unsigned int)
110 std::min(ncols,
Long(std::numeric_limits<unsigned int>::max()));
112 auto nnz_row_cap =
int(std::min(
Long(nnz_per_row_max), ncols));
116 auto os = lrow * nnz_per_row_max;
118 if (lrow+1 == nlrows) {
125 for (
int j = 0; j < nnz_row; ++j) {
130 for (
int jj = 0; jj < j; ++jj) {
131 new_c = new_c && (c != pcol[os+jj]);
140 for (
int j = nnz_row; j < nnz_per_row_max; ++j) {
147 r.
define(std::move(partition), pmat, pcol, lnnz, prow,
180 if (col_partition_A.empty() && ! col_partition_B.empty()) {
182 }
else if (! col_partition_A.empty() && col_partition_B.empty()) {
189 if (pca.row_begin != pcb.row_begin ||
190 pca.col_begin != pcb.col_begin ||
191 pca.csr0.nnz != pcb.csr0.nnz ||
192 pca.csr0.nrows != pcb.csr0.nrows ||
193 pca.csr1.nnz != pcb.csr1.nnz ||
194 pca.csr1.nrows != pcb.csr1.nrows)
199 Long N = std::max({pca.csr0.nnz, pca.csr0.nrows, pca.csr1.nnz, pca.csr1.nrows});
202 if (i < pca.csr0.nnz) {
203 t = amrex::almostEqual(pca.csr0.mat[i], pcb.csr0.mat[i], ulp) &&
204 pca.csr0.col_index[i] == pcb.csr0.col_index[i];
206 if (t && i < pca.csr0.nrows) {
207 t = pca.csr0.row_offset[i] == pcb.csr0.row_offset[i];
209 if (t && pca.csr1.nnz > 0) {
210 t = pca.row_map[i] == pcb.row_map[i];
213 if (t && i < pca.csr1.nnz) {
215 t = amrex::almostEqual(pca.csr1.mat[i], pcb.csr1.mat[i], ulp) &&
216 pca.col_map[pca.csr1.col_index[i]] == pcb.col_map[pcb.csr1.col_index[i]];
218 if (t && i < pca.csr1.nrows) {
219 t = pca.csr1.row_offset[i] == pcb.csr1.row_offset[i];
#define AMREX_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
Definition AMReX_AlgPartition.H:21
Long numGlobalRows() const
Total number of rows covered by the partition.
Definition AMReX_AlgPartition.H:50
Long numLocalRows() const
Number of local rows.
Definition AMReX_AlgPartition.H:45
Distributed CSR matrix that manages storage and GPU-friendly partitions.
Definition AMReX_SpMatrix.H:63
T * data()
Don't use this beyond initial setup.
Definition AMReX_SpMatrix.H:203
AlgPartition const & columnPartition() const
Return the column partition used for matrix-vector and matrix-matrix multiplications.
Definition AMReX_SpMatrix.H:188
ParCsr< T const > const_parcsr() const
Const-qualified alias of parcsr() for convenience.
Definition AMReX_SpMatrix.H:1089
Long numLocalRows() const
Number of rows owned by this rank.
Definition AMReX_SpMatrix.H:191
void setVal(F const &f, CsrSorted is_sorted)
Initialize matrix entries using a row-wise functor.
Definition AMReX_SpMatrix.H:993
void define(AlgPartition partition, int nnz_per_row)
Allocate storage for a default-constructed matrix with a fixed number of nonzeros per row.
Definition AMReX_SpMatrix.H:775
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:80
amrex_long Long
Definition AMReX_INT.H:30
int MyProc() noexcept
Definition AMReX_ParallelDescriptor.H:128
unsigned int Random_int(unsigned int n)
Generates one pseudorandom unsigned integer which is uniformly distributed on [0,n-1]-interval for ea...
Definition AMReX_Random.cpp:186
Real Random()
Generate a psuedo-random real from uniform distribution.
Definition AMReX_Random.cpp:155
unsigned int RandomPoisson(Real lambda)
Generate a psuedo-random integer from a Poisson distribution.
Definition AMReX_Random.cpp:172
__host__ __device__ bool almostEqual(T x, T y, int ulp=2)
Definition AMReX_Algorithm.H:139
Definition AMReX_Amr.cpp:50
SpMatrix< T, Allocator > RandomMatrix(AlgPartition partition, Long nrows, Long ncols, Real lambda, int nnz_per_row_max)
Return a random sparse matrix.
Definition AMReX_SpMatUtil.H:95
amrex::ArenaAllocator< T > DefaultAllocator
Definition AMReX_GpuAllocators.H:205
SpMatrix< T, Allocator > IdentityMatrix(AlgPartition partition)
Return an identity matrix with given partition.
Definition AMReX_SpMatUtil.H:70
CSR< T, V > transpose(CSR< T, V > const &csr, Long ncols)
Build the transpose CSR of csr.
Definition AMReX_SpMatUtil.H:24
AMREX_ATTRIBUTE_FLATTEN_FOR void ParallelForRNG(T n, L const &f) noexcept
Definition AMReX_GpuLaunchFunctsC.H:1157
const int[]
Definition AMReX_BLProfiler.cpp:1665
Owning CSR container backed by AMReX resizable vectors.
Definition AMReX_CSR.H:50
V< Long > row_offset
Definition AMReX_CSR.H:53
Long nnz
Definition AMReX_CSR.H:54
CsrView< T > view()
Mutable view of the underlying buffers.
Definition AMReX_CSR.H:78
CsrView< T const > const_view() const
Convenience alias for view() const.
Definition AMReX_CSR.H:93
V< Long > col_index
Definition AMReX_CSR.H:52
V< T > mat
Definition AMReX_CSR.H:51
Sorted CSR means for each row the column indices are sorted.
Definition AMReX_SpMatrix.H:47
Valid CSR means all entries are valid. It may be sorted ro unsorted.
Definition AMReX_SpMatrix.H:53
Definition AMReX_RandomEngine.H:72