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 KOKKOS_IMPL_IS_CONCEPT( reducer )
106 
107 namespace Impl {
108 
109 // For backward compatibility:
110 
111 using Kokkos::is_memory_space ;
112 using Kokkos::is_memory_traits ;
113 using Kokkos::is_execution_space ;
114 using Kokkos::is_execution_policy ;
115 using Kokkos::is_array_layout ;
116 
117 // Implementation concept:
118 
119 KOKKOS_IMPL_IS_CONCEPT( iteration_pattern )
120 KOKKOS_IMPL_IS_CONCEPT( schedule_type )
121 KOKKOS_IMPL_IS_CONCEPT( index_type )
122 
123 }
124 
125 #undef KOKKOS_IMPL_IS_CONCEPT
126 
127 } // namespace Kokkos
128 
129 //----------------------------------------------------------------------------
130 
131 namespace Kokkos {
132 
133 template< class ExecutionSpace , class MemorySpace >
134 struct Device {
135  static_assert( Kokkos::is_execution_space<ExecutionSpace>::value
136  , "Execution space is not valid" );
137  static_assert( Kokkos::is_memory_space<MemorySpace>::value
138  , "Memory space is not valid" );
139  typedef ExecutionSpace execution_space;
140  typedef MemorySpace memory_space;
142 };
143 
144 
145 template< typename T >
146 struct is_space {
147 private:
148 
149  template< typename , typename = void >
150  struct exe : std::false_type { typedef void space ; };
151 
152  template< typename , typename = void >
153  struct mem : std::false_type { typedef void space ; };
154 
155  template< typename , typename = void >
156  struct dev : std::false_type { typedef void space ; };
157 
158  template< typename U >
159  struct exe<U,typename std::conditional<true,void,typename U::execution_space>::type>
160  : std::is_same<U,typename U::execution_space>::type
161  { typedef typename U::execution_space space ; };
162 
163  template< typename U >
164  struct mem<U,typename std::conditional<true,void,typename U::memory_space>::type>
165  : std::is_same<U,typename U::memory_space>::type
166  { typedef typename U::memory_space space ; };
167 
168  template< typename U >
169  struct dev<U,typename std::conditional<true,void,typename U::device_type>::type>
170  : std::is_same<U,typename U::device_type>::type
171  { typedef typename U::device_type space ; };
172 
173  typedef typename is_space::template exe<T> is_exe ;
174  typedef typename is_space::template mem<T> is_mem ;
175  typedef typename is_space::template dev<T> is_dev ;
176 
177 public:
178 
179  enum { value = is_exe::value || is_mem::value || is_dev::value };
180 
181  typedef typename is_exe::space execution_space ;
182  typedef typename is_mem::space memory_space ;
183 
184  // For backward compatibility, deprecated in favor of
185  // Kokkos::Impl::HostMirror<S>::host_mirror_space
186 
187  typedef typename std::conditional
188  < std::is_same< memory_space , Kokkos::HostSpace >::value
189 #if defined( KOKKOS_ENABLE_CUDA )
190  || std::is_same< memory_space , Kokkos::CudaUVMSpace >::value
191  || std::is_same< memory_space , Kokkos::CudaHostPinnedSpace >::value
192 #endif /* #if defined( KOKKOS_ENABLE_CUDA ) */
193  , memory_space
195  >::type host_memory_space ;
196 
197 #if defined( KOKKOS_ENABLE_CUDA )
198  typedef typename std::conditional
199  < std::is_same< execution_space , Kokkos::Cuda >::value
200  , Kokkos::DefaultHostExecutionSpace , execution_space
201  >::type host_execution_space ;
202 #else
203  #if defined( KOKKOS_ENABLE_OPENMPTARGET )
204  typedef typename std::conditional
205  < std::is_same< execution_space , Kokkos::Experimental::OpenMPTarget >::value
206  , Kokkos::DefaultHostExecutionSpace , execution_space
207  >::type host_execution_space ;
208  #else
209  typedef execution_space host_execution_space ;
210  #endif
211 #endif
212 
213  typedef typename std::conditional
214  < std::is_same< execution_space , host_execution_space >::value &&
215  std::is_same< memory_space , host_memory_space >::value
217  >::type host_mirror_space ;
218 };
219 
220 // For backward compatiblity
221 
222 namespace Impl {
223 
224 using Kokkos::is_space ;
225 
226 }
227 
228 } // namespace Kokkos
229 
230 //----------------------------------------------------------------------------
231 
232 namespace Kokkos {
233 namespace Impl {
234 
240 template< typename DstMemorySpace , typename SrcMemorySpace >
242 
243  static_assert( Kokkos::is_memory_space< DstMemorySpace >::value &&
244  Kokkos::is_memory_space< SrcMemorySpace >::value
245  , "template arguments must be memory spaces" );
246 
254  enum { assignable = std::is_same<DstMemorySpace,SrcMemorySpace>::value };
255 
259  enum { accessible = assignable };
260 
264  enum { deepcopy = assignable };
265 };
266 
267 
287 template< typename AccessSpace , typename MemorySpace >
289 private:
290 
291  static_assert( Kokkos::is_space< AccessSpace >::value
292  , "template argument #1 must be a Kokkos space" );
293 
294  static_assert( Kokkos::is_memory_space< MemorySpace >::value
295  , "template argument #2 must be a Kokkos memory space" );
296 
297  // The input AccessSpace may be a Device<ExecSpace,MemSpace>
298  // verify that it is a valid combination of spaces.
299  static_assert( Kokkos::Impl::MemorySpaceAccess
300  < typename AccessSpace::execution_space::memory_space
301  , typename AccessSpace::memory_space
302  >::accessible
303  , "template argument #1 is an invalid space" );
304 
306  < typename AccessSpace::execution_space::memory_space , MemorySpace >
307  exe_access ;
308 
310  < typename AccessSpace::memory_space , MemorySpace >
311  mem_access ;
312 
313 public:
314 
320  enum { accessible = exe_access::accessible };
321 
327  enum { assignable =
328  is_memory_space< AccessSpace >::value && mem_access::assignable };
329 
331  enum { deepcopy = mem_access::deepcopy };
332 
333  // What intercessory space for AccessSpace::execution_space
334  // to be able to access MemorySpace?
335  // If same memory space or not accessible use the AccessSpace
336  // else construct a device with execution space and memory space.
337  typedef typename std::conditional
338  < std::is_same<typename AccessSpace::memory_space,MemorySpace>::value ||
339  ! exe_access::accessible
340  , AccessSpace
342  >::type space ;
343 };
344 
345 }} // namespace Kokkos::Impl
346 
347 //----------------------------------------------------------------------------
348 
349 #endif // KOKKOS_CORE_CONCEPTS_HPP
350 
Can AccessSpace access MemorySpace ?
Memory space for main process and CPU execution spaces.
Memory management for host memory.
Access relationship between DstMemorySpace and SrcMemorySpace.