https://mooseframework.inl.gov
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 namespace Moose::Kokkos
21 {
22 
23 class System;
24 
28 class Matrix
29 {
30 public:
34  Matrix() = default;
38  ~Matrix() { destroy(); }
42  Mat mat() { return _matrix; }
46  void destroy();
47 
48 #ifdef MOOSE_KOKKOS_SCOPE
49 
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 
100 private:
101 #ifdef MOOSE_KOKKOS_SCOPE
102 
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
139 KOKKOS_FUNCTION inline PetscInt
140 Matrix::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 void zero(PetscInt i)
Zero a row.
Definition: KokkosMatrix.h:69
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
Matrix()=default
Default constructor.
Array< PetscScalar > _val
Definition: KokkosMatrix.h:126
The Kokkos system class.
Definition: KokkosSystem.h:34
The Kokkos wrapper class for PETSc matrix.
Definition: KokkosMatrix.h:28
bool _is_alloc
Flag whether the matrix was allocated.
Definition: KokkosMatrix.h:135
KOKKOS_FUNCTION PetscScalar & operator()(PetscInt i, PetscInt j) const
Get an entry with given row and column indices.
Definition: KokkosMatrix.h:80
Array< PetscInt > _col_idx
CSR vectors on device.
Definition: KokkosMatrix.h:123
bool isAlloc() const
Get whether the matrix was allocated.
Definition: KokkosMatrix.h:53
KOKKOS_FUNCTION T * data() const
Get the data pointer.
Definition: KokkosArray.h:217
Array< PetscInt > _row_ptr
Definition: KokkosMatrix.h:125
Array< PetscInt > _row_idx
Definition: KokkosMatrix.h:124
bool _is_host
Flag whether the PETSc matrix is a host matrix.
Definition: KokkosMatrix.h:131
void destroy()
Free all data and reset.
void close()
Assemble the underlying PETSc matrix.
KOKKOS_FUNCTION PetscInt find(PetscInt i, PetscInt j) const
Get the index of given row and column indices.
Definition: KokkosMatrix.h:140
void create(libMesh::SparseMatrix< PetscScalar > &matrix, const System &system)
Create the matrix from a libMesh PetscMatrix.
auto & operator=(PetscScalar scalar)
Assign a scalar value uniformly.
Definition: KokkosMatrix.h:92
~Matrix()
Destructor.
Definition: KokkosMatrix.h:38
Mat mat()
Get PETSc matrix handle.
Definition: KokkosMatrix.h:42
PetscCount _nr
Number of rows local to this process.
Definition: KokkosMatrix.h:118
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)
Mat _matrix
PETSc matrix.
Definition: KokkosMatrix.h:114