Intrepid2
Intrepid2_Utils.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid2 Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Kyungjoo Kim (kyukim@sandia.gov), or
38 // Mauro Perego (mperego@sandia.gov)
39 //
40 // ************************************************************************
41 // @HEADER
42 
49 #ifndef __INTREPID2_UTILS_HPP__
50 #define __INTREPID2_UTILS_HPP__
51 
52 #include "Intrepid2_ConfigDefs.hpp"
53 #include "Intrepid2_Types.hpp"
54 
55 #include "Kokkos_Core.hpp"
56 
57 
58 namespace Intrepid2 {
59 
60 #if defined(KOKKOS_OPT_RANGE_AGGRESSIVE_VECTORIZATION) && defined(KOKKOS_ENABLE_PRAGMA_IVDEP) && !defined(__CUDA_ARCH__)
61 #define INTREPID2_USE_IVDEP
62 #endif
63 
64  //
65  // test macros
66  //
67 
68 #define INTREPID2_TEST_FOR_WARNING(test, msg) \
69  if (test) { \
70  printf("[Intrepid2] Warning in file %s, line %d\n",__FILE__,__LINE__); \
71  printf(" Test that evaluated to true: %s\n", #test); \
72  printf(" %s \n", msg); \
73  }
74 
75 #define INTREPID2_TEST_FOR_EXCEPTION(test, x, msg) \
76  if (test) { \
77  printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
78  printf(" Test that evaluated to true: %s\n", #test); \
79  printf(" %s \n", msg); \
80  throw x(msg); \
81  }
82 
83 #define INTREPID2_TEST_FOR_ABORT(test, msg) \
84  if (test) { \
85  printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
86  printf(" Test that evaluated to true: %s\n", #test); \
87  printf(" %s \n", msg); \
88  Kokkos::abort( "[Intrepid2] Abort\n"); \
89  }
90 
91  // check the first error only
92 #ifdef INTREPID2_TEST_FOR_DEBUG_ABORT_OVERRIDE_TO_CONTINUE
93 #define INTREPID2_TEST_FOR_DEBUG_ABORT(test, info, msg) \
94  if (!(info) && (test)) { \
95  printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
96  printf(" Test that evaluated to true: %s\n", #test); \
97  printf(" %s \n", msg); \
98  info = true; \
99  }
100 #else
101 #define INTREPID2_TEST_FOR_DEBUG_ABORT(test, info, msg) \
102  if (!(info) && (test)) { \
103  printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
104  printf(" Test that evaluated to true: %s\n", #test); \
105  printf(" %s \n", msg); \
106  info = true ; \
107  Kokkos::abort( "[Intrepid2] Abort\n"); \
108  }
109 #endif
110 
114  template<typename T>
115  struct ScalarTraits {
116  typedef typename T::scalar_type scalar_type;
117  };
118 
119  // this is built in types to support
123  template<>
124  struct ScalarTraits<float> {
125  typedef float scalar_type;
126  };
130  template<>
131  struct ScalarTraits<double> {
132  typedef double scalar_type;
133  };
137  template<>
138  struct ScalarTraits<int> {
139  typedef int scalar_type;
140  };
144  template<>
145  struct ScalarTraits<long int> {
146  typedef long int scalar_type;
147  };
151  template<>
152  struct ScalarTraits<long long> {
153  typedef long long scalar_type;
154  };
155 
156 
157 
161  template<typename ViewSpaceType, typename UserSpaceType>
162  struct ExecSpace {
163  typedef UserSpaceType ExecSpaceType;
164  };
165 
169  template<typename ViewSpaceType>
170  struct ExecSpace<ViewSpaceType,void> {
171  typedef ViewSpaceType ExecSpaceType;
172  };
173 
174 
178  template <typename ViewType>
179  struct DeduceLayout {
180  using input_layout = typename ViewType::array_layout;
181  using default_layout = typename ViewType::device_type::execution_space::array_layout;
182  using result_layout =
183  typename std::conditional<
184  std::is_same< input_layout, Kokkos::LayoutStride >::value,
185  default_layout,
186  input_layout >::type;
187  };
188 
189 
190  //
191  // utilities device comparible
192  //
193 
194  // this will be gone
195  template<typename IdxType, typename DimType, typename IterType>
196  KOKKOS_FORCEINLINE_FUNCTION
197  static void
198  unrollIndex(IdxType &i, IdxType &j,
199  const DimType /* dim0 */,
200  const DimType dim1,
201  const IterType iter) {
202  // left index
203  //j = iter/dim0;
204  //i = iter%dim0;
205 
206  // right index
207  i = iter/dim1;
208  j = iter%dim1;
209  }
210 
211  template<typename IdxType, typename DimType, typename IterType>
212  KOKKOS_FORCEINLINE_FUNCTION
213  static void
214  unrollIndex(IdxType &i, IdxType &j, IdxType &k,
215  const DimType dim0,
216  const DimType dim1,
217  const DimType dim2,
218  const IterType iter) {
219  IdxType tmp;
220 
221  //unrollIndex(tmp, k, dim0*dim1, dim2, iter);
222  //unrollIndex( i, j, dim0, dim1, tmp);
223 
224  unrollIndex( i, tmp, dim0, dim1*dim2, iter);
225  unrollIndex( j, k, dim1, dim2, tmp);
226  }
227 
231  template<typename T>
232  class Util {
233  public:
234  KOKKOS_FORCEINLINE_FUNCTION
235  static T min(const T a, const T b) {
236  return (a < b ? a : b);
237  }
238 
239  KOKKOS_FORCEINLINE_FUNCTION
240  static T max(const T a, const T b) {
241  return (a > b ? a : b);
242  }
243 
244  KOKKOS_FORCEINLINE_FUNCTION
245  static T abs(const T a) {
246  return (a > 0 ? a : T(-a));
247  }
248 
249  };
250 
251  template<typename T>
252  KOKKOS_FORCEINLINE_FUNCTION
253  static T min(const T &a, const T &b) {
254  return (a < b ? a : b);
255  }
256 
257  template<typename T>
258  KOKKOS_FORCEINLINE_FUNCTION
259  static T max(const T &a, const T &b) {
260  return (a > b ? a : b);
261  }
262 
263  template<typename T>
264  KOKKOS_FORCEINLINE_FUNCTION
265  static T abs(const T &a) {
266  return (a > 0 ? a : T(-a));
267  }
268 
276  template<typename T>
277  KOKKOS_FORCEINLINE_FUNCTION
278  constexpr typename
279  std::enable_if< !std::is_pod<T>::value, typename ScalarTraits<T>::scalar_type >::type
280  get_scalar_value(const T& obj) {return obj.val();}
281 
282  template<typename T>
283  KOKKOS_FORCEINLINE_FUNCTION
284  constexpr typename
285  std::enable_if< std::is_pod<T>::value, typename ScalarTraits<T>::scalar_type >::type
286  get_scalar_value(const T& obj){return obj;}
287 
288 
295  template<typename T, typename ...P>
296  KOKKOS_INLINE_FUNCTION
297  constexpr typename
298  std::enable_if< std::is_pod<T>::value, unsigned >::type
299  dimension_scalar(const Kokkos::DynRankView<T, P...> /* view */) {return 1;}
300 
301  template<typename T, typename ...P>
302  KOKKOS_INLINE_FUNCTION
303  constexpr typename
304  std::enable_if< std::is_pod<T>::value, unsigned >::type
305  dimension_scalar(const Kokkos::View<T, P...> view) {return 1;}
306 
307  template<typename T>
308  KOKKOS_FORCEINLINE_FUNCTION
309  static ordinal_type get_dimension_scalar(const T view) {
310  return dimension_scalar(view);
311  }
312 
313 
314 
315 
316 
317 } // end namespace Intrepid2
318 
319 #endif
small utility functions
scalar type traits
Contains definitions of custom data types in Intrepid2.
layout deduction (temporary meta-function)