File: lal_preprocessor.h

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (338 lines) | stat: -rw-r--r-- 13,099 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// **************************************************************************
//                               preprocessor.h
//                             -------------------
//                           W. Michael Brown (ORNL)
//                           Nitin Dhamankar (Intel)
//
//  Device-side preprocessor definitions
//
// __________________________________________________________________________
//    This file is part of the LAMMPS Accelerator Library (LAMMPS_AL)
// __________________________________________________________________________
//
//    begin                :
//    email                : brownw@ornl.gov
// ***************************************************************************/

//*************************************************************************
//                       Device Configuration Definitions
//
//  For OpenCL, the configuration is a string (optionally controlled at
//  runtime) where tokens specify the values below in order)
//
//  CONFIG_ID:
//     Definition:   Unique ID for a configuration
//                   100-199 for NVIDIA GPUs with CUDA / HIP
//                   200-299 for NVIDIA GPUs with OpenCL
//                   300-399 for AMD GPUs with HIP
//                   400-499 for AMD GPUs with OpenCL
//                   500-599 for Intel GPUs with OpenCL
//  SIMD_SIZE:
//     Definition:   For CUDA this is the warp size.
//                   For AMD this is the wavefront size.
//                   For OpenCL < 2.1 this is the number of workitems
//                     guarenteed to have the same instruction pointer
//                   For OpenCL >= 2.1 this is the smallest expected subgroup
//                     size. Actually subgroup sizes are determined per kernel.
//  MEM_THREADS
//     Definition:   Number of elements in main memory transaction. Used in
//                   PPPM. If unknown, set to SIMD_SIZE.
//  SHUFFLE_AVAIL
//     Definition:   Controls the use of instructions for horizontal vector
//                   operations. 0 disables and will increase shared memory
//                   usage. 1 enables for CUDA, HIP, and OpenCL >= 2.1 on
//                   NVIDIA and Intel devices.
//  FAST_MATH
//     Definition:   0: do not use -cl-fast-relaxed-math optimization flag or
//                   native transcendentals for OpenCL (fused multiply-add
//                   still enabled). For CUDA and HIP, this is controlled by
//                   the Makefile at compile time. 1: enable fast math opts
//
//  THREADS_PER_ATOM
//     Definition:   Default number of work items or CUDA threads assigned per
//                   per atom for pair styles
//     Restrictions: Must be power of 2; THREADS_PER_ATOM<=SIMD_SIZE
//  THREADS_PER_CHARGE
//     Definition:   Default number of work items or CUDA threads assigned per
//                   per atom for pair styles using charge
//     Restrictions: Must be power of 2; THREADS_PER_ATOM<=SIMD_SIZE
//  THREADS_PER_THREE
//     Definition:   Default number of work items or CUDA threads assigned per
//                   per atom for 3-body styles
//     Restrictions: Must be power of 2; THREADS_PER_ATOM^2<=SIMD_SIZE
//
//  BLOCK_PAIR
//     Definition:   Default block size for pair styles
//     Restrictions: Must be integer multiple of SIMD_SIZE
//  BLOCK_BIO_PAIR
//     Definition:   Default block size for CHARMM styles
//     Restrictions: Must be integer multiple of SIMD_SIZE
//  BLOCK_ELLIPSE
//     Definition:   Default block size for ellipsoidal models and some 3-body
//                   styles
//     Restrictions: Must be integer multiple of SIMD_SIZE
//  PPPM_BLOCK_1D
//     Definition:   Default block size for PPPM kernels
//     Restrictions: Must be integer multiple of SIMD_SIZE
//  BLOCK_NBOR_BUILD
//     Definition:   Default block size for neighbor list builds
//     Restrictions: Must be integer multiple of SIMD_SIZE
//  BLOCK_CELL_2D
//     Definition:   Default block size in each dimension for matrix transpose
//  BLOCK_CELL_ID
//     Definition:   Unused in current implementation; Maintained for legacy
//                   purposes and specialized builds
//
//  MAX_SHARED_TYPES 8
//     Definition:   Max # of atom type params can be stored in shared memory
//     Restrictions: MAX_SHARED_TYPES*MAX_SHARED_TYPES<=BLOCK_PAIR
//  MAX_BIO_SHARED_TYPES
//     Definition:   Max # of atom type params can be stored in shared memory
//     Restrictions: MAX_BIO_SHARED_TYPES<=BLOCK_BIO_PAIR*2
//  PPPM_MAX_SPLINE
//     Definition:   Maximum order for splines in PPPM
//     Restrictions: PPPM_BLOCK_1D>=PPPM_MAX_SPLINE*PPPM_MAX_SPLINE
//
//*************************************************************************/

// -------------------------------------------------------------------------
//                           CUDA and HIP DEFINITIONS
// -------------------------------------------------------------------------

#if defined(NV_KERNEL) || defined(USE_HIP)
#include "lal_pre_cuda_hip.h"
#endif

