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
21 {
22 namespace Kokkos
23 {
24 
25 class System;
26 
30 class Matrix
31 {
32 public:
36  Matrix() = default;
40  ~Matrix() { destroy(); }
44  void destroy();
45 
46 #ifdef MOOSE_KOKKOS_SCOPE
47 
51  bool isAlloc() const { return _is_alloc; }
57  void create(libMesh::SparseMatrix<PetscScalar> & matrix, const System & system);
61  void close();
62 
67  KOKKOS_FUNCTION void zero(PetscInt i)
68  {
69  for (PetscInt j = _row_ptr[i]; j < _row_ptr[i + 1]; ++j)
70  _val[j] = 0;
71  }
78  KOKKOS_FUNCTION PetscScalar & operator()(PetscInt i, PetscInt j) const
79  {
80  auto idx = find(i, j);
81 
82  KOKKOS_ASSERT(idx != -1)
83 
84  return _val[idx];
85  }
90  auto & operator=(PetscScalar scalar)
91  {
92  _val = scalar;
93 
94  return *this;
95  }
96 #endif
97 
98 private:
99 #ifdef MOOSE_KOKKOS_SCOPE
100 
106  KOKKOS_FUNCTION PetscInt find(PetscInt i, PetscInt j) const;
107 #endif
108 
112  Mat _matrix = PETSC_NULLPTR;
116  PetscCount _nr = 0;
126 
129  bool _is_host = false;
133  bool _is_alloc = false;
134 };
135 
136 #ifdef MOOSE_KOKKOS_SCOPE
137 KOKKOS_FUNCTION inline PetscInt
138 Matrix::find(PetscInt i, PetscInt j) const
139 {
140  KOKKOS_ASSERT(i < _nr);
141 
142  auto begin = _col_idx.data() + _row_ptr[i];
143  auto end = _col_idx.data() + _row_ptr[i + 1];
144  auto target = Utils::find(j, begin, end);
145 
146  if (target == end)
147  return -1;
148  else
149  return target - _col_idx.data();
150 }
151 #endif
152 
153 } // namespace Kokkos
154 } // namespace Moose
KOKKOS_FUNCTION void zero(PetscInt i)
Zero a row.
Definition: KokkosMatrix.h:67
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:30
Matrix()=default
Default constructor.
Array< PetscScalar > _val
Definition: KokkosMatrix.h:124
The Kokkos system class.
Definition: KokkosSystem.h:35
The Kokkos wrapper class for PETSc matrix.
Definition: KokkosMatrix.h:30
bool _is_alloc
Flag whether the matrix was allocated.
Definition: KokkosMatrix.h:133
KOKKOS_FUNCTION PetscScalar & operator()(PetscInt i, PetscInt j) const
Get an entry with given row and column indices.
Definition: KokkosMatrix.h:78
Array< PetscInt > _col_idx
CSR vectors on device.
Definition: KokkosMatrix.h:121
bool isAlloc() const
Get whether the matrix was allocated.
Definition: KokkosMatrix.h:51
Array< PetscInt > _row_ptr
Definition: KokkosMatrix.h:123
Array< PetscInt > _row_idx
Definition: KokkosMatrix.h:122
bool _is_host
Flag whether the PETSc matrix is a host matrix.
Definition: KokkosMatrix.h:129
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:138
void create(libMesh::SparseMatrix< PetscScalar > &matrix, const System &system)
Create the matrix from a libMesh PetscMatrix.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
auto & operator=(PetscScalar scalar)
Assign a scalar value uniformly.
Definition: KokkosMatrix.h:90
~Matrix()
Destructor.
Definition: KokkosMatrix.h:40
PetscCount _nr
Number of rows local to this process.
Definition: KokkosMatrix.h:116
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:112