LCOV - code coverage report
Current view: top level - src/grid/common - grid_common.h (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:cccd2f3) Lines: 100.0 % 26 26
Test Date: 2026-05-06 07:07:47 Functions: 100.0 % 1 1

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

Generated by: LCOV version 2.0-1