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 #include "KokkosArray.h"
13 
14 #include "MooseTypes.h"
15 
16 namespace Moose::Kokkos
17 {
18 
30 template <typename T>
32 {
33 public:
38  ReferenceWrapper(T & reference) : _reference(reference), _copy(reference) {}
43  : _reference(object._reference), _copy(object._reference)
44  {
45  }
46 
47 #ifdef MOOSE_KOKKOS_SCOPE
48 
53  KOKKOS_FUNCTION operator const T &() const
54  {
55  KOKKOS_IF_ON_HOST(return _reference;)
56 
57  return _copy;
58  }
64  KOKKOS_FUNCTION const T & operator*() const
65  {
66  KOKKOS_IF_ON_HOST(return _reference;)
67 
68  return _copy;
69  }
75  KOKKOS_FUNCTION const T * operator->() const
76  {
77  KOKKOS_IF_ON_HOST(return &_reference;)
78 
79  return &_copy;
80  }
86  template <typename... Args>
87  KOKKOS_FUNCTION auto operator()(Args &&... args) const -> decltype(auto)
88  {
89  KOKKOS_IF_ON_HOST(return _reference(std::forward<Args>(args)...);)
90 
91  return _copy(std::forward<Args>(args)...);
92  }
93 #else
94 
98  operator const T &() const { return _reference; }
103  const T & operator*() const { return _reference; }
108  const T * operator->() const { return &_reference; }
113  template <typename... Args>
114  auto operator()(Args &&... args) const -> decltype(auto)
115  {
116  return _reference(std::forward<Args>(args)...);
117  }
118 #endif
119 
123  operator T &() { return _reference; }
128  T & operator*() { return _reference; }
133  T * operator->() { return &_reference; }
138  template <typename... Args>
139  auto operator()(Args &&... args) -> decltype(auto)
140  {
141  return _reference(std::forward<Args>(args)...);
142  }
143 
144 protected:
152  const T _copy;
153 };
154 
155 template <typename T>
157 {
158  static constexpr bool value = true;
159 };
160 
161 } // namespace Moose::Kokkos
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.
The type trait that determines the default behavior of copy constructor and deepCopy() If this type t...
Definition: KokkosArray.h:90
const T _copy
Device copy of the variable.
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.
static constexpr bool value
Definition: KokkosArray.h:92