https://mooseframework.inl.gov
Loading...
Searching...
No Matches
KokkosMatrix.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://www.mooseframework.org
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#pragma once
11
12#include "KokkosArray.h"
13
14#ifdef MOOSE_KOKKOS_SCOPE
15#include "KokkosUtils.h"
16#endif
17
18#include "libmesh/petsc_matrix.h"
19
20#include <vector>
21
22namespace Moose::Kokkos
23{
24
25class System;
26
30class Matrix
31{
32public:
36 Matrix() = default;
44 Mat mat() { return _matrix; }
48 void destroy();
49
50#ifdef MOOSE_KOKKOS_SCOPE
55 bool isAlloc() const { return _is_alloc; }
69 const System & system,
70 std::vector<PetscInt> & coo_i,
71 std::vector<PetscInt> & coo_j);
75 void close();
76
81 KOKKOS_FUNCTION void zero(PetscInt i)
82 {
83 for (PetscInt j = _row_ptr[i]; j < _row_ptr[i + 1]; ++j)
84 _val[j] = 0;
85 }
92 KOKKOS_FUNCTION PetscScalar & operator()(PetscInt i, PetscInt j) const
93 {
94 auto idx = find(i, j);
95
96 KOKKOS_ASSERT(idx != -1);
97
98 return _val[idx];
99 }
104 auto & operator=(PetscScalar scalar)
105 {
106 _val = scalar;
107
108 return *this;
109 }
110#endif
111
112private:
113#ifdef MOOSE_KOKKOS_SCOPE
120 KOKKOS_FUNCTION PetscInt find(PetscInt i, PetscInt j) const;
121#endif
122
126 Mat _matrix = PETSC_NULLPTR;
130 PetscCount _nr = 0;
139
142 bool _is_host = false;
146 bool _is_alloc = false;
147};
148
149#ifdef MOOSE_KOKKOS_SCOPE
150KOKKOS_FUNCTION inline PetscInt
151Matrix::find(PetscInt i, PetscInt j) const
152{
153 KOKKOS_ASSERT(i < _nr);
154
155 auto begin = _col_idx.data() + _row_ptr[i];
156 auto end = _col_idx.data() + _row_ptr[i + 1];
157 auto target = Utils::find(j, begin, end);
158
159 if (target == end)
160 return -1;
161 else
162 return target - _col_idx.data();
163}
164#endif
165
166} // namespace Moose::Kokkos
KOKKOS_FUNCTION T * data() const
Get the data pointer.
The Kokkos array class.
The Kokkos wrapper class for PETSc matrix.
Array< PetscInt > _row_ptr
KOKKOS_FUNCTION void zero(PetscInt i)
Zero a row.
KOKKOS_FUNCTION PetscScalar & operator()(PetscInt i, PetscInt j) const
Get an entry with given row and column indices.
bool _is_host
Flag whether the PETSc matrix is a host matrix.
auto & operator=(PetscScalar scalar)
Assign a scalar value uniformly.
Array< PetscScalar > _val
KOKKOS_FUNCTION PetscInt find(PetscInt i, PetscInt j) const
Get the index of given row and column indices.
void close()
Assemble the underlying PETSc matrix.
Mat mat()
Get PETSc matrix handle.
void create(libMesh::SparseMatrix< PetscScalar > &matrix, const System &system, std::vector< PetscInt > &coo_i, std::vector< PetscInt > &coo_j)
Create the matrix from a libMesh PetscMatrix.
bool isAlloc() const
Get whether the matrix was allocated.
PetscCount _nr
Number of rows local to this process.
Array< PetscInt > _col_idx
CSR vectors on device.
Matrix()=default
Default constructor.
Mat _matrix
PETSc matrix.
bool _is_alloc
Flag whether the matrix was allocated.
void destroy()
Free all data and reset.
The Kokkos base system class.
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
Definition KokkosUtils.h:40