https://mooseframework.inl.gov
KokkosVector.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 #include "libmesh/petsc_vector.h"
15 #include "libmesh/dof_map.h"
16 
17 namespace Moose
18 {
19 namespace Kokkos
20 {
21 
22 class System;
23 
27 class Vector
28 {
29 public:
33  Vector() = default;
37  ~Vector() { destroy(); }
41  void destroy();
42 
43 #ifdef MOOSE_KOKKOS_SCOPE
44 
48  bool isAlloc() const { return _is_alloc; }
55  void create(libMesh::NumericVector<PetscScalar> & vector, const System & system, bool assemble);
59  void copyToDevice();
63  void copyToHost();
67  void restore();
71  void close();
72 
78  KOKKOS_FUNCTION PetscScalar & operator()(dof_id_type i) const
79  {
80  return i < _local.size() ? _local[i] : _ghost(i);
81  }
87  KOKKOS_FUNCTION PetscScalar & operator[](dof_id_type i) const
88  {
89  return i < _local.size() ? _local[i] : _ghost(i);
90  }
95  auto & operator=(PetscScalar scalar)
96  {
97  _local = scalar;
98  _ghost = scalar;
99 
100  return *this;
101  }
102 
106  struct PackBuffer
108  {
109  };
111  {
112  };
113 
114  KOKKOS_FUNCTION void operator()(PackBuffer, const PetscCount tid) const;
115  KOKKOS_FUNCTION void operator()(UnpackBuffer, const PetscCount tid) const;
117 #endif
118 
119 private:
123  struct DeviceAssembly
125  {
149  void destroy();
150  };
151 
154 
155  unsigned int _current_proc;
157 
161  Vec _global_vector = PETSC_NULLPTR;
163  Vec _local_vector = PETSC_NULLPTR;
165 
168  PetscScalar * _array = PETSC_NULLPTR;
172  const System * _system;
184 
187  bool _assemble = false;
191  bool _is_ghosted = false;
195  bool _is_host = false;
199  bool _is_alloc = false;
200 };
201 
202 } // namespace Kokkos
203 } // namespace Moose
The Kokkos array class.
Definition: KokkosArray.h:56
void create(const Array< Array< libMesh::dof_id_type >> &list)
Allocate data.
bool isAlloc() const
Get whether the vector was allocated.
Definition: KokkosVector.h:48
bool _assemble
Flag whether the vector will be assembled.
Definition: KokkosVector.h:187
void restore()
Restore the underlying PETSc vector.
const System * _system
Pointer to the Kokkos system.
Definition: KokkosVector.h:172
Array< int > offset
Starting offset of each process into the communication buffer.
Definition: KokkosVector.h:137
void destroy()
Free all data and reset.
Kokkos functions for direct assembly on device.
Definition: KokkosVector.h:107
Array< PetscScalar > buffer
Communication buffer.
Definition: KokkosVector.h:141
~Vector()
Destructor.
Definition: KokkosVector.h:37
KOKKOS_FUNCTION PetscScalar & operator()(dof_id_type i) const
Get an entry with a given index.
Definition: KokkosVector.h:78
Array< PetscScalar > _local
Data vectors on device.
Definition: KokkosVector.h:181
void close()
Assemble the underlying PETSc vector.
Vector()=default
Default constructor.
Array< int > count
Number of DOFs to send/receive for each process.
Definition: KokkosVector.h:133
KOKKOS_FUNCTION PetscScalar & operator[](dof_id_type i) const
Get an entry with a given index.
Definition: KokkosVector.h:87
The Kokkos system class.
Definition: KokkosSystem.h:35
bool _is_host
Flag whether the PETSc vector is a host vector.
Definition: KokkosVector.h:195
Data for direct assembly on device.
Definition: KokkosVector.h:124
void create(libMesh::NumericVector< PetscScalar > &vector, const System &system, bool assemble)
Create the vector from a libMesh PetscVector.
The Kokkos wrapper class for PETSc vector.
Definition: KokkosVector.h:27
const libMesh::Parallel::Communicator * _comm
Pointer to the libMesh communicator.
Definition: KokkosVector.h:176
DeviceAssembly _send
Definition: KokkosVector.h:152
void copyToDevice()
Copy from the host libMesh PetscVector.
auto & operator=(PetscScalar scalar)
Assign a scalar value uniformly.
Definition: KokkosVector.h:95
PetscScalar * _array
Raw data of local PETSc vector.
Definition: KokkosVector.h:168
bool _is_ghosted
Flag whether the PETSc vector is ghosted.
Definition: KokkosVector.h:191
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
Array< Array< libMesh::dof_id_type > > list
List of DOFs to send/receive for each process.
Definition: KokkosVector.h:129
DeviceAssembly _recv
Definition: KokkosVector.h:153
unsigned int _current_proc
Definition: KokkosVector.h:155
bool _is_alloc
Flag whether the vector was allocated.
Definition: KokkosVector.h:199
void copyToHost()
Copy to the host libMesh PetscVector.
Vec _global_vector
PETSc vectors.
Definition: KokkosVector.h:162
uint8_t dof_id_type
Array< PetscScalar > _ghost
Definition: KokkosVector.h:182