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::Kokkos
18 {
19 
20 class System;
21 
25 class Vector
26 {
27 public:
31  Vector() = default;
35  ~Vector() { destroy(); }
39  void destroy();
40 
41 #ifdef MOOSE_KOKKOS_SCOPE
42 
46  bool isAlloc() const { return _is_alloc; }
53  void create(libMesh::NumericVector<PetscScalar> & vector, const System & system, bool assemble);
57  void copyToDevice();
61  void copyToHost();
65  void restore();
69  void close();
70 
76  KOKKOS_FUNCTION PetscScalar & operator()(dof_id_type i) const
77  {
78  return i < _local.size() ? _local[i] : _ghost(i);
79  }
85  KOKKOS_FUNCTION PetscScalar & operator[](dof_id_type i) const
86  {
87  return i < _local.size() ? _local[i] : _ghost(i);
88  }
93  auto & operator=(PetscScalar scalar)
94  {
95  _local = scalar;
96  _ghost = scalar;
97 
98  return *this;
99  }
100 
104  struct PackBuffer
106  {
107  };
109  {
110  };
111 
112  KOKKOS_FUNCTION void operator()(PackBuffer, const PetscCount tid) const;
113  KOKKOS_FUNCTION void operator()(UnpackBuffer, const PetscCount tid) const;
115 #endif
116 
117 private:
121  struct DeviceAssembly
123  {
147  void destroy();
148  };
149 
152 
153  unsigned int _current_proc;
155 
159  Vec _global_vector = PETSC_NULLPTR;
161  Vec _local_vector = PETSC_NULLPTR;
163 
166  PetscScalar * _array = PETSC_NULLPTR;
170  const System * _system;
182 
185  bool _assemble = false;
189  bool _is_ghosted = false;
193  bool _is_host = false;
197  bool _is_alloc = false;
198 };
199 
200 } // namespace Moose::Kokkos
The Kokkos array class.
Definition: KokkosArray.h:64
void create(const Array< Array< libMesh::dof_id_type >> &list)
Allocate data.
bool isAlloc() const
Get whether the vector was allocated.
Definition: KokkosVector.h:46
bool _assemble
Flag whether the vector will be assembled.
Definition: KokkosVector.h:185
void restore()
Restore the underlying PETSc vector.
const System * _system
Pointer to the Kokkos system.
Definition: KokkosVector.h:170
Array< int > offset
Starting offset of each process into the communication buffer.
Definition: KokkosVector.h:135
void destroy()
Free all data and reset.
Kokkos functions for direct assembly on device.
Definition: KokkosVector.h:105
Array< PetscScalar > buffer
Communication buffer.
Definition: KokkosVector.h:139
~Vector()
Destructor.
Definition: KokkosVector.h:35
KOKKOS_FUNCTION PetscScalar & operator()(dof_id_type i) const
Get an entry with a given index.
Definition: KokkosVector.h:76
Array< PetscScalar > _local
Data vectors on device.
Definition: KokkosVector.h:179
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:131
KOKKOS_FUNCTION PetscScalar & operator[](dof_id_type i) const
Get an entry with a given index.
Definition: KokkosVector.h:85
The Kokkos system class.
Definition: KokkosSystem.h:34
bool _is_host
Flag whether the PETSc vector is a host vector.
Definition: KokkosVector.h:193
Data for direct assembly on device.
Definition: KokkosVector.h:122
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:25
const libMesh::Parallel::Communicator * _comm
Pointer to the libMesh communicator.
Definition: KokkosVector.h:174
KOKKOS_FUNCTION index_type size() const
Get the total array size.
Definition: KokkosArray.h:205
DeviceAssembly _send
Definition: KokkosVector.h:150
void copyToDevice()
Copy from the host libMesh PetscVector.
auto & operator=(PetscScalar scalar)
Assign a scalar value uniformly.
Definition: KokkosVector.h:93
PetscScalar * _array
Raw data of local PETSc vector.
Definition: KokkosVector.h:166
bool _is_ghosted
Flag whether the PETSc vector is ghosted.
Definition: KokkosVector.h:189
Array< Array< libMesh::dof_id_type > > list
List of DOFs to send/receive for each process.
Definition: KokkosVector.h:127
DeviceAssembly _recv
Definition: KokkosVector.h:151
unsigned int _current_proc
Definition: KokkosVector.h:153
bool _is_alloc
Flag whether the vector was allocated.
Definition: KokkosVector.h:197
void copyToHost()
Copy to the host libMesh PetscVector.
Vec _global_vector
PETSc vectors.
Definition: KokkosVector.h:160
uint8_t dof_id_type
Array< PetscScalar > _ghost
Definition: KokkosVector.h:180