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 #include "KokkosMesh.h"
20 
21 namespace Moose::Kokkos
22 {
23 
24 inline void
26 {
27  _record = &record;
28  _id = record.id;
29 }
30 
31 template <typename T, unsigned int dimension>
33 {
34  _default = true;
35  _value = value;
36 }
37 
38 template <typename T, unsigned int dimension>
40 {
41  // If reference exists, copy the reference property
42  // Reference can be nullptr if the property is a default or optional property
43  const auto & prop = property._reference ? *property._reference : property;
44 
45  shallowCopy(prop);
46 
47  _reference = property._reference;
48 }
49 
50 template <typename T, unsigned int dimension>
51 auto &
53 {
54  shallowCopy(property);
55 
56  return *this;
57 }
58 
59 template <typename T, unsigned int dimension>
60 void
62 {
63  MaterialPropertyBase::init(record, key);
64 
65  _reference = this;
66 }
67 
68 #ifdef MOOSE_KOKKOS_SCOPE
69 template <typename T, unsigned int dimension>
70 void
72 {
73  auto prop_cast = dynamic_cast<const MaterialProperty<T, dimension> *>(&prop);
74 
75  mooseAssert(prop_cast, "The property to copy should be of the same type and dimension.");
76 
77  for (const auto i : index_range(prop_cast->_data))
78  if (prop_cast->_data[i].isAlloc())
79  _data[i].deepCopy(prop_cast->_data[i]);
80 
81  _data.copyToDevice();
82 }
83 
84 template <typename T, unsigned int dimension>
85 void
87 {
88  auto prop_cast = dynamic_cast<MaterialProperty<T, dimension> *>(&prop);
89 
90  mooseAssert(prop_cast, "The property to swap should be of the same type and dimension.");
91 
92  _data.swap(prop_cast->_data);
93 }
94 
95 template <typename T, unsigned int dimension>
96 void
98 {
99  _record = property._record;
100  _id = property._id;
101  _default = property._default;
102  _constant_option = property._constant_option;
103 
104  _reference = property._reference;
105  _data = property._data;
106  _value = property._value;
107 }
108 
109 template <typename T, unsigned int dimension>
110 void
112  const Assembly & assembly,
113  const std::set<SubdomainID> & subdomains,
114  const bool bnd,
115  StorageKey)
116 {
117  if (!_data.isAlloc())
118  _data.create(mesh.getNumSubdomains());
119 
120  if (!_constant_option.isAlloc())
121  {
122  _constant_option.create(mesh.getNumSubdomains());
123  _constant_option = PropertyConstantOption::NONE;
124  }
125 
126  for (const auto subdomain : subdomains)
127  {
128  auto sid = mesh.getContiguousSubdomainID(subdomain);
129  auto constant_option = libmesh_map_find(_record->constant_option, subdomain);
130 
131  using index_type = typename std::remove_reference_t<decltype(_data[sid])>::unsigned_index_type;
132 
133  std::vector<index_type> n;
134 
135  for (unsigned int i = 0; i < dimension; ++i)
136  n.push_back(libmesh_map_find(_record->dims, subdomain)[i]);
137 
138  if (constant_option == PropertyConstantOption::NONE)
139  n.push_back(bnd ? assembly.getNumFaceQps(sid) : assembly.getNumQps(sid));
140  else if (constant_option == PropertyConstantOption::ELEMENT)
141  n.push_back(bnd ? assembly.getElemFacePropertySize(sid)
142  : mesh.getNumSubdomainLocalElements(subdomain));
143  else
144  n.push_back(1);
145 
146  if (!_data[sid].isAlloc())
147  _data[sid].createDevice(n);
148 
149  _constant_option[sid] = constant_option;
150  }
151 
152  _data.copyToDevice();
153  _constant_option.copyToDevice();
154 }
155 
156 template <typename T, unsigned int dimension>
158 MaterialProperty<T, dimension>::operator()(const Datum & datum, const unsigned int qp) const
159 {
160  return MaterialPropertyValue<T, dimension>(*this, datum, qp);
161 }
162 #endif
163 
164 template <typename T, unsigned int dimension>
165 void
166 propertyStore(std::ostream & stream, void * prop)
167 {
168  auto property = static_cast<MaterialProperty<T, dimension> *>(prop);
169 
170  dataStore(stream, property->_data, nullptr);
171 }
172 template <typename T, unsigned int dimension>
173 void
174 propertyLoad(std::istream & stream, void * prop)
175 {
176  auto property = static_cast<MaterialProperty<T, dimension> *>(prop);
177 
178  dataLoad(stream, property->_data, nullptr);
179 }
180 
181 } // namespace Moose::Kokkos
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 Kokkos mesh object.
Definition: KokkosMesh.h:52
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:23
const PropRecord * _record
Pointer to the record of this property.
void dataLoad(std::istream &stream, Array< T, dimension, index_type, layout > &array, void *context)
Definition: KokkosArray.h:1269
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.
auto & operator=(const MaterialProperty< T, dimension > &property)
Shallow copy another property.
unsigned int id
Property ID.
virtual void allocate(const Mesh &mesh, const Assembly &assembly, const std::set< SubdomainID > &subdomains, const bool bnd, StorageKey) override
Allocate the data storage.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
MaterialProperty()=default
Default constructor.
void dataStore(std::ostream &stream, Array< T, dimension, index_type, layout > &array, void *context)
Definition: KokkosArray.h:1225
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 ...
KOKKOS_FUNCTION dof_id_type getElemFacePropertySize(ContiguousSubdomainID subdomain) const
Get the size of element-constant face material property data storage of a subdomain.
void shallowCopy(const MaterialProperty< T, dimension > &property)
Shallow copy another property.
KOKKOS_FUNCTION dof_id_type getNumQps(ContiguousSubdomainID subdomain) const
Get the total number of elemental quadrature points in a subdomain.
The Kokkos material property class.
auto index_range(const T &sizable)
void propertyStore(std::ostream &stream, void *prop)