Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 Christian R. Trott (crtrott@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 
86 template< unsigned int maxT = 0 /* Max threads per block */
87  , unsigned int minB = 0 /* Min blocks per SM */
88  >
90 {
93  static unsigned int constexpr maxTperB {maxT};
94  static unsigned int constexpr minBperSM {minB};
95 };
96 
97 } // namespace Kokkos
98 
99 //----------------------------------------------------------------------------
100 //----------------------------------------------------------------------------
101 
102 namespace Kokkos {
103 
104 #define KOKKOS_IMPL_IS_CONCEPT( CONCEPT ) \
105  template< typename T > struct is_ ## CONCEPT { \
106  private: \
107  template< typename , typename = std::true_type > struct have : std::false_type {}; \
108  template< typename U > struct have<U,typename std::is_same< \
109  typename std::remove_cv<U>::type, \
110  typename std::remove_cv<typename U:: CONCEPT>::type \
111  >::type> : std::true_type {}; \
112  public: \
113  enum { value = is_ ## CONCEPT::template have<T>::value }; \
114  };
115 
116 // Public concept:
117 
118 KOKKOS_IMPL_IS_CONCEPT( memory_space )
119 KOKKOS_IMPL_IS_CONCEPT( memory_traits )
120 KOKKOS_IMPL_IS_CONCEPT( execution_space )
121 KOKKOS_IMPL_IS_CONCEPT( execution_policy )
122 KOKKOS_IMPL_IS_CONCEPT( array_layout )
123 KOKKOS_IMPL_IS_CONCEPT( reducer )
124 
125 namespace Impl {
126 
127 // For backward compatibility:
128 
129 using Kokkos::is_memory_space ;
130 using Kokkos::is_memory_traits ;
131 using Kokkos::is_execution_space ;
132 using Kokkos::is_execution_policy ;
133 using Kokkos::is_array_layout ;
134 
135 // Implementation concept:
136 
137 KOKKOS_IMPL_IS_CONCEPT( iteration_pattern )
138 KOKKOS_IMPL_IS_CONCEPT( schedule_type )
139 KOKKOS_IMPL_IS_CONCEPT( index_type )
140 KOKKOS_IMPL_IS_CONCEPT( launch_bounds )
141 
142 }
143 
144 #undef KOKKOS_IMPL_IS_CONCEPT
145 
146 } // namespace Kokkos
147 
148 //----------------------------------------------------------------------------
149 
150 namespace Kokkos {
151 
152 template< class ExecutionSpace , class MemorySpace >
153 struct Device {
154  static_assert( Kokkos::is_execution_space<ExecutionSpace>::value
155  , "Execution space is not valid" );
156  static_assert( Kokkos::is_memory_space<MemorySpace>::value
157  , "Memory space is not valid" );
158  typedef ExecutionSpace execution_space;
159  typedef MemorySpace memory_space;
160  typedef Device<execution_space,memory_space> device_type;
161 };
162 
163 
164 template< typename T >
165 struct is_space {
166 private:
167 
168  template< typename , typename = void >
169  struct exe : std::false_type { typedef void space ; };
170 
171  template< typename , typename = void >
172  struct mem : std::false_type { typedef void space ; };
173 
174  template< typename , typename = void >
175  struct dev : std::false_type { typedef void space ; };
176 
177  template< typename U >
178  struct exe<U,typename std::conditional<true,void,typename U::execution_space>::type>
179  : std::is_same<U,typename U::execution_space>::type
180  { typedef typename U::execution_space space ; };
181 
182  template< typename U >
183  struct mem<U,typename std::conditional<true,void,typename U::memory_space>::type>
184  : std::is_same<U,typename U::memory_space>::type
185  { typedef typename U::memory_space space ; };
186 
187  template< typename U >
188  struct dev<U,typename std::conditional<true,void,typename U::device_type>::type>
189  : std::is_same<U,typename U::device_type>::type
190  { typedef typename U::device_type space ; };
191 
192  typedef typename is_space::template exe<T> is_exe ;
193  typedef typename is_space::template mem<T> is_mem ;
194  typedef typename is_space::template dev<T> is_dev ;
195 
196 public:
197 
198  enum { value = is_exe::value || is_mem::value || is_dev::value };
199 
200  typedef typename is_exe::space execution_space ;
201  typedef typename is_mem::space memory_space ;
202 
203  // For backward compatibility, deprecated in favor of
204  // Kokkos::Impl::HostMirror<S>::host_mirror_space
205 
206  typedef typename std::conditional
207  < std::is_same< memory_space , Kokkos::HostSpace >::value
208 #if defined( KOKKOS_ENABLE_CUDA )
209  || std::is_same< memory_space , Kokkos::CudaUVMSpace >::value
210  || std::is_same< memory_space , Kokkos::CudaHostPinnedSpace >::value
211 #endif /* #if defined( KOKKOS_ENABLE_CUDA ) */
212  , memory_space
214  >::type host_memory_space ;
215 
216 #if defined( KOKKOS_ENABLE_CUDA )
217  typedef typename std::conditional
218  < std::is_same< execution_space , Kokkos::Cuda >::value
219  , Kokkos::DefaultHostExecutionSpace , execution_space
220  >::type host_execution_space ;
221 #else
222  #if defined( KOKKOS_ENABLE_OPENMPTARGET )
223  typedef typename std::conditional
224  < std::is_same< execution_space , Kokkos::Experimental::OpenMPTarget >::value
225  , Kokkos::DefaultHostExecutionSpace , execution_space
226  >::type host_execution_space ;
227  #else
228  typedef execution_space host_execution_space ;
229  #endif
230 #endif
231 
232  typedef typename std::conditional
233  < std::is_same< execution_space , host_execution_space >::value &&
234  std::is_same< memory_space , host_memory_space >::value
235  , T , Kokkos::Device< host_execution_space , host_memory_space >
236  >::type host_mirror_space ;
237 };
238 
239 // For backward compatiblity
240 
241 namespace Impl {
242 
243 using Kokkos::is_space ;
244 
245 }
246 
247 } // namespace Kokkos
248 
249 //----------------------------------------------------------------------------
250 
251 namespace Kokkos {
252 namespace Impl {
253 
259 template< typename DstMemorySpace , typename SrcMemorySpace >
261 
262  static_assert( Kokkos::is_memory_space< DstMemorySpace >::value &&
263  Kokkos::is_memory_space< SrcMemorySpace >::value
264  , "template arguments must be memory spaces" );
265 
273  enum { assignable = std::is_same<DstMemorySpace,SrcMemorySpace>::value };
274 
278  enum { accessible = assignable };
279 
283  enum { deepcopy = assignable };
284 };
285 
286 }} // namespace Kokkos::Impl
287 
288 namespace Kokkos {
289 
309 template< typename AccessSpace , typename MemorySpace >
311 private:
312 
313  static_assert( Kokkos::is_space< AccessSpace >::value
314  , "template argument #1 must be a Kokkos space" );
315 
316  static_assert( Kokkos::is_memory_space< MemorySpace >::value
317  , "template argument #2 must be a Kokkos memory space" );
318 
319  // The input AccessSpace may be a Device<ExecSpace,MemSpace>
320  // verify that it is a valid combination of spaces.
321  static_assert( Kokkos::Impl::MemorySpaceAccess
322  < typename AccessSpace::execution_space::memory_space
323  , typename AccessSpace::memory_space
324  >::accessible
325  , "template argument #1 is an invalid space" );
326 
328  < typename AccessSpace::execution_space::memory_space , MemorySpace >
329  exe_access ;
330 
332  < typename AccessSpace::memory_space , MemorySpace >
333  mem_access ;
334 
335 public:
336 
342  enum { accessible = exe_access::accessible };
343 
349  enum { assignable =
350  is_memory_space< AccessSpace >::value && mem_access::assignable };
351 
353  enum { deepcopy = mem_access::deepcopy };
354 
355  // What intercessory space for AccessSpace::execution_space
356  // to be able to access MemorySpace?
357  // If same memory space or not accessible use the AccessSpace
358  // else construct a device with execution space and memory space.
359  typedef typename std::conditional
360  < std::is_same<typename AccessSpace::memory_space,MemorySpace>::value ||
361  ! exe_access::accessible
362  , AccessSpace
363  , Kokkos::Device< typename AccessSpace::execution_space , MemorySpace >
364  >::type space ;
365 };
366 
367 } // namespace Kokkos
368 
369 namespace Kokkos {
370 namespace Impl {
371 
372 using Kokkos::SpaceAccessibility ; // For backward compatibility
373 
374 }} // namespace Kokkos::Impl
375 
376 //----------------------------------------------------------------------------
377 
378 #endif // KOKKOS_CORE_CONCEPTS_HPP
379 
Can AccessSpace access MemorySpace ?
Memory management for host memory.
Specify Launch Bounds for CUDA execution.
Access relationship between DstMemorySpace and SrcMemorySpace.