Line data Source code
1 : /*----------------------------------------------------------------------------*/
2 : /* CP2K: A general program to perform molecular dynamics simulations */
3 : /* Copyright 2000-2026 CP2K developers group <https://cp2k.org> */
4 : /* */
5 : /* SPDX-License-Identifier: BSD-3-Clause */
6 : /*----------------------------------------------------------------------------*/
7 : #ifndef GRID_COMMON_H
8 : #define GRID_COMMON_H
9 :
10 : #define GRID_STRINGIFY(SYMBOL) #SYMBOL
11 :
12 : // GCC added the simd pragma with version 6. Clang defines __GNUC__ as 4 for
13 : // compatibility, so it has to be excluded explicitly here and below.
14 : #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && \
15 : !defined(__INTEL_LLVM_COMPILER) && __GNUC__ < 6
16 : #define GRID_PRAGMA_SIMD(OBJS, N)
17 : #define GRID_PRAGMA_SIMD_LOOP
18 : // Intel added the simd pragma with version 19.00.
19 : #elif defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1900
20 : #define GRID_PRAGMA_SIMD(OBJS, N)
21 : #define GRID_PRAGMA_SIMD_LOOP
22 : // All compilers support the same syntax defined by the OpenMP standard.
23 : #else
24 : #define GRID_PRAGMA_SIMD(OBJS, N) \
25 : _Pragma(GRID_STRINGIFY(omp simd linear OBJS simdlen(N)))
26 : #define GRID_PRAGMA_SIMD_LOOP _Pragma(GRID_STRINGIFY(omp simd))
27 : #endif
28 :
29 : #define GRID_OMP_MIN_ITERATIONS 1024
30 :
31 : // GCC added the unroll pragma with version 8 and...
32 : #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && \
33 : !defined(__INTEL_LLVM_COMPILER) && __GNUC__ < 8
34 : #define GRID_PRAGMA_UNROLL(N)
35 : #define GRID_PRAGMA_UNROLL_UP_TO(N)
36 : // ...chose a custom syntax, which Clang also accepts.
37 : #elif defined(__GNUC__) && !defined(__INTEL_COMPILER) && \
38 : !defined(__INTEL_LLVM_COMPILER) && (__GNUC__ >= 8 || defined(__clang__))
39 : #define GRID_PRAGMA_UNROLL(N) _Pragma(GRID_STRINGIFY(GCC unroll N))
40 : #define GRID_PRAGMA_UNROLL_UP_TO(N) _Pragma(GRID_STRINGIFY(GCC unroll N))
41 : // Most other compilers support a common syntax.
42 : #else
43 : #define GRID_PRAGMA_UNROLL(N) _Pragma(GRID_STRINGIFY(unroll(N)))
44 : #define GRID_PRAGMA_UNROLL_UP_TO(N) _Pragma("unroll")
45 : #endif
46 :
47 : #if defined(__CUDACC__) || defined(__HIPCC__)
48 : #define GRID_HOST_DEVICE __host__ __device__
49 : #else
50 : #define GRID_HOST_DEVICE
51 : #endif
52 :
53 : /*******************************************************************************
54 : * \brief Factorial function, e.g. fac(5) = 5! = 120.
55 : * \author Ole Schuett
56 : ******************************************************************************/
57 552673779 : GRID_HOST_DEVICE static inline double fac(const int i) {
58 552673779 : static const double table[] = {
59 : 0.10000000000000000000E+01, 0.10000000000000000000E+01,
60 : 0.20000000000000000000E+01, 0.60000000000000000000E+01,
61 : 0.24000000000000000000E+02, 0.12000000000000000000E+03,
62 : 0.72000000000000000000E+03, 0.50400000000000000000E+04,
63 : 0.40320000000000000000E+05, 0.36288000000000000000E+06,
64 : 0.36288000000000000000E+07, 0.39916800000000000000E+08,
65 : 0.47900160000000000000E+09, 0.62270208000000000000E+10,
66 : 0.87178291200000000000E+11, 0.13076743680000000000E+13,
67 : 0.20922789888000000000E+14, 0.35568742809600000000E+15,
68 : 0.64023737057280000000E+16, 0.12164510040883200000E+18,
69 : 0.24329020081766400000E+19, 0.51090942171709440000E+20,
70 : 0.11240007277776076800E+22, 0.25852016738884976640E+23,
71 : 0.62044840173323943936E+24, 0.15511210043330985984E+26,
72 : 0.40329146112660563558E+27, 0.10888869450418352161E+29,
73 : 0.30488834461171386050E+30, 0.88417619937397019545E+31,
74 : 0.26525285981219105864E+33};
75 552673779 : return table[i];
76 : }
77 :
78 : /*******************************************************************************
79 : * \brief Number of Cartesian orbitals up to given angular momentum quantum.
80 : * \author Ole Schuett
81 : ******************************************************************************/
82 24084004299 : GRID_HOST_DEVICE static inline int ncoset(const int l) {
83 24084004299 : static const int table[] = {0, // l=-1, usefull for computing loop bounds
84 : 1, // l=0
85 : 4, // l=1
86 : 10, // l=2 ...
87 : 20, 35, 56, 84, 120, 165, 220, 286,
88 : 364, 455, 560, 680, 816, 969, 1140, 1330};
89 6411376903 : return table[l + 1];
90 : }
91 :
92 : /*******************************************************************************
93 : * \brief Maps three angular momentum components to a single zero based index.
94 : * \author Ole Schuett
95 : ******************************************************************************/
96 29667022499 : GRID_HOST_DEVICE static inline int coset(int lx, int ly, int lz) {
97 29667022499 : const int l = lx + ly + lz;
98 29667022499 : if (l == 0) {
99 : return 0;
100 : } else {
101 23610968456 : return ncoset(l - 1) + ((l - lx) * (l - lx + 1)) / 2 + lz;
102 : }
103 : }
104 :
105 : /*******************************************************************************
106 : * \brief Returns the smaller of two given integer (missing from the C standard)
107 : * \author Ole Schuett
108 : ******************************************************************************/
109 45209602067 : GRID_HOST_DEVICE static inline int imin(int x, int y) {
110 45209602067 : return (x < y ? x : y);
111 : }
112 :
113 : /*******************************************************************************
114 : * \brief Returns the larger of two given integer (missing from the C standard)
115 : * \author Ole Schuett
116 : ******************************************************************************/
117 6568113358 : GRID_HOST_DEVICE static inline int imax(int x, int y) {
118 4715875355 : return (x > y ? x : y);
119 : }
120 :
121 : /*******************************************************************************
122 : * \brief Equivalent of Fortran's MODULO, which always return a positive number.
123 : * https://gcc.gnu.org/onlinedocs/gfortran/MODULO.html
124 : * \author Ole Schuett
125 : ******************************************************************************/
126 13121179270 : GRID_HOST_DEVICE static inline int modulo(int a, int m) {
127 13121179270 : return ((a % m + m) % m);
128 : }
129 :
130 : /*******************************************************************************
131 : * \brief Orbital angular momentum.
132 : * \author Ole Schuett
133 : ******************************************************************************/
134 : typedef struct {
135 : int l[3];
136 : } orbital;
137 :
138 : /*******************************************************************************
139 : * \brief Increase i'th component of given orbital angular momentum.
140 : * \author Ole Schuett
141 : ******************************************************************************/
142 2013866904 : GRID_HOST_DEVICE static inline orbital up(const int i, const orbital a) {
143 2013866904 : orbital b = a;
144 2013866904 : b.l[i] += 1;
145 216883041 : return b;
146 : }
147 :
148 : /*******************************************************************************
149 : * \brief Decrease i'th component of given orbital angular momentum.
150 : * \author Ole Schuett
151 : ******************************************************************************/
152 2009109252 : GRID_HOST_DEVICE static inline orbital down(const int i, const orbital a) {
153 2009109252 : orbital b = a;
154 2009109252 : b.l[i] = imax(0, a.l[i] - 1);
155 217659285 : return b;
156 : }
157 :
158 : /*******************************************************************************
159 : * \brief Return coset index of given orbital angular momentum.
160 : * \author Ole Schuett
161 : ******************************************************************************/
162 11656863305 : GRID_HOST_DEVICE static inline int idx(const orbital a) {
163 3208408401 : return coset(a.l[0], a.l[1], a.l[2]);
164 : }
165 :
166 : #endif // GRID_COMMON_H
167 :
168 : // EOF
|