19#include <ATen/Parallel.h>
20#include "neml2/neml2.h"
21#include "neml2/models/Model.h"
22#include "neml2/dispatchers/WorkScheduler.h"
23#include "neml2/dispatchers/WorkDispatcher.h"
24#include "neml2/dispatchers/valuemap_helpers.h"
25#include "neml2/dispatchers/derivmap_helpers.h"
38 template <
typename...
P>
59 using RJType = std::tuple<neml2::ValueMap, neml2::DerivMap>;
61 neml2::WorkDispatcher<neml2::ValueMap, RJType, RJType, neml2::ValueMap, RJType>;
76 std::shared_ptr<neml2::Model>
_model;
85 std::unordered_map<std::thread::id, std::shared_ptr<neml2::Model>>
_model_pool;
95 params.
addParam<DataFileName>(
"input",
96 "Path to the NEML2 input file containing the NEML2 model(s).");
97 params.
addParam<std::vector<std::string>>(
100 "Additional command line arguments to use when parsing the NEML2 input file.");
104 "Name of the NEML2 model, i.e., the string inside the brackets [] in the NEML2 input file "
105 "that corresponds to the model you want to use.");
108 "Device on which to evaluate the NEML2 model. The string supplied must follow the following "
109 "schema: (cpu|cuda)[:<device-index>] where cpu or cuda specifies the device type, and "
110 ":<device-index> optionally specifies a device index. For example, device='cpu' sets the "
111 "target compute device to be CPU, and device='cuda:1' sets the target compute device to be "
112 "CUDA with device ID 1. If not specified, default to the compute device specified via the "
113 "command line argument --compute-device.");
116 "Similar to the 'device' parameter, this parameter specifies the device on which to store "
117 "the outputs. Default to be the same as 'device'.");
121 "NEML2 scheduler to use to run the model. If not specified no scheduler is used and MOOSE "
122 "will pass all the constitutive updates to the provided device at once.");
124 params.
addParam<
bool>(
"async_dispatch",
true,
"Whether to use asynchronous dispatch.");
132template <
typename...
P>
141template <
typename...
P>
143 : T(params, args...),
144 _device(params.isParamValid(
"device") ?
neml2::Device(params.get<
std::string>(
"device"))
145 : this->getMooseApp().getLibtorchDevice()),
146 _output_device(params.isParamValid(
"output_device")
147 ?
neml2::Device(params.get<
std::string>(
"output_device"))
150 _async_dispatch(params.get<bool>(
"async_dispatch"))
153 const auto & fname = params.
get<DataFileName>(
"input");
154 const auto & cli_args = params.
get<std::vector<std::string>>(
"cli_args");
155 _factory = neml2::load_input(std::string(fname), neml2::utils::join(cli_args,
" "));
165 auto red = [](std::vector<RJType> && results) ->
RJType
168 std::vector<neml2::ValueMap> vms;
169 std::vector<neml2::DerivMap> dms;
170 for (
auto && [vm,
dm] : results)
172 vms.push_back(std::move(vm));
173 dms.push_back(std::move(
dm));
175 return std::make_tuple(neml2::valuemap_cat_reduce(std::move(vms), 0),
176 neml2::derivmap_cat_reduce(std::move(dms), 0));
181 return std::make_tuple(neml2::valuemap_move_device(std::move(std::get<0>(x)),
_device),
182 neml2::derivmap_move_device(std::move(std::get<1>(x)),
_device));
185 auto thread_init = [
this](neml2::Device
device) ->
void
187 mooseAssert(libMesh::cast_int<unsigned int>(at::get_num_threads()) ==
libMesh::n_threads(),
188 "Inconsistent number of threads");
189 mooseAssert(libMesh::cast_int<unsigned int>(at::get_num_interop_threads()) ==
191 "Inconsistent number of interop threads");
200 [&](neml2::ValueMap && x, neml2::Device
device) ->
RJType
210 return model->value_and_dvalue(std::move(x));
213 &neml2::valuemap_move_device,
223 mooseAssert(_model !=
nullptr,
"_model must be initialized");
224 neml2::diagnose(*_model);
PetscErrorCode PetscOptionItems *PetscErrorCode DM dm
Interface class to provide common input parameters, members, and methods for MOOSEObjects that use NE...
const bool _async_dispatch
Whether to dispatch work asynchronously.
NEML2ModelInterface(const InputParameters ¶ms, P &&... args)
const neml2::Device _device
The device on which to evaluate the NEML2 model.
std::shared_ptr< neml2::Model > _model
The NEML2 material model.
static InputParameters validParams()
virtual void validateModel() const
Validate the NEML2 material model.
const neml2::Device _output_device
The device on which to store the outputs.
neml2::Model & model() const
Get the NEML2 model.
neml2::WorkDispatcher< neml2::ValueMap, RJType, RJType, neml2::ValueMap, RJType > DispatcherType
neml2::WorkScheduler * scheduler()
Get the work scheduler.
const neml2::Device & output_device() const
Get the target output device.
std::unordered_map< std::thread::id, std::shared_ptr< neml2::Model > > _model_pool
Models for each thread.
std::unique_ptr< DispatcherType > _dispatcher
Work dispatcher.
const std::unique_ptr< DispatcherType > & dispatcher() const
Get the work dispatcher.
std::tuple< neml2::ValueMap, neml2::DerivMap > RJType
const neml2::Device & device() const
Get the target compute device.
std::shared_ptr< neml2::WorkScheduler > _scheduler
The work scheduler to use.
std::unique_ptr< neml2::Factory > _factory
The NEML2 factory.
std::shared_ptr< neml2::Model > getModel(neml2::Factory &factory, const std::string &name, neml2::Dtype dtype=neml2::kFloat64)
Get the NEML2 Model.