https://mooseframework.inl.gov
KokkosFunctorRegistry.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 "KokkosFunctorWrapper.h"
13 #include "KokkosFunctionWrapper.h"
14 
15 namespace Moose::Kokkos
16 {
17 
19 {
20 public:
26  virtual std::unique_ptr<FunctorWrapperHostBase> build(const void * object) const = 0;
27 };
28 
29 template <typename Object>
31 {
32 public:
33  std::unique_ptr<FunctorWrapperHostBase> build(const void * object) const override final
34  {
35  return std::make_unique<FunctorWrapperHost<Object>>(object);
36  }
37 };
38 
40 {
41 public:
47  virtual std::unique_ptr<FunctionWrapperHostBase> build(const void * object) const = 0;
48 };
49 
50 template <typename Object>
52 {
53 public:
54  std::unique_ptr<FunctionWrapperHostBase> build(const void * object) const override final
55  {
56  return std::make_unique<FunctionWrapperHost<Object>>(object);
57  }
58 };
59 
61 {
62 public:
63  FunctorRegistry() = default;
64 
65  FunctorRegistry(FunctorRegistry const &) = delete;
66  FunctorRegistry & operator=(FunctorRegistry const &) = delete;
67 
68  FunctorRegistry(FunctorRegistry &&) = delete;
70 
76  template <typename Object>
77  static char addFunctor(const std::string & name)
78  {
79  getRegistry()._functors[name] = std::make_unique<FunctorRegistryEntry<Object>>();
80 
81  return 0;
82  }
83 
89  template <typename Object>
90  static char addFunction(const std::string & name)
91  {
92  getRegistry()._functions[name] = std::make_unique<FunctionRegistryEntry<Object>>();
93 
94  return 0;
95  }
96 
103  static std::unique_ptr<FunctorWrapperHostBase> buildFunctor(const void * object,
104  const std::string & name)
105  {
106  auto it = getRegistry()._functors.find(name);
107  if (it == getRegistry()._functors.end())
108  mooseError("Kokkos functor not registered for type '",
109  name,
110  "'. Double check that you used Kokkos-specific registration macro.");
111 
112  return it->second->build(object);
113  }
114 
121  static std::unique_ptr<FunctionWrapperHostBase> buildFunction(const void * object,
122  const std::string & name)
123  {
124  auto it = getRegistry()._functions.find(name);
125  if (it == getRegistry()._functions.end())
126  mooseError("Kokkos function not registered for type '",
127  name,
128  "'. Double check that you used Kokkos-specific registration macro.");
129 
130  return it->second->build(object);
131  }
132 
133 private:
138  static FunctorRegistry & getRegistry();
139 
144  std::map<std::string, std::unique_ptr<FunctorRegistryEntryBase>> _functors;
149  std::map<std::string, std::unique_ptr<FunctionRegistryEntryBase>> _functions;
150 };
151 
152 } // namespace Moose::Kokkos
153 
154 #define registerKokkosFunction(app, classname) \
155  registerMooseObject(app, classname); \
156  static char combineNames(kokkos_functor_##classname, __COUNTER__) = \
157  Moose::Kokkos::FunctorRegistry::addFunctor<classname>(#classname); \
158  static char combineNames(kokkos_function_##classname, __COUNTER__) = \
159  Moose::Kokkos::FunctorRegistry::addFunction<classname>(#classname)
160 
161 #define registerKokkosFunctionAliased(app, classname, alias) \
162  registerMooseObjectAliased(app, classname, alias); \
163  static char combineNames(kokkos_functor_##classname, __COUNTER__) = \
164  Moose::Kokkos::FunctorRegistry::addFunctor<classname>(alias); \
165  static char combineNames(kokkos_function_##classname, __COUNTER__) = \
166  Moose::Kokkos::FunctorRegistry::addFunction<classname>(alias)
std::string name(const ElemQuality q)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
static char addFunctor(const std::string &name)
Register a functor.
std::unique_ptr< FunctionWrapperHostBase > build(const void *object) const override final
Build a host wrapper for this function.
static FunctorRegistry & getRegistry()
Get the registry singleton.
FunctorRegistry & operator=(FunctorRegistry const &)=delete
std::unique_ptr< FunctorWrapperHostBase > build(const void *object) const override final
Build a host wrapper for this functor.
static char addFunction(const std::string &name)
Register a function.
std::map< std::string, std::unique_ptr< FunctionRegistryEntryBase > > _functions
Map containing the host wrapper shells of functions with the key being the registered object type nam...
virtual std::unique_ptr< FunctionWrapperHostBase > build(const void *object) const =0
Build a host wrapper for this function.
virtual std::unique_ptr< FunctorWrapperHostBase > build(const void *object) const =0
Build a host wrapper for this functor.
static std::unique_ptr< FunctorWrapperHostBase > buildFunctor(const void *object, const std::string &name)
Build and get a host wrapper of a functor.
std::map< std::string, std::unique_ptr< FunctorRegistryEntryBase > > _functors
Map containing the host wrapper shells of functors with the key being the registered object type name...
static std::unique_ptr< FunctionWrapperHostBase > buildFunction(const void *object, const std::string &name)
Build and get a host wrapper of a function.