LvArray
Macros.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, Lawrence Livermore National Security, LLC and LvArray contributors.
3  * All rights reserved.
4  * See the LICENSE file for details.
5  * SPDX-License-Identifier: (BSD-3-Clause)
6  */
7 
13 #pragma once
14 
15 // Source includes
16 #include "LvArrayConfig.hpp"
17 #include "system.hpp"
18 
19 // System includes
20 #include <fstream>
21 #include <sstream>
22 #include <iostream>
23 #include <type_traits>
24 
25 #if defined(LVARRAY_USE_CUDA)
26  #include <cassert>
27 #endif
28 
33 #define STRINGIZE_NX( A ) #A
34 
39 #define STRINGIZE( A ) STRINGIZE_NX( A )
40 
45 #define LVARRAY_UNUSED_ARG( X )
46 
51 #define LVARRAY_UNUSED_VARIABLE( X ) ( ( void ) X )
52 
57 #define LVARRAY_DEBUG_VAR( X ) LVARRAY_UNUSED_VARIABLE( X )
58 
60 #define LOCATION __FILE__ ":" STRINGIZE( __LINE__ )
61 
66 #define TYPEOFPTR( X ) std::remove_pointer_t< decltype( X ) >
67 
72 #define TYPEOFREF( X ) std::remove_reference_t< decltype( X ) >
73 
77 #define LVARRAY_LOG( ... ) std::cout << __VA_ARGS__ << std::endl
78 
82 #define LVARRAY_LOG_VAR( ... ) LVARRAY_LOG( STRINGIZE( __VA_ARGS__ ) << " = " << __VA_ARGS__ )
83 
94 #if defined(__CUDA_ARCH__)
95  #if !defined(NDEBUG)
96 #define LVARRAY_ERROR_IF( EXP, MSG ) \
97  do \
98  { \
99  if( EXP ) \
100  { \
101  assert( false && "EXP = " STRINGIZE( EXP ) "MSG = " STRINGIZE( MSG ) ); \
102  } \
103  } while( false )
104  #else
105 #define LVARRAY_ERROR_IF( EXP, MSG ) \
106  do \
107  { \
108  if( EXP ) \
109  { \
110  constexpr char const * formatString = "***** ERROR\n" \
111  "***** LOCATION: " LOCATION "\n" \
112  "***** Block: [%u, %u, %u]\n" \
113  "***** Thread: [%u, %u, %u]\n" \
114  "***** Controlling expression (should be false): " STRINGIZE( EXP ) "\n" \
115  "***** MSG: " STRINGIZE( MSG ) "\n\n"; \
116  printf( formatString, blockIdx.x, blockIdx.y, blockIdx.z, threadIdx.x, threadIdx.y, threadIdx.z ); \
117  asm ( "trap;" ); \
118  } \
119  } while( false )
120  #endif
121 #else
122 #define LVARRAY_ERROR_IF( EXP, MSG ) \
123  do \
124  { \
125  if( EXP ) \
126  { \
127  std::ostringstream __oss; \
128  __oss << "***** ERROR\n"; \
129  __oss << "***** LOCATION: " LOCATION "\n"; \
130  __oss << "***** Controlling expression (should be false): " STRINGIZE( EXP ) "\n"; \
131  __oss << MSG << "\n"; \
132  __oss << LvArray::system::stackTrace( true ); \
133  std::cout << __oss.str() << std::endl; \
134  LvArray::system::callErrorHandler(); \
135  } \
136  } while( false )
137 #endif
138 
143 #define LVARRAY_ERROR( MSG ) LVARRAY_ERROR_IF( true, MSG )
144 
156 #if !defined(NDEBUG)
157 #define LVARRAY_ASSERT_MSG( EXP, MSG ) LVARRAY_ERROR_IF( !(EXP), MSG )
158 #else
159 #define LVARRAY_ASSERT_MSG( EXP, MSG ) ((void) 0)
160 #endif
161 
168 #define LVARRAY_THROW_IF( EXP, MSG, TYPE ) \
169  do \
170  { \
171  if( EXP ) \
172  { \
173  std::ostringstream __oss; \
174  __oss << "\n"; \
175  __oss << "***** LOCATION: " LOCATION "\n"; \
176  __oss << "***** Controlling expression (should be false): " STRINGIZE( EXP ) "\n"; \
177  __oss << MSG << "\n"; \
178  __oss << LvArray::system::stackTrace( true ); \
179  throw TYPE( __oss.str() ); \
180  } \
181  } while( false )
182 
184 #define LVARRAY_ASSERT( EXP ) LVARRAY_ASSERT_MSG( EXP, "" )
185 
191 #define LVARRAY_WARNING_IF( EXP, MSG ) \
192  do \
193  { \
194  if( EXP ) \
195  { \
196  std::ostringstream __oss; \
197  __oss << "***** WARNING\n"; \
198  __oss << "***** LOCATION: " LOCATION "\n"; \
199  __oss << "***** Controlling expression (should be false): " STRINGIZE( EXP ) "\n"; \
200  __oss << MSG; \
201  std::cout << __oss.str() << std::endl; \
202  } \
203  } while( false )
204 
209 #define LVARRAY_WARNING( MSG ) LVARRAY_WARNING_IF( true, MSG )
210 
216 #define LVARRAY_INFO_IF( EXP, MSG ) \
217  do \
218  { \
219  if( EXP ) \
220  { \
221  std::ostringstream __oss; \
222  __oss << "***** INFO\n"; \
223  __oss << "***** LOCATION: " LOCATION "\n"; \
224  __oss << "***** Controlling expression: " STRINGIZE( EXP ) "\n"; \
225  __oss << MSG; \
226  std::cout << __oss.str() << std::endl; \
227  } \
228  } while( false )
229 
234 #define LVARRAY_INFO( msg ) LVARRAY_INFO_IF( true, msg )
235 
244 #define LVARRAY_ERROR_IF_OP_MSG( lhs, OP, NOP, rhs, msg ) \
245  LVARRAY_ERROR_IF( lhs OP rhs, \
246  msg << "\n" << \
247  "Expected " << #lhs << " " << #NOP << " " << #rhs << "\n" << \
248  " " << #lhs << " = " << lhs << "\n" << \
249  " " << #rhs << " = " << rhs << "\n" )
250 
260 #define LVARRAY_THROW_IF_OP_MSG( lhs, OP, NOP, rhs, msg, TYPE ) \
261  LVARRAY_THROW_IF( lhs OP rhs, \
262  msg << "\n" << \
263  "Expected " << #lhs << " " << #NOP << " " << #rhs << "\n" << \
264  " " << #lhs << " = " << lhs << "\n" << \
265  " " << #rhs << " = " << rhs << "\n", TYPE )
266 
273 #define LVARRAY_ERROR_IF_EQ_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_OP_MSG( lhs, ==, !=, rhs, msg )
274 
282 #define LVARRAY_THROW_IF_EQ_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_OP_MSG( lhs, ==, !=, rhs, msg, TYPE )
283 
289 #define LVARRAY_ERROR_IF_EQ( lhs, rhs ) LVARRAY_ERROR_IF_EQ_MSG( lhs, rhs, "" )
290 
297 #define LVARRAY_THROW_IF_EQ( lhs, rhs, TYPE ) LVARRAY_THROW_IF_EQ_MSG( lhs, rhs, "", TYPE )
298 
305 #define LVARRAY_ERROR_IF_NE_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_OP_MSG( lhs, !=, ==, rhs, msg )
306 
314 #define LVARRAY_THROW_IF_NE_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_OP_MSG( lhs, !=, ==, rhs, msg, TYPE )
315 
321 #define LVARRAY_ERROR_IF_NE( lhs, rhs ) LVARRAY_ERROR_IF_NE_MSG( lhs, rhs, "" )
322 
329 #define LVARRAY_THROW_IF_NE( lhs, rhs, TYPE ) LVARRAY_THROW_IF_NE_MSG( lhs, rhs, "", TYPE )
330 
337 #define LVARRAY_ERROR_IF_GT_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_OP_MSG( lhs, >, <=, rhs, msg )
338 
346 #define LVARRAY_THROW_IF_GT_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_OP_MSG( lhs, >, <=, rhs, msg, TYPE )
347 
353 #define LVARRAY_ERROR_IF_GT( lhs, rhs ) LVARRAY_ERROR_IF_GT_MSG( lhs, rhs, "" )
354 
361 #define LVARRAY_THROW_IF_GT( lhs, rhs, TYPE ) LVARRAY_THROW_IF_GT_MSG( lhs, rhs, "", TYPE )
362 
369 #define LVARRAY_ERROR_IF_GE_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_OP_MSG( lhs, >=, <, rhs, msg )
370 
378 #define LVARRAY_THROW_IF_GE_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_OP_MSG( lhs, >=, <, rhs, msg, TYPE )
379 
385 #define LVARRAY_ERROR_IF_GE( lhs, rhs ) LVARRAY_ERROR_IF_GE_MSG( lhs, rhs, "" )
386 
393 #define LVARRAY_THROW_IF_GE( lhs, rhs, TYPE ) LVARRAY_THROW_IF_GE_MSG( lhs, rhs, "", TYPE )
394 
401 #define LVARRAY_ERROR_IF_LT_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_OP_MSG( lhs, <, >=, rhs, msg )
402 
410 #define LVARRAY_THROW_IF_LT_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_OP_MSG( lhs, <, >=, rhs, msg, TYPE )
411 
417 #define LVARRAY_ERROR_IF_LT( lhs, rhs ) LVARRAY_ERROR_IF_LT_MSG( lhs, rhs, "" )
418 
425 #define LVARRAY_THROW_IF_LT( lhs, rhs, TYPE ) LVARRAY_THROW_IF_LT_MSG( lhs, rhs, "", TYPE )
426 
433 #define LVARRAY_ERROR_IF_LE_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_OP_MSG( lhs, <=, >, rhs, msg )
434 
442 #define LVARRAY_THROW_IF_LE_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_OP_MSG( lhs, <=, >, rhs, msg, TYPE )
443 
449 #define LVARRAY_ERROR_IF_LE( lhs, rhs ) LVARRAY_ERROR_IF_GE_MSG( lhs, rhs, "" )
450 
457 #define LVARRAY_THROW_IF_LE( lhs, rhs, TYPE ) LVARRAY_THROW_IF_GE_MSG( lhs, rhs, "", TYPE )
458 
466 #define LVARRAY_ASSERT_OP_MSG( lhs, OP, rhs, msg ) \
467  LVARRAY_ASSERT_MSG( lhs OP rhs, \
468  msg << "\n" << \
469  " " << #lhs << " = " << lhs << "\n" << \
470  " " << #rhs << " = " << rhs << "\n" )
471 
478 #define LVARRAY_ASSERT_EQ_MSG( lhs, rhs, msg ) LVARRAY_ASSERT_OP_MSG( lhs, ==, rhs, msg )
479 
485 #define LVARRAY_ASSERT_EQ( lhs, rhs ) LVARRAY_ASSERT_EQ_MSG( lhs, rhs, "" )
486 
493 #define LVARRAY_ASSERT_NE_MSG( lhs, rhs, msg ) LVARRAY_ASSERT_OP_MSG( lhs, !=, rhs, msg )
494 
500 #define LVARRAY_ASSERT_NE( lhs, rhs ) LVARRAY_ASSERT_NE_MSG( lhs, rhs, "" )
501 
508 #define LVARRAY_ASSERT_GT_MSG( lhs, rhs, msg ) LVARRAY_ASSERT_OP_MSG( lhs, >, rhs, msg )
509 
515 #define LVARRAY_ASSERT_GT( lhs, rhs ) LVARRAY_ASSERT_GT_MSG( lhs, rhs, "" )
516 
523 #define LVARRAY_ASSERT_GE_MSG( lhs, rhs, msg ) LVARRAY_ASSERT_OP_MSG( lhs, >=, rhs, msg )
524 
530 #define LVARRAY_ASSERT_GE( lhs, rhs ) LVARRAY_ASSERT_GE_MSG( lhs, rhs, "" )
531 
532 #if defined(LVARRAY_USE_CUDA) && defined(__CUDACC__)
533 #define LVARRAY_HOST_DEVICE __host__ __device__
535 
537 #define LVARRAY_DEVICE __device__
538 
546 #define DISABLE_HD_WARNING _Pragma("hd_warning_disable")
547 #else
548 #define LVARRAY_HOST_DEVICE
550 
552 #define LVARRAY_DEVICE
553 
561 #define DISABLE_HD_WARNING
562 #endif
563 
564 
565 #if defined(__clang__)
566 #define LVARRAY_RESTRICT __restrict__
567 #define LVARRAY_RESTRICT_REF __restrict__
568 #define LVARRAY_INTEL_CONSTEXPR constexpr
569 #elif defined(__GNUC__)
570  #if defined(__INTEL_COMPILER)
571 #define LVARRAY_RESTRICT __restrict__
572 #define LVARRAY_RESTRICT_REF __restrict__
573 #define LVARRAY_INTEL_CONSTEXPR
574  #else
575 #define LVARRAY_RESTRICT __restrict__
576 #define LVARRAY_RESTRICT_REF __restrict__
577 #define LVARRAY_INTEL_CONSTEXPR constexpr
578  #endif
579 #endif
580 
581 #if !defined(LVARRAY_BOUNDS_CHECK)
582 
585 #define CONSTEXPR_WITHOUT_BOUNDS_CHECK constexpr
586 #else
587 
590 #define CONSTEXPR_WITHOUT_BOUNDS_CHECK
591 #endif
592 
593 #if defined(NDEBUG)
594 
597 #define CONSTEXPR_WITH_NDEBUG constexpr
598 #else
599 
602 #define CONSTEXPR_WITH_NDEBUG
603 #endif
604 
605 #if !defined(LVARRAY_BOUNDS_CHECK)
606 
609 #define CONSTEXPR_WITHOUT_BOUNDS_CHECK constexpr
610 #else
611 
614 #define CONSTEXPR_WITHOUT_BOUNDS_CHECK
615 #endif
616 
617 #if defined(NDEBUG)
618 
621 #define CONSTEXPR_WITH_NDEBUG constexpr
622 #else
623 
626 #define CONSTEXPR_WITH_NDEBUG
627 #endif
Contains functions that interact with the system or runtime environment.