https://mooseframework.inl.gov
KokkosMaterialProperty.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 
13 
14 #ifdef MOOSE_KOKKOS_SCOPE
16 #endif
17 
18 #include "KokkosAssembly.h"
19 
20 #include "MooseMesh.h"
21 
22 namespace Moose
23 {
24 namespace Kokkos
25 {
26 
27 inline void
29 {
30  _record = &record;
31  _id = record.id;
32 }
33 
34 template <typename T, unsigned int dimension>
36 {
37  _default = true;
38  _value = value;
39 }
40 
41 template <typename T, unsigned int dimension>
43 {
44  // If reference exists, copy the reference property
45  // Reference can be nullptr if the property is a default or optional property
46  const auto & prop = property._reference ? *property._reference : property;
47 
48  shallowCopy(prop);
49 
50  _reference = property._reference;
51 }
52 
53 template <typename T, unsigned int dimension>
54 auto &
56 {
57  shallowCopy(property);
58 
59  return *this;
60 }
61 
62 template <typename T, unsigned int dimension>
63 void
65 {
66  MaterialPropertyBase::init(record, key);
67 
68  _reference = this;
69 }
70 
71 #ifdef MOOSE_KOKKOS_SCOPE
72 template <typename T, unsigned int dimension>
73 void
75 {
76  auto prop_cast = dynamic_cast<const MaterialProperty<T, dimension> *>(&prop);
77 
78  mooseAssert(prop_cast, "The property to copy should be of the same type and dimension.");
79 
80  for (const auto i : index_range(prop_cast->_data))
81  if (prop_cast->_data[i].isAlloc())
82  _data[i].deepCopy(prop_cast->_data[i]);
83 
84  _data.copyToDevice();
85 }
86 
87 template <typename T, unsigned int dimension>
88 void
90 {
91  auto prop_cast = dynamic_cast<MaterialProperty<T, dimension> *>(&prop);
92 
93  mooseAssert(prop_cast, "The property to swap should be of the same type and dimension.");
94 
95  _data.swap(prop_cast->_data);
96 }
97 
98 template <typename T, unsigned int dimension>
99 void
101 {
102  _record = property._record;
103  _id = property._id;
104  _default = property._default;
105 
106  _reference = property._reference;
107  _data = property._data;
108  _value = property._value;
109 }
110 
111 template <typename T, unsigned int dimension>
112 void
114  const Assembly & assembly,
115  const std::set<SubdomainID> & subdomains,
116  const bool bnd,
117  StorageKey)
118 {
119  if (!_data.isAlloc())
120  _data.create(mesh.meshSubdomains().size());
121 
122  for (const auto subdomain : subdomains)
123  {
124  auto sid = mesh.getKokkosMesh()->getContiguousSubdomainID(subdomain);
125 
126  std::vector<dof_id_type> n;
127 
128  for (unsigned int i = 0; i < dimension; ++i)
129  n.push_back(_record->dims[i]);
130 
131  n.push_back(bnd ? assembly.getNumFaceQps(sid) : assembly.getNumQps(sid));
132 
133  if (!_data[sid].isAlloc())
134  _data[sid].createDevice(n);
135  }
136 
137  _data.copyToDevice();
138 }
139 
140 template <typename T, unsigned int dimension>
142 MaterialProperty<T, dimension>::operator()(const Datum & datum, const unsigned int qp) const
143 {
144  return MaterialPropertyValue<T, dimension>(*this, datum, qp);
145 }
146 #endif
147 
148 template <typename T, unsigned int dimension>
149 void
150 propertyStore(std::ostream & stream, void * prop)
151 {
152  auto property = static_cast<MaterialProperty<T, dimension> *>(prop);
153 
154  dataStore(stream, property->_data, nullptr);
155 }
156 template <typename T, unsigned int dimension>
157 void
158 propertyLoad(std::istream & stream, void * prop)
159 {
160  auto property = static_cast<MaterialProperty<T, dimension> *>(prop);
161 
162  dataLoad(stream, property->_data, nullptr);
163 }
164 
165 } // namespace Kokkos
166 } // namespace Moose
virtual void init(const PropRecord &record, const StorageKey &)
Initialize this property.
The Kokkos assembly class.
A structure storing the metadata of Kokkos material properties.
The base class for Kokkos material properties.
KOKKOS_FUNCTION MaterialPropertyValue< T, dimension > operator()(const Datum &datum, const unsigned int qp) const
Get the property values of a quadrature point.
The Kokkos object that holds thread-private data in the parallel operations of any Kokkos object...
Definition: KokkosDatum.h:25
const PropRecord * _record
Pointer to the record of this property.
void propertyLoad(std::istream &stream, void *prop)
virtual void init(const PropRecord &record, const StorageKey &key) override
Initialize this property.
MeshBase & mesh
virtual void swap(MaterialPropertyBase &prop, StorageKey) override
Swap with another property.
virtual void allocate(const MooseMesh &mesh, const Assembly &assembly, const std::set< SubdomainID > &subdomains, const bool bnd, StorageKey) override
Allocate the data storage.
auto & operator=(const MaterialProperty< T, dimension > &property)
Shallow copy another property.
unsigned int id
Property ID.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
MaterialProperty()=default
Default constructor.
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:92
virtual void copy(const MaterialPropertyBase &prop, StorageKey) override
Deep copy another property.
KOKKOS_FUNCTION dof_id_type getNumFaceQps(ContiguousSubdomainID subdomain) const
Get the total number of facial quadrature points in a subdomain Note: this number does not represent ...
void shallowCopy(const MaterialProperty< T, dimension > &property)
Shallow copy another property.
void dataStore(std::ostream &stream, Array< T, dimension > &array, void *context)
Definition: KokkosArray.h:887
void dataLoad(std::istream &stream, Array< T, dimension > &array, void *context)
Definition: KokkosArray.h:931
KOKKOS_FUNCTION dof_id_type getNumQps(ContiguousSubdomainID subdomain) const
Get the total number of elemental quadrature points in a subdomain.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
The Kokkos material property class.
auto index_range(const T &sizable)
void propertyStore(std::ostream &stream, void *prop)