Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
Morpheus_Spaces.hpp
1
24#ifndef MORPHEUS_SPACES_HPP
25#define MORPHEUS_SPACES_HPP
26
27#include <Morpheus_CustomBackend.hpp>
28#include <Morpheus_GenericBackend.hpp>
29
30namespace Morpheus {
38template <class ExecutionSpace, class MemorySpace, class BackendSpace>
39struct Device {
41 "ExecutionSpace does not have a valid execution_space trait");
43 "MemorySpace does not have a valid memory_space trait");
46 "BackendSpace must be either a custom or generic backend.");
47 using backend = typename BackendSpace::backend;
49 typename ExecutionSpace::execution_space;
51 typename MemorySpace::memory_space;
52 using device_type =
53 Device<execution_space, memory_space, backend>; // The type of the device
54};
55
61template <class T>
63 typedef char yes[1];
64 typedef char no[2];
65
66 template <class U>
67 static yes& test(
68 U*, typename std::enable_if<
69 is_custom_backend_v<typename U::backend> ||
70 is_generic_backend_v<typename U::backend>>::type* = nullptr);
71
72 template <class U>
73 static no& test(...);
74
75 public:
76 static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
77};
78
84template <typename T>
85inline constexpr bool has_backend_v = has_backend<T>::value;
86
93template <typename S>
94struct HostMirror {
95 private:
96 // If input execution space can access HostSpace then keep it.
97 // Example: Kokkos::OpenMP can access, Kokkos::Cuda cannot
98 enum {
99 keep_exe = Kokkos::Impl::MemorySpaceAccess<
100 typename S::execution_space::memory_space,
101 Kokkos::HostSpace>::accessible
102 };
103
104 // If HostSpace can access memory space then keep it.
105 // Example: Cannot access Kokkos::CudaSpace, can access Kokkos::CudaUVMSpace
106 enum {
107 keep_mem =
108 Kokkos::Impl::MemorySpaceAccess<Kokkos::HostSpace,
109 typename S::memory_space>::accessible
110 };
111
112 using wrapped_space = typename std::conditional<
113 is_execution_space_v<S> || is_memory_space_v<S> ||
114 (Kokkos::is_device<S>::value && !has_backend<S>::value),
116
117 public:
118 using backend = typename std::conditional<
119 keep_exe && keep_mem,
120 wrapped_space, // Already on host (Serial or OpenMP)
121 typename std::conditional<
122 keep_mem,
123 Morpheus::Device<Kokkos::HostSpace::execution_space,
124 typename wrapped_space::memory_space, wrapped_space>,
125 Morpheus::HostSpace>::type>::type; // Cuda or HIP
126};
127
128} // namespace Morpheus
129
130#endif // MORPHEUS_SPACES_HPP
Checks if the given type T has a valid supported backend.
Definition: Morpheus_Spaces.hpp:62
Checks if the given type T has a valid supported execution space.
Definition: Morpheus_SpaceTraits.hpp:154
Checks if the given type T has a valid supported memory space.
Definition: Morpheus_SpaceTraits.hpp:81
Checks if the given type T is a valid generic backend i.e is a GenericBackend container....
Definition: Morpheus_GenericBackend.hpp:171
typename Impl::is_custom_backend_helper< typename std::remove_cv< T >::type >::type is_custom_backend
Checks if the given type T is a valid custom backend i.e is a CustomBackend container.
Definition: Morpheus_CustomBackend.hpp:211
Generic Morpheus interfaces.
Definition: dummy.cpp:24
constexpr bool has_backend_v
Short-hand to has_backend.
Definition: Morpheus_Spaces.hpp:85
A wrapper that converts a valid space into a custom backend.
Definition: Morpheus_CustomBackend.hpp:75
A type that binds together the execution, memory space and backend.
Definition: Morpheus_Spaces.hpp:39
typename BackendSpace::backend backend
The type of backend.
Definition: Morpheus_Spaces.hpp:47
typename MemorySpace::memory_space memory_space
Memory Space for the data.
Definition: Morpheus_Spaces.hpp:51
typename ExecutionSpace::execution_space execution_space
Execution Space to run in.
Definition: Morpheus_Spaces.hpp:49
A wrapper that converts a valid space into a generic backend.
Definition: Morpheus_GenericBackend.hpp:68
Given a space S, the HostMirror will generate the appropriate Host backend.
Definition: Morpheus_Spaces.hpp:94