Matrix: template Scalar type

This commit is contained in:
Arne Morten Kvarving 2024-04-15 16:25:13 +02:00
parent 25374b0e54
commit 5fbd7635cd
5 changed files with 15 additions and 14 deletions

View File

@ -29,17 +29,17 @@ namespace Accelerator
/// This struct resembles a csr matrix, only doubles are supported
/// The data is stored in contiguous memory, such that they can be copied to a device in one transfer.
class Matrix {
template<class Scalar>
class Matrix
{
public:
/// Allocate square Matrix and data arrays with given sizes
/// \param[in] N number of rows
/// \param[in] nnzs number of nonzeros
Matrix(int N_, int nnzs_)
: N(N_),
M(N_),
nnzs(nnzs_)
: N(N_)
, M(N_)
, nnzs(nnzs_)
{
nnzValues.resize(nnzs);
colIndices.resize(nnzs);
@ -51,12 +51,12 @@ public:
/// \param[in] M number of columns
/// \param[in] nnzs number of nonzeros
Matrix(int N_, int M_, int nnzs_)
: Matrix(N_, nnzs_)
: Matrix(N_, nnzs_)
{
M = M_;
}
std::vector<double> nnzValues;
std::vector<Scalar> nnzValues;
std::vector<int> colIndices;
std::vector<int> rowPointers;
int N, M;

View File

@ -173,10 +173,10 @@ void CPR<block_size>::init_opencl_buffers() {
d_Amatrices.reserve(num_levels);
d_Rmatrices.reserve(num_levels - 1);
d_invDiags.reserve(num_levels - 1);
for (Matrix& m : Amatrices) {
for (Matrix<double>& m : Amatrices) {
d_Amatrices.emplace_back(context.get(), m.N, m.M, m.nnzs, 1);
}
for (Matrix& m : Rmatrices) {
for (Matrix<double>& m : Rmatrices) {
d_Rmatrices.emplace_back(context.get(), m.N, m.M, m.nnzs, 1);
d_f.emplace_back(*context, CL_MEM_READ_WRITE, sizeof(double) * m.N);
d_u.emplace_back(*context, CL_MEM_READ_WRITE, sizeof(double) * m.N);

View File

@ -59,7 +59,7 @@ class CPR : public Preconditioner<block_size>
private:
int num_levels;
std::vector<double> weights, coarse_vals, coarse_x, coarse_y;
std::vector<Matrix> Amatrices, Rmatrices; // scalar matrices that represent the AMG hierarchy
std::vector<Matrix<double>> Amatrices, Rmatrices; // scalar matrices that represent the AMG hierarchy
std::vector<OpenclMatrix> d_Amatrices, d_Rmatrices; // scalar matrices that represent the AMG hierarchy
std::vector<std::vector<int> > PcolIndices; // prolongation does not need a full matrix, only store colIndices
std::vector<cl::Buffer> d_PcolIndices;

View File

@ -46,7 +46,8 @@ void OpenclMatrix::upload(cl::CommandQueue *queue, double *vals, int *cols, int
}
}
void OpenclMatrix::upload(cl::CommandQueue *queue, Matrix *matrix) {
void OpenclMatrix::upload(cl::CommandQueue* queue, Matrix<double>* matrix)
{
if (block_size != 1) {
OPM_THROW(std::logic_error, "Error trying to upload a BlockedMatrix to OpenclMatrix with different block_size");
}

View File

@ -29,7 +29,7 @@ namespace Opm
namespace Accelerator
{
class Matrix;
template<class Scalar> class Matrix;
template<class Scalar> class BlockedMatrix;
/// This struct resembles a csr matrix, only doubles are supported
@ -49,7 +49,7 @@ public:
}
void upload(cl::CommandQueue *queue, double *vals, int *cols, int *rows);
void upload(cl::CommandQueue *queue, Matrix *matrix);
void upload(cl::CommandQueue* queue, Matrix<double>* matrix);
void upload(cl::CommandQueue* queue, BlockedMatrix<double>* matrix);
cl::Buffer nnzValues;