// -------------------------------------------------------------------------
//                         OPENCL DEVICE CONFIGURATAIONS
// -------------------------------------------------------------------------

// See lal_pre_ocl_config.h for OpenCL device configurations

#if !defined(NV_KERNEL) && !defined(USE_HIP)

#define USE_OPENCL

// -------------------------------------------------------------------------
//                         OPENCL KERNEL MACROS
// -------------------------------------------------------------------------

#if (__OPENCL_VERSION__ > 199)
#define NOUNROLL __attribute__((opencl_unroll_hint(1)))
#else
#define NOUNROLL
#endif

#define GLOBAL_ID_X get_global_id(0)
#define THREAD_ID_X get_local_id(0)
#define BLOCK_ID_X get_group_id(0)
#define BLOCK_SIZE_X get_local_size(0)
#define GLOBAL_SIZE_X get_global_size(0)
#define THREAD_ID_Y get_local_id(1)
#define BLOCK_ID_Y get_group_id(1)
#define NUM_BLOCKS_X get_num_groups(0)
#define __syncthreads() barrier(CLK_LOCAL_MEM_FENCE)
#define ucl_inline inline

// -------------------------------------------------------------------------
//                      OPENCL KERNEL MACROS - TEXTURES
// -------------------------------------------------------------------------

#define fetch4(ans,i,x) ans=x[i]
#define fetch(ans,i,q) ans=q[i]

// -------------------------------------------------------------------------
//                       OPENCL KERNEL MACROS - MATH
// -------------------------------------------------------------------------

#ifndef _SINGLE_SINGLE

#ifndef cl_khr_fp64
#ifndef cl_amd_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
#endif
#endif
#if defined(cl_khr_fp64)
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
#elif defined(cl_amd_fp64)
#pragma OPENCL EXTENSION cl_amd_fp64 : enable
#endif

#endif

#define fast_mul(X,Y) (X)*(Y)

#define ucl_atan atan
#define ucl_cbrt cbrt
#define ucl_ceil ceil
#define ucl_abs fabs

#if defined(FAST_MATH) && !defined(_DOUBLE_DOUBLE)

#define ucl_exp native_exp
#define ucl_powr native_powr
#define ucl_rsqrt native_rsqrt
#define ucl_sqrt native_sqrt
#define ucl_recip native_recip

#else

#define ucl_exp exp
#define ucl_powr powr
#define ucl_rsqrt rsqrt
#define ucl_sqrt sqrt
#define ucl_recip(x) ((numtyp)1.0/(x))

#endif

// -------------------------------------------------------------------------
//                      OPENCL KERNEL MACROS - SHUFFLE
// -------------------------------------------------------------------------

