https://mooseframework.inl.gov
KokkosReferenceWrapper.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 #ifdef MOOSE_KOKKOS_SCOPE
13 #include "KokkosHeader.h"
14 #endif
15 
16 #include "MooseTypes.h"
17 
18 namespace Moose
19 {
20 namespace Kokkos
21 {
22 
34 template <typename T>
36 {
37 public:
42  ReferenceWrapper(T & reference) : _reference(reference), _copy(reference) {}
47  : _reference(object._reference), _copy(object._reference)
48  {
49  }
50 
51 #ifdef MOOSE_KOKKOS_SCOPE
52 
57  KOKKOS_FUNCTION operator const T &() const
58  {
59  KOKKOS_IF_ON_HOST(return _reference;)
60 
61  return _copy;
62  }
68  KOKKOS_FUNCTION const T & operator*() const
69  {
70  KOKKOS_IF_ON_HOST(return _reference;)
71 
72  return _copy;
73  }
79  KOKKOS_FUNCTION const T * operator->() const
80  {
81  KOKKOS_IF_ON_HOST(return &_reference;)
82 
83  return &_copy;
84  }
90  template <typename... Args>
91  KOKKOS_FUNCTION auto operator()(Args &&... args) const -> decltype(auto)
92  {
93  KOKKOS_IF_ON_HOST(return _reference(std::forward<Args>(args)...);)
94 
95  return _copy(std::forward<Args>(args)...);
96  }
97 #else
98 
102  operator const T &() const { return _reference; }
107  const T & operator*() const { return _reference; }
112  const T * operator->() const { return &_reference; }
117  template <typename... Args>
118  auto operator()(Args &&... args) const -> decltype(auto)
119  {
120  return _reference(std::forward<Args>(args)...);
121  }
122 #endif
123 
127  operator T &() { return _reference; }
132  T & operator*() { return _reference; }
137  T * operator->() { return &_reference; }
142  template <typename... Args>
143  auto operator()(Args &&... args) -> decltype(auto)
144  {
145  return _reference(std::forward<Args>(args)...);
146  }
147 
148 protected:
156  const T _copy;
157 };
158 
159 } // namespace Kokkos
160 } // namespace Moose
KOKKOS_FUNCTION const T * operator->() const
Get the const pointer to the stored variable.
The Kokkos object that can hold the reference of a variable.
auto operator()(Args &&... args) const -> decltype(auto)
Forward arguments to the stored host reference&#39;s const operator()
T & _reference
Writeable host reference of the variable.
T * operator->()
Get the writeable pointer of the stored host reference.
KOKKOS_FUNCTION auto operator()(Args &&... args) const -> decltype(auto)
Forward arguments to the stored variable&#39;s const operator() depending on the architecture this functi...
KOKKOS_FUNCTION const T & operator*() const
Get the const reference of the stored variable.
const T * operator->() const
Get the const pointer of the stored host reference.
ReferenceWrapper(const ReferenceWrapper< T > &object)
Copy constructor.
const T _copy
Device copy of the variable.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
const T & operator*() const
Get the const reference of the stored host reference.
auto operator()(Args &&... args) -> decltype(auto)
Forward arguments to the stored host reference&#39;s operator()
T & operator*()
Get the writeable reference of the stored host reference.
ReferenceWrapper(T &reference)
Constructor.