XGC1
globals.hpp
Go to the documentation of this file.
1 #ifndef GLOBALS_HPP
2 #define GLOBALS_HPP
3 #include <limits.h>
4 #include <limits>
5 #include <string>
6 #include <cassert>
7 #include "space_settings.hpp"
8 #include "array_deep_copy.hpp"
9 #include "access_add.hpp"
10 #include "simd.hpp"
11 #ifdef USE_MPI
12 #include "my_mpi.hpp"
13 #endif
14 #include "constants.hpp"
15 
16 /* Returns max number of omp threads, or 1 if no omp
17  */
18 inline int get_num_cpu_threads(){
19 #ifdef USE_OMP
20  return omp_get_max_threads();
21 #else
22  return 1;
23 #endif
24 }
25 
26 /* Return true if MPI rank is zero
27  * */
28 inline bool is_rank_zero(){
29 #ifdef USE_MPI
30  return SML_COMM_RANK==0;
31 #else
32  return true;
33 #endif
34 }
35 
36 /* Safely abort and exit the code
37  * */
38 inline void exit_XGC(std::string msg){
39  printf("%s",msg.c_str());
40  fflush(stdout);
41 #ifdef USE_MPI
42  MPI_Abort(SML_COMM_WORLD, 1);
43 #else
44  exit(1);
45 #endif
46 }
47 
48 /* Asserts condition
49  * (Callable from Device)
50  * (C++ has no standardized assert with a message)
51  * */
52 KOKKOS_INLINE_FUNCTION void assert_XGC(bool cond, const char* msg){
53  if(!cond){
54  DEVICE_PRINTF("%s",msg);
55  assert(false);
56  }
57 }
58 
59 /* Check that two integers multiplied together doesn't cause an overflow
60  * */
61 inline bool causes_multiplication_overflow(int a,int b){
62  int c = a*b;
63  if(b==0) return false; // No overflow
64  return !(c/b == a);
65 }
66 
67 inline bool causes_multiplication_overflow(long long int a,long long int b){
68  if (a==0 || b==0) return false;
69 #if defined(__SIZEOF_INT128__)
70  __int128 prod = static_cast<__int128>(a) * static_cast<__int128>(b);
71  return prod > std::numeric_limits<long long int>::max() || prod < std::numeric_limits<long long int>::lowest();
72 #else
73  if (a > 0){
74  if (b > 0){
75  return a > std::numeric_limits<long long int>::max()/b;
76  } else {
77  return b < std::numeric_limits<long long int>::lowest()/a;
78  }
79  } else {
80  if (b > 0){
81  return a < std::numeric_limits<long long int>::lowest()/b;
82  } else {
83  return a != 0 && b < std::numeric_limits<long long int>::max()/a;
84  }
85  }
86 #endif
87 }
88 
89 /* Check that two integers added together doesn't cause an overflow
90  * */
91 inline bool causes_addition_overflow(int a,int b){
92  if ( ((b > 0) && (a > INT_MAX - b)) // a + b would overflow
93  ||((b < 0) && (a < INT_MIN - b)) ){ // a + b would underflow
94  return true;
95  } else {
96  return false;
97  }
98 }
99 
100 enum class Order{
101  Zero,
102  One,
103  Two
104 };
105 
107  ELECTRON = 0,
108  MAIN_ION = 1
109 };
110 
111 enum KinType{
113  GyroKin=1
114 };
115 
116 /* PhiInterpType specifies whether the field uses two values in order to interpolate in the phi direction
117  * */
118 enum class PhiInterpType{
119  Planes,
120  None
121 };
122 
123 // Eventually, nearly the entire code will be templated on PhiInterpType, which is XGC1 vs XGCa
124 // As an intermediate step, it is convenient to specify the template in one global location
125 #ifdef XGC1
127 #else
129 #endif
130 
131 /* What type of marker weight algorithm to use, reduced delta-f or total-f
132  */
133 enum class MarkerType{
135  FullF,
136  TotalF,
137  None
138 };
139 enum class FAnalyticShape{
140  Maxwellian,
141  SlowingDown,
142  None
143 };
144 enum class WeightEvoEq{
145  Direct,
146  PDE,
147  None
148 };
149 
150 enum class MagneticFieldMode{
153 };
154 #ifdef EXPLICIT_EM
156 #else
158 #endif
159 
160 enum class BFieldSymmetry{
161  Tokamak,
163 };
164 #ifdef STELLARATOR
166 #else
168 #endif
169 
170 /* Whether to use cylindrical limit geometry with periodic boundary function
171  */
172 enum class GeometryType{
173  Toroidal,
175 };
176 #ifdef CYLINDRICAL
178 #else
180 #endif
181 
191 template<GeometryType GT>
192 KOKKOS_INLINE_FUNCTION double geometry_switch(double a, double b);
193 
194 template<>
195 KOKKOS_INLINE_FUNCTION double geometry_switch<GeometryType::Toroidal>(double a, double b){
196  return a;
197 }
198 
199 template<>
200 KOKKOS_INLINE_FUNCTION double geometry_switch<GeometryType::CylindricalLimit>(double a, double b){
201  return b;
202 }
203 
204 
208 template<GeometryType GT>
209 KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms();
210 
211 template<>
212 KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms<GeometryType::CylindricalLimit>(){
213  return 0;
214 }
215 
216 template<>
217 KOKKOS_INLINE_FUNCTION constexpr bool use_toroidal_terms<GeometryType::Toroidal>(){
218  return 1;
219 }
220 
221 enum class PullbackMethod{
223  IdealMHD
224 };
225 
226 /***/
227 
229  PIR = 0,
230  PIZ,
231  PIP,
235  PTL_NPHASE
236 };
237 
239  PIM = 0,
242 #ifdef PTL_G2
243  PIG2,
244 #endif
245  PTL_NCONST
246 };
247 
248 // Divide two integers, then round up
249 KOKKOS_INLINE_FUNCTION int divide_and_round_up(int a, int b){
250  return (a+b-1)/b;
251 }
252 
253 KOKKOS_INLINE_FUNCTION long long int divide_and_round_up(long long int a, long long int b){
254  return (a+b-1)/b;
255 }
256 
257 // Positive modulo. i.e. positive_modulo(-1,3) will return 2, whereas (-1)%3 returns -1.
258 KOKKOS_INLINE_FUNCTION unsigned positive_modulo( int value, unsigned m) {
259  int mod = value % (int)m;
260  if (mod < 0) {
261  mod += m;
262  }
263  return mod;
264 }
265 
266 /* Returns the offset of an evenly distributed set of objects into even subsets. If the set can be distributed evenly,
267  * then this operation is simply the local subset times the result of the division. If not, then the first k
268  * subsets will have one extra object, where k is the remainder.
269  *
270  * n_obj is the number of objects that need to be divided
271  * n_subsets is the number of ranks the objects are being divided into
272  * i_subset is the index of this particular subset
273  *
274  */
275 inline long long int offsets_of_even_distribution(long long int n_obj, long long int n_subsets, long long int i_subset){
276  long long int obj_per_subset = n_obj/n_subsets;
277  long long int remainder = n_obj%n_subsets;
278  if (i_subset<remainder){
279  obj_per_subset += 1;
280  return i_subset*obj_per_subset;
281  }else{
282  return i_subset*obj_per_subset + remainder;
283  }
284 }
285 
286 /* Returns the count of an evenly distributed set of objects into even subsets. If the set can be distributed evenly,
287  * then this operation is simply the result of the division. If not, then the first k
288  * subsets will have one extra object, where k is the remainder.
289  *
290  * n_obj is the number of objects that need to be divided
291  * n_subsets is the number of ranks the objects are being divided into
292  * i_subset is the index of this particular subset
293  *
294  */
295 inline long long int counts_of_even_distribution(long long int n_obj, long long int n_subsets, long long int i_subset){
296  long long int obj_per_subset = n_obj/n_subsets;
297  long long int remainder = n_obj%n_subsets;
298  if (i_subset<remainder){
299  obj_per_subset += 1;
300  }
301  return obj_per_subset;
302 }
303 
304 // Converts integer to string and adds leading zeros
305 inline std::string formatted_int2str(int input, int n_digits){
306  std::string string_no_leading = std::to_string(input); // no leading zeros
307 
308  int initial_length = string_no_leading.length();
309  int zeros_to_add = n_digits - std::min(n_digits, initial_length);
310 
311  // Add leading zeros
312  return std::string(zeros_to_add, '0') + string_no_leading;
313 }
314 
315 // is_same_type is equivalent to std::is_same, but can be used in cuda kernels
316 template<class T, class U> struct is_same_type{static constexpr bool val=false;};
317 template<class T > struct is_same_type<T, T>{static constexpr bool val=true;};
318 
319 // Defined and initialized in my_mpi.cpp, reset in sml.tpp
320 extern bool global_debug_flag;
321 extern bool global_perf_barriers_flag;
322 
323 #endif
void exit_XGC(std::string msg)
Definition: globals.hpp:38
PullbackMethod
Definition: globals.hpp:221
ParticlePhase
Definition: globals.hpp:228
@ PIW1
1st weight
Definition: globals.hpp:233
@ PIW2
2nd weight
Definition: globals.hpp:234
@ PIP
phi coordinate
Definition: globals.hpp:231
@ PIRHO
gyroradius
Definition: globals.hpp:232
@ PIR
r coordinate
Definition: globals.hpp:229
@ PTL_NPHASE
Definition: globals.hpp:235
@ PIZ
z coordinate
Definition: globals.hpp:230
constexpr MagneticFieldMode MFM_GLOBAL
Definition: globals.hpp:157
GeometryType
Definition: globals.hpp:172
FAnalyticShape
Definition: globals.hpp:139
int get_num_cpu_threads()
Definition: globals.hpp:18
bool causes_multiplication_overflow(int a, int b)
Definition: globals.hpp:61
bool is_rank_zero()
Definition: globals.hpp:28
SpeciesType
Definition: globals.hpp:106
@ ELECTRON
Definition: globals.hpp:107
@ MAIN_ION
Definition: globals.hpp:108
long long int offsets_of_even_distribution(long long int n_obj, long long int n_subsets, long long int i_subset)
Definition: globals.hpp:275
MagneticFieldMode
Definition: globals.hpp:150
KOKKOS_INLINE_FUNCTION void assert_XGC(bool cond, const char *msg)
Definition: globals.hpp:52
BFieldSymmetry
Definition: globals.hpp:160
KOKKOS_INLINE_FUNCTION int divide_and_round_up(int a, int b)
Definition: globals.hpp:249
constexpr KOKKOS_INLINE_FUNCTION bool use_toroidal_terms()
KOKKOS_INLINE_FUNCTION unsigned positive_modulo(int value, unsigned m)
Definition: globals.hpp:258
WeightEvoEq
Definition: globals.hpp:144
bool causes_addition_overflow(int a, int b)
Definition: globals.hpp:91
long long int counts_of_even_distribution(long long int n_obj, long long int n_subsets, long long int i_subset)
Definition: globals.hpp:295
constexpr BFieldSymmetry BFS_GLOBAL
Definition: globals.hpp:167
constexpr PhiInterpType PIT_GLOBAL
Definition: globals.hpp:126
bool global_perf_barriers_flag
Definition: checkpoint.cpp:12
constexpr GeometryType GEOMETRY
Definition: globals.hpp:179
bool global_debug_flag
Definition: checkpoint.cpp:11
KinType
Definition: globals.hpp:111
@ GyroKin
Definition: globals.hpp:113
@ DriftKin
Definition: globals.hpp:112
ParticleConsts
Definition: globals.hpp:238
@ PTL_NCONST
Definition: globals.hpp:245
@ PIF0
F0.
Definition: globals.hpp:241
@ PIW0
W0.
Definition: globals.hpp:240
@ PIM
Magnetic moment mu.
Definition: globals.hpp:239
MarkerType
Definition: globals.hpp:133
PhiInterpType
Definition: globals.hpp:118
KOKKOS_INLINE_FUNCTION double geometry_switch(double a, double b)
std::string formatted_int2str(int input, int n_digits)
Definition: globals.hpp:305
Order
Definition: globals.hpp:100
int SML_COMM_RANK
Definition: my_mpi.cpp:5
MPI_Comm SML_COMM_WORLD
Definition: my_mpi.cpp:4
#define DEVICE_PRINTF(...)
Definition: space_settings.hpp:86
Definition: globals.hpp:316
static constexpr bool val
Definition: globals.hpp:316