Kokkos Core Kernels Package  Version of the Day
Kokkos_Concepts.hpp
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Kokkos v. 2.0
6 // Copyright (2014) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact H. Carter Edwards (hcedwar@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
44 #ifndef KOKKOS_CORE_CONCEPTS_HPP
45 #define KOKKOS_CORE_CONCEPTS_HPP
46 
47 #include <type_traits>
48 
49 // Needed for 'is_space<S>::host_mirror_space
50 #include <Kokkos_Core_fwd.hpp>
51 
52 //----------------------------------------------------------------------------
53 //----------------------------------------------------------------------------
54 
55 namespace Kokkos {
56 
57 //Schedules for Execution Policies
58 struct Static {};
59 struct Dynamic {};
60 
61 //Schedule Wrapper Type
62 template<class T>
63 struct Schedule
64 {
65  static_assert( std::is_same<T,Static>::value
66  || std::is_same<T,Dynamic>::value
67  , "Kokkos: Invalid Schedule<> type."
68  );
69  using schedule_type = Schedule ;
70  using type = T;
71 };
72 
73 //Specify Iteration Index Type
74 template<typename T>
75 struct IndexType
76 {
77  static_assert(std::is_integral<T>::value,"Kokkos: Invalid IndexType<>.");
78  using index_type = IndexType ;
79  using type = T;
80 };
81 
82 } // namespace Kokkos
83 
84 //----------------------------------------------------------------------------
85 //----------------------------------------------------------------------------
86 
87 namespace Kokkos {
88 
89 #define KOKKOS_IMPL_IS_CONCEPT( CONCEPT ) \
90  template< typename T > struct is_ ## CONCEPT { \
91  private: \
92  template< typename , typename = std::true_type > struct have : std::false_type {}; \
93  template< typename U > struct have<U,typename std::is_same<U,typename U:: CONCEPT >::type> : std::true_type {}; \
94  public: \
95  enum { value = is_ ## CONCEPT::template have<T>::value }; \
96  };
97 
98 // Public concept:
99 
100 KOKKOS_IMPL_IS_CONCEPT( memory_space )
101 KOKKOS_IMPL_IS_CONCEPT( memory_traits )
102 KOKKOS_IMPL_IS_CONCEPT( execution_space )
103 KOKKOS_IMPL_IS_CONCEPT( execution_policy )
104 KOKKOS_IMPL_IS_CONCEPT( array_layout )
105 
106 namespace Impl {
107 
108 // For backward compatibility:
109 
110 using Kokkos::is_memory_space ;
111 using Kokkos::is_memory_traits ;
112 using Kokkos::is_execution_space ;
113 using Kokkos::is_execution_policy ;
114 using Kokkos::is_array_layout ;
115 
116 // Implementation concept:
117 
118 KOKKOS_IMPL_IS_CONCEPT( iteration_pattern )
119 KOKKOS_IMPL_IS_CONCEPT( schedule_type )
120 KOKKOS_IMPL_IS_CONCEPT( index_type )
121 
122 }
123 
124 #undef KOKKOS_IMPL_IS_CONCEPT
125 
126 } // namespace Kokkos
127 
128 //----------------------------------------------------------------------------
129 
130 namespace Kokkos {
131 
132 template< class ExecutionSpace , class MemorySpace >
133 struct Device {
134  static_assert( Kokkos::is_execution_space<ExecutionSpace>::value
135  , "Execution space is not valid" );
136  static_assert( Kokkos::is_memory_space<MemorySpace>::value
137  , "Memory space is not valid" );
138  typedef ExecutionSpace execution_space;
139  typedef MemorySpace memory_space;
141 };
142 
143 
144 template< typename T >
145 struct is_space {
146 private:
147 
148  template< typename , typename = void >
149  struct exe : std::false_type { typedef void space ; };
150 
151  template< typename , typename = void >
152  struct mem : std::false_type { typedef void space ; };
153 
154  template< typename , typename = void >
155  struct dev : std::false_type { typedef void space ; };
156 
157  template< typename U >
158  struct exe<U,typename std::conditional<true,void,typename U::execution_space>::type>
159  : std::is_same<U,typename U::execution_space>::type
160  { typedef typename U::execution_space space ; };
161 
162  template< typename U >
163  struct mem<U,typename std::conditional<true,void,typename U::memory_space>::type>
164  : std::is_same<U,typename U::memory_space>::type
165  { typedef typename U::memory_space space ; };
166 
167  template< typename U >
168  struct dev<U,typename std::conditional<true,void,typename U::device_type>::type>
169  : std::is_same<U,typename U::device_type>::type
170  { typedef typename U::device_type space ; };
171 
172  typedef typename is_space::template exe<T> is_exe ;
173  typedef typename is_space::template mem<T> is_mem ;
174  typedef typename is_space::template dev<T> is_dev ;
175 
176 public:
177 
178  enum { value = is_exe::value || is_mem::value || is_dev::value };
179 
180  typedef typename is_exe::space execution_space ;
181  typedef typename is_mem::space memory_space ;
182 
183  // For backward compatibility, deprecated in favor of
184  // Kokkos::Impl::HostMirror<S>::host_mirror_space
185 
186  typedef typename std::conditional
187  < std::is_same< memory_space , Kokkos::HostSpace >::value
188 #if defined( KOKKOS_HAVE_CUDA )
189  || std::is_same< memory_space , Kokkos::CudaUVMSpace >::value
190  || std::is_same< memory_space , Kokkos::CudaHostPinnedSpace >::value
191 #endif /* #if defined( KOKKOS_HAVE_CUDA ) */
192  , memory_space
194  >::type host_memory_space ;
195 
196 #if defined( KOKKOS_HAVE_CUDA )
197  typedef typename std::conditional
198  < std::is_same< execution_space , Kokkos::Cuda >::value
199  , Kokkos::DefaultHostExecutionSpace , execution_space
200  >::type host_execution_space ;
201 #else
202  typedef execution_space host_execution_space ;
203 #endif
204 
205  typedef typename std::conditional
206  < std::is_same< execution_space , host_execution_space >::value &&
207  std::is_same< memory_space , host_memory_space >::value
209  >::type host_mirror_space ;
210 };
211 
212 // For backward compatiblity
213 
214 namespace Impl {
215 
216 using Kokkos::is_space ;
217 
218 }
219 
220 } // namespace Kokkos
221 
222 //----------------------------------------------------------------------------
223 
224 namespace Kokkos {
225 namespace Impl {
226 
232 template< typename DstMemorySpace , typename SrcMemorySpace >
234 
235  static_assert( Kokkos::is_memory_space< DstMemorySpace >::value &&
236  Kokkos::is_memory_space< SrcMemorySpace >::value
237  , "template arguments must be memory spaces" );
238 
246  enum { assignable = std::is_same<DstMemorySpace,SrcMemorySpace>::value };
247 
251  enum { accessible = assignable };
252 
256  enum { deepcopy = assignable };
257 };
258 
259 
279 template< typename AccessSpace , typename MemorySpace >
281 private:
282 
283  static_assert( Kokkos::is_space< AccessSpace >::value
284  , "template argument #1 must be a Kokkos space" );
285 
286  static_assert( Kokkos::Impl::MemorySpaceAccess
287  < typename AccessSpace::execution_space::memory_space
288  , typename AccessSpace::memory_space >::value
289  , "template argument #1 is an invalid space" );
290 
291  static_assert( Kokkos::is_memory_space< MemorySpace >::value
292  , "template argument #2 must be a Kokkos memory space" );
293 
295  < typename AccessSpace::execution_space::memory_space , MemorySpace >
296  exe_access ;
297 
299  < typename AccessSpace::memory_space , MemorySpace >
300  mem_access ;
301 
302 public:
303 
309  enum { accessible = exe_access::accessible };
310 
316  enum { assignable =
317  is_memory_space< AccessSpace >::value && mem_access::assignable };
318 
320  enum { deepcopy = mem_access::deepcopy };
321 
322  // What intercessory space for AccessSpace::execution_space
323  // to be able to access MemorySpace?
324  // If same memory space or not accessible use the AccessSpace
325  // else construct a device with execution space and memory space.
326  typedef typename std::conditional
327  < std::is_same<typename AccessSpace::memory_space,MemorySpace>::value ||
328  ! exe_access::accessible
329  , AccessSpace
331  >::type space ;
332 };
333 
334 }} // namespace Kokkos::Impl
335 
336 //----------------------------------------------------------------------------
337 
338 #endif // KOKKOS_CORE_CONCEPTS_HPP
339 
Can AccessSpace access MemorySpace ?
Memory space for main process and CPU execution spaces.
Memory management for host memory.
Access relationship between DstMemorySpace and SrcMemorySpace.