GEOS
KernelLaunchSelectors.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_PHYSICSSOLVERS_KERNELLAUNCHSELECTORS_HPP
21 #define GEOS_PHYSICSSOLVERS_KERNELLAUNCHSELECTORS_HPP
22 
23 #include "common/GeosxConfig.hpp"
24 #include "common/logger/Logger.hpp"
25 
26 #include <type_traits>
27 #include <utility>
28 
29 namespace geos
30 {
31 namespace internal
32 {
33 
34 template< typename S, typename T, typename LAMBDA >
35 void invokePhaseDispatchLambda ( S val, T numPhases, LAMBDA && lambda )
36 {
37  if( numPhases == 1 )
38  {
39  lambda( val, std::integral_constant< T, 1 >());
40  return;
41  }
42  else if( numPhases == 2 )
43  {
44  lambda( val, std::integral_constant< T, 2 >());
45  return;
46  }
47  else if( numPhases == 3 )
48  {
49  lambda( val, std::integral_constant< T, 3 >());
50  return;
51  }
52  else
53  {
54  GEOS_ERROR( GEOS_FMT( "Unsupported state: {}", numPhases ) );
55  }
56 }
57 
58 template< typename S, typename T, typename LAMBDA >
59 void invokeThermalDispatchLambda ( S val, T isThermal, LAMBDA && lambda )
60 {
61  if( isThermal == 1 )
62  {
63  lambda( val, std::integral_constant< T, 1 >());
64  return;
65  }
66  else if( isThermal == 0 )
67  {
68  lambda( val, std::integral_constant< T, 0 >());
69  return;
70  }
71  else
72  {
73  GEOS_ERROR( GEOS_FMT( "Unsupported state: {}", isThermal ) );
74  }
75 }
76 
77 template< typename T, typename LAMBDA >
78 void kernelLaunchSelectorThermalSwitch( T value, LAMBDA && lambda )
79 {
80  static_assert( std::is_integral< T >::value, "kernelLaunchSelectorThermalSwitch: type should be integral" );
81 
82  switch( value )
83  {
84  case 0:
85  {
86  lambda( std::integral_constant< T, 0 >() );
87  return;
88  }
89  case 1:
90  {
91  lambda( std::integral_constant< T, 1 >() );
92  return;
93  }
94  default:
95  {
96  GEOS_ERROR( GEOS_FMT( "Unsupported thermal state: {}", value ) );
97  }
98  }
99 }
100 
105 template< typename T, typename LAMBDA >
106 void kernelLaunchSelectorCompSwitch( T value, LAMBDA && lambda )
107 {
108  static_assert( std::is_integral< T >::value, "kernelLaunchSelectorCompSwitch: type should be integral" );
109  switch( value )
110  {
111  #define GEOS_DISPATCH_COMPONENT( NC ) \
112  case NC: \
113  { lambda( std::integral_constant< T, NC >() ); return; \
114  }
115  GEOS_FOR_EACH_COMPONENT( GEOS_DISPATCH_COMPONENT )
116 #undef GEOS_DISPATCH_COMPONENT
117  default:
118  {
119  GEOS_ERROR( GEOS_FMT( "Unsupported number of components: {}. This build instantiates 1 through {} (GEOS_MAX_FLUID_COMPONENTS).",
120  value, GEOS_MAX_FLUID_COMPONENTS ) );
121  }
122  }
123 }
124 
125 template< typename T, typename LAMBDA >
126 void kernelLaunchSelectorCompThermSwitch( T value, bool const isThermal, LAMBDA && lambda )
127 {
128  kernelLaunchSelectorCompSwitch( value, [&] ( auto NC )
129  {
130  invokeThermalDispatchLambda( NC, isThermal, lambda );
131  } );
132 }
133 
134 template< typename T, typename LAMBDA >
135 void kernelLaunchSelectorCompPhaseSwitch( T value, T n_phase, LAMBDA && lambda )
136 {
137  kernelLaunchSelectorCompSwitch( value, [&] ( auto NC )
138  {
139  invokePhaseDispatchLambda( NC, n_phase, lambda );
140  } );
141 }
142 
143 
144 } // end namspace internal
145 } // end namespace geos
146 
147 
148 #endif // GEOS_PHYSICSSOLVERS_KERNELLAUNCHSELECTORS_HPP
#define GEOS_ERROR(...)
Raise a hard error and terminate the program.
Definition: Logger.hpp:220