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 #include "Kokkos_Macros.hpp" // provides some preprocessor values used in definitions of INTREPID2_DEPRECATED, etc.
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 
111 // adapted from Kokkos_Macros.hpp
112 #if defined(KOKKOS_COMPILER_GNU) || defined(KOKKOS_COMPILER_CLANG)
113  #if defined(KOKKOS_COMPILER_CLANG)
114  #define INTREPID2_DEPRECATED_TYPENAME_REPLACEMENT(msg,fixit) __attribute__((deprecated(msg,fixit)))
115  #define INTREPID2_DEPRECATED_TYPENAME_TRAILING_ATTRIBUTE(msg)
116  #else // GNU
117  #if not defined(KOKKOS_ENABLE_CUDA)
118  #define INTREPID2_DEPRECATED_TYPENAME_REPLACEMENT(msg,fixit) __attribute__ ((deprecated(msg)))
119  #define INTREPID2_DEPRECATED_TYPENAME_TRAILING_ATTRIBUTE(msg)
120  // see https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Function-Attributes.html
121  #else // GNU + CUDA
122  // for unknown reasons, the CUDA compilers seem to have trouble with this gcc feature
123  // we therefore disable typedef deprecation warnings on CUDA
124  #define INTREPID2_DEPRECATED_TYPENAME_REPLACEMENT(msg,fixit)
125  #define INTREPID2_DEPRECATED_TYPENAME_TRAILING_ATTRIBUTE(msg)
126  #endif
127  #endif
128 #else
129  // don't issue deprecation warnings on compilers other than gcc and clang (this is not part of the C++11 standard; it is compiler-specific)
130  #define INTREPID2_DEPRECATED_TYPENAME_REPLACEMENT(msg,fixit)
131  #define INTREPID2_DEPRECATED_TYPENAME_TRAILING_ATTRIBUTE(msg)
132 #endif
133 
136  template<typename T>
137  struct ScalarTraits {
138  typedef typename T::scalar_type scalar_type;
139  };
140 
141  // this is built in types to support
145  template<>
146  struct ScalarTraits<float> {
147  typedef float scalar_type;
148  };
152  template<>
153  struct ScalarTraits<double> {
154  typedef double scalar_type;
155  };
159  template<>
160  struct ScalarTraits<int> {
161  typedef int scalar_type;
162  };
166  template<>
167  struct ScalarTraits<long int> {
168  typedef long int scalar_type;
169  };
173  template<>
174  struct ScalarTraits<long long> {
175  typedef long long scalar_type;
176  };
177 
178 
179 
183  template<typename ViewSpaceType, typename UserSpaceType>
184  struct ExecSpace {
185  typedef UserSpaceType ExecSpaceType;
186  };
187 
191  template<typename ViewSpaceType>
192  struct ExecSpace<ViewSpaceType,void> {
193  typedef ViewSpaceType ExecSpaceType;
194  };
195 
196 
200  template <typename ViewType>
201  struct DeduceLayout {
202  using input_layout = typename ViewType::array_layout;
203  using default_layout = typename ViewType::device_type::execution_space::array_layout;
204  using result_layout =
205  typename std::conditional<
206  std::is_same< input_layout, Kokkos::LayoutStride >::value,
207  default_layout,
208  input_layout >::type;
209  };
210 
211 
212  //
213  // utilities device comparible
214  //
215 
216  // this will be gone
217  template<typename IdxType, typename DimType, typename IterType>
218  KOKKOS_FORCEINLINE_FUNCTION
219  static void
220  unrollIndex(IdxType &i, IdxType &j,
221  const DimType /* dim0 */,
222  const DimType dim1,
223  const IterType iter) {
224  // left index
225  //j = iter/dim0;
226  //i = iter%dim0;
227 
228  // right index
229  i = iter/dim1;
230  j = iter%dim1;
231  }
232 
233  template<typename IdxType, typename DimType, typename IterType>
234  KOKKOS_FORCEINLINE_FUNCTION
235  static void
236  unrollIndex(IdxType &i, IdxType &j, IdxType &k,
237  const DimType dim0,
238  const DimType dim1,
239  const DimType dim2,
240  const IterType iter) {
241  IdxType tmp;
242 
243  //unrollIndex(tmp, k, dim0*dim1, dim2, iter);
244  //unrollIndex( i, j, dim0, dim1, tmp);
245 
246  unrollIndex( i, tmp, dim0, dim1*dim2, iter);
247  unrollIndex( j, k, dim1, dim2, tmp);
248  }
249 
253  template<typename T>
254  class Util {
255  public:
256  KOKKOS_FORCEINLINE_FUNCTION
257  static T min(const T a, const T b) {
258  return (a < b ? a : b);
259  }
260 
261  KOKKOS_FORCEINLINE_FUNCTION
262  static T max(const T a, const T b) {
263  return (a > b ? a : b);
264  }
265 
266  KOKKOS_FORCEINLINE_FUNCTION
267  static T abs(const T a) {
268  return (a > 0 ? a : T(-a));
269  }
270 
271  };
272 
273  template<typename T>
274  KOKKOS_FORCEINLINE_FUNCTION
275  static T min(const T &a, const T &b) {
276  return (a < b ? a : b);
277  }
278 
279  template<typename T>
280  KOKKOS_FORCEINLINE_FUNCTION
281  static T max(const T &a, const T &b) {
282  return (a > b ? a : b);
283  }
284 
285  template<typename T>
286  KOKKOS_FORCEINLINE_FUNCTION
287  static T abs(const T &a) {
288  return (a > 0 ? a : T(-a));
289  }
290 
298  template<typename T>
299  KOKKOS_FORCEINLINE_FUNCTION
300  constexpr typename
301  std::enable_if< !std::is_pod<T>::value, typename ScalarTraits<T>::scalar_type >::type
302  get_scalar_value(const T& obj) {return obj.val();}
303 
304  template<typename T>
305  KOKKOS_FORCEINLINE_FUNCTION
306  constexpr typename
307  std::enable_if< std::is_pod<T>::value, typename ScalarTraits<T>::scalar_type >::type
308  get_scalar_value(const T& obj){return obj;}
309 
310 
317  template<typename T, typename ...P>
318  KOKKOS_INLINE_FUNCTION
319  constexpr typename
320  std::enable_if< std::is_pod<T>::value, unsigned >::type
321  dimension_scalar(const Kokkos::DynRankView<T, P...> /* view */) {return 1;}
322 
323  template<typename T, typename ...P>
324  KOKKOS_INLINE_FUNCTION
325  constexpr typename
326  std::enable_if< std::is_pod<T>::value, unsigned >::type
327  dimension_scalar(const Kokkos::View<T, P...> view) {return 1;}
328 
329  template<typename T>
330  KOKKOS_FORCEINLINE_FUNCTION
331  static ordinal_type get_dimension_scalar(const T view) {
332  return dimension_scalar(view);
333  }
334 
335 
336 
337 
338 
339 } // end namespace Intrepid2
340 
341 #endif
small utility functions
scalar type traits
Contains definitions of custom data types in Intrepid2.
layout deduction (temporary meta-function)