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
20namespace Moose::Kokkos
21{
22
23class System;
24
28class Matrix
29{
30public:
34 Matrix() = default;
42 Mat mat() { return _matrix; }
46 void destroy();
47
48#ifdef MOOSE_KOKKOS_SCOPE
53 bool isAlloc() const { return _is_alloc; }
59 void create(libMesh::SparseMatrix<PetscScalar> & matrix, const System & system);
63 void close();
64
69 KOKKOS_FUNCTION void zero(PetscInt i)
70 {
71 for (PetscInt j = _row_ptr[i]; j < _row_ptr[i + 1]; ++j)
72 _val[j] = 0;
73 }
80 KOKKOS_FUNCTION PetscScalar & operator()(PetscInt i, PetscInt j) const
81 {
82 auto idx = find(i, j);
83
84 KOKKOS_ASSERT(idx != -1);
85
86 return _val[idx];
87 }
92 auto & operator=(PetscScalar scalar)
93 {
94 _val = scalar;
95
96 return *this;
97 }
98#endif
99
100private:
101#ifdef MOOSE_KOKKOS_SCOPE
108 KOKKOS_FUNCTION PetscInt find(PetscInt i, PetscInt j) const;
109#endif
110
114 Mat _matrix = PETSC_NULLPTR;
118 PetscCount _nr = 0;
128
131 bool _is_host = false;
135 bool _is_alloc = false;
136};
137
138#ifdef MOOSE_KOKKOS_SCOPE
139KOKKOS_FUNCTION inline PetscInt
140Matrix::find(PetscInt i, PetscInt j) const
141{
142 KOKKOS_ASSERT(i < _nr);
143
144 auto begin = _col_idx.data() + _row_ptr[i];
145 auto end = _col_idx.data() + _row_ptr[i + 1];
146 auto target = Utils::find(j, begin, end);
147
148 if (target == end)
149 return -1;
150 else
151 return target - _col_idx.data();
152}
153#endif
154
155} // 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.
Array< PetscInt > _row_idx
Mat mat()
Get PETSc matrix handle.
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.
void create(libMesh::SparseMatrix< PetscScalar > &matrix, const System &system)
Create the matrix from a libMesh PetscMatrix.
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