#if (SHUFFLE_AVAIL == 1)
  #ifdef cl_intel_subgroups
    #pragma OPENCL EXTENSION cl_intel_subgroups : enable
    #define shfl_down(var, delta, width) \
      intel_sub_group_shuffle_down(var, var, delta)
    #define shfl_xor(var, lanemask, width) \
      intel_sub_group_shuffle_xor(var, lanemask)
    #define simd_broadcast_i(var, src, width) sub_group_broadcast(var, src)
    #define simd_broadcast_f(var, src, width) sub_group_broadcast(var, src)
    #define simd_broadcast_d(var, src, width) sub_group_broadcast(var, src)
  #else
    #ifdef _SINGLE_SINGLE
      inline float shfl_down(float var, unsigned int delta, int width) {
        float ret;
        int c;
        c = ((SIMD_SIZE-width) << 8) | 0x1f;
        asm volatile ("shfl.sync.down.b32 %0, %1, %2, %3, %4;" : "=f"(ret) : "f"(var), "r"(delta), "r"(c), "r"(0xffffffff));
        return ret;
      }
      inline float shfl_xor(float var, unsigned int lanemask, int width) {
        float ret;
        int c;
        c = ((SIMD_SIZE-width) << 8) | 0x1f;
        asm volatile ("shfl.sync.bfly.b32 %0, %1, %2, %3, %4;" : "=f"(ret) : "f"(var), "r"(lanemask), "r"(c), "r"(0xffffffff));
        return ret;
      }
    #else
      inline double shfl_down(double var, unsigned int delta, int width) {
        int c = ((SIMD_SIZE-width) << 8) | 0x1f;
        int x,y,x2,y2;
        double ans;
        asm volatile ("mov.b64 {%0, %1}, %2;" : "=r"(y), "=r"(x) : "d"(var));
        asm volatile ("shfl.sync.down.b32 %0, %1, %2, %3, %4;" : "=r"(x2) : "r"(x), "r"(delta), "r"(c), "r"(0xffffffff));
        asm volatile ("shfl.sync.down.b32 %0, %1, %2, %3, %4;" : "=r"(y2) : "r"(y), "r"(delta), "r"(c), "r"(0xffffffff));
        asm volatile ("mov.b64 %0, {%1, %2};" : "=d"(ans) : "r"(y2), "r"(x2));
        return ans;
      }
      inline double shfl_xor(double var, unsigned int lanemask, int width) {
        int c = ((SIMD_SIZE-width) << 8) | 0x1f;
        int x,y,x2,y2;
        double ans;
        asm volatile ("mov.b64 {%0, %1}, %2;" : "=r"(y), "=r"(x) : "d"(var));
        asm volatile ("shfl.sync.bfly.b32 %0, %1, %2, %3, %4;" : "=r"(x2) : "r"(x), "r"(lanemask), "r"(c), "r"(0xffffffff));
        asm volatile ("shfl.sync.bfly.b32 %0, %1, %2, %3, %4;" : "=r"(y2) : "r"(y), "r"(lanemask), "r"(c), "r"(0xffffffff));
        asm volatile ("mov.b64 %0, {%1, %2};" : "=d"(ans) : "r"(y2), "r"(x2));
        return ans;
      }
    #endif
    inline int simd_broadcast_i(int var, unsigned int src, int width) {
      int ret;
      int c;
      c = ((SIMD_SIZE-width) << 8) | 0x1f;
      asm volatile ("shfl.sync.idx.b32 %0, %1, %2, %3, %4;" : "=f"(ret) : "f"(var), "r"(src), "r"(c), "r"(0xffffffff));
      return ret;
    }
    inline float simd_broadcast_f(float var, unsigned int src, int width) {
      float ret;
      int c;
      c = ((SIMD_SIZE-width) << 8) | 0x1f;
      asm volatile ("shfl.sync.idx.b32 %0, %1, %2, %3, %4;" : "=f"(ret) : "f"(var), "r"(src), "r"(c), "r"(0xffffffff));
      return ret;
    }
    #ifdef _DOUBLE_DOUBLE
      inline double simd_broadcast_d(double var, unsigned int src, int width) {
        int c = ((SIMD_SIZE-width) << 8) | 0x1f;
        int x,y,x2,y2;
        double ans;
        asm volatile ("mov.b64 {%0, %1}, %2;" : "=r"(y), "=r"(x) : "d"(var));
        asm volatile ("shfl.sync.idx.b32 %0, %1, %2, %3, %4;" : "=r"(x2) : "r"(x), "r"(src), "r"(c), "r"(0xffffffff));
        asm volatile ("shfl.sync.idx.b32 %0, %1, %2, %3, %4;" : "=r"(y2) : "r"(y), "r"(src), "r"(c), "r"(0xffffffff));
        asm volatile ("mov.b64 %0, {%1, %2};" : "=d"(ans) : "r"(y2), "r"(x2));
        return ans;
      }
    #endif
  #endif
#endif

// -------------------------------------------------------------------------
//                      OPENCL KERNEL MACROS - SUBGROUPS
// -------------------------------------------------------------------------

#ifdef USE_OPENCL_SUBGROUPS
  #ifndef cl_intel_subgroups
    #pragma OPENCL EXTENSION cl_khr_subgroups : enable
  #endif
  #define simdsync() sub_group_barrier(CLK_LOCAL_MEM_FENCE)
  #define simd_size() get_max_sub_group_size()
#else
  #define simdsync()
  #define simd_size() SIMD_SIZE
#endif

// -------------------------------------------------------------------------
//                            END OPENCL DEFINITIONS
// -------------------------------------------------------------------------

#endif

// -------------------------------------------------------------------------
//                  ARCHITECTURE INDEPENDENT DEFINITIONS
// -------------------------------------------------------------------------

#ifdef _DOUBLE_DOUBLE
#define numtyp double
#define numtyp2 double2
#define numtyp4 double4
#define acctyp double
#define acctyp2 double2
#define acctyp4 double4
#endif

#ifdef _SINGLE_DOUBLE
#define numtyp float
#define numtyp2 float2
#define numtyp4 float4
#define acctyp double
#define acctyp2 double2
#define acctyp4 double4
#endif

#ifndef numtyp
#define numtyp float
#define numtyp2 float2
#define numtyp4 float4
#define acctyp float
#define acctyp2 float2
#define acctyp4 float4
#endif

#define EWALD_F (numtyp)1.12837917
#define EWALD_P (numtyp)0.3275911
#define A1 (numtyp)0.254829592
#define A2 (numtyp)-0.284496736
#define A3 (numtyp)1.421413741
#define A4 (numtyp)-1.453152027
#define A5 (numtyp)1.061405429

#define SBBITS 30
#define NEIGHMASK 0x3FFFFFFF
ucl_inline int sbmask(int j) { return j >> SBBITS & 3; };

// default to 32-bit smallint and other ints, 64-bit bigint:
// same as defined in src/lmptype.h
#if !defined(LAMMPS_SMALLSMALL) && !defined(LAMMPS_BIGBIG) && \
    !defined(LAMMPS_SMALLBIG)
#define LAMMPS_SMALLBIG
#endif