HILA
Loading...
Searching...
No Matches
backend_gpu/lattice.h
1#ifndef HILA_BACKEND_LATTICE_GPU_H_
2#define HILA_BACKEND_LATTICE_GPU_H_
3
4#include "coordinates.h"
5#include "globals.h"
6
7/// Define some global variables
8
9#ifdef IN_HILA_GPU
10#define HILA_GPU_EXTERN /* nothing */
11#else
12#define HILA_GPU_EXTERN extern
13#endif
14
15
16/// Lattice related data that needs to be communicated
17/// to kernels
19 /// Storage for the neighbour indexes. Stored on device
20 unsigned *d_neighb[NDIRS];
21
22#ifdef SPECIAL_BOUNDARY_CONDITIONS
23 /// Neighbour indexes with special boundaries. Stored on device
24 unsigned *d_neighb_special[NDIRS];
25#endif
26
27 /// The full number of elements in a field, including haloes.
28 /// This is necessary for structure-of-arrays -storage
30 /// beginning and end of this loop (using lattice to communicate,
31 /// which may not be the clearest choice.)
32 int loop_begin, loop_end;
33
34#ifdef EVEN_SITES_FIRST
35 /// Finally a pointer to the list of coordinates, stored on device
36 CoordinateVector *d_coordinates;
37
38 // /// get the coordinates at a given site
39 // __host__ __device__ const CoordinateVector &coordinates(unsigned idx) const {
40 // return _dev_coordinates[idx];
41 // }
42 // __host__ __device__ int coordinate(unsigned idx, Direction dir) const {
43 // return _dev_coordinates[idx][dir];
44 // }
45
46#else
47 // Now not EVEN_SITES_FIRST
48
49 // these defined in hila_gpu.cpp
50 __device__ const CoordinateVector coordinates(unsigned idx) const;
51 __device__ int coordinate(unsigned idx, Direction dir) const;
52
53#endif
54
55 /// setup the backend lattice data
56 void setup(lattice_struct &lattice);
57
58 void set_device_globals(const lattice_struct &lattice);
59};
60
61////////////////////////////////////////////////////////////////////////////
62// Define here some globals and inline functions
63
64HILA_GPU_EXTERN __constant__ unsigned _dev_field_alloc_size;
65#ifdef EVEN_SITES_FIRST
66HILA_GPU_EXTERN __constant__ CoordinateVector *_dev_coordinates;
67#endif
68
69template <typename T>
70inline __device__ const CoordinateVector &get_dev_coordinates(T idx) {
71 return _dev_coordinates[idx];
72}
73
74template <typename T, typename D>
75inline __device__ int get_dev_coordinate(T idx, D dir) {
76 return _dev_coordinates[idx][dir];
77}
78
79
80// make this template to avoid code generation if not needed
81#ifdef EVEN_SITES_FIRST
82template <typename T>
83inline __device__ Parity get_dev_site_parity(T idx) {
84 auto cv = get_dev_coordinates(idx);
85 int s = 0;
86 foralldir(d) s += cv.e(d);
87 if (s % 2 == 0)
88 return Parity::even;
89 else
90 return Parity::odd;
91}
92#endif
93
94// Save "constants" lattice size and volume here
95HILA_GPU_EXTERN hila::global<int64_t> _d_volume;
96HILA_GPU_EXTERN hila::global<CoordinateVector> _d_size;
97
98#ifndef EVEN_SITES_FIRST
99HILA_GPU_EXTERN hila::global<CoordinateVector> _d_nodesize;
100HILA_GPU_EXTERN hila::global<CoordinateVector> _d_nodemin;
101HILA_GPU_EXTERN hila::global<CoordinateVector> _d_nodefactor;
102#endif
103
104
105// Then, define global functions loop_lattice_size() and _volume()
106#pragma hila loop_function
107inline int loop_lattice_size(Direction dir) {
108 return _d_size()[dir];
109}
110
111#pragma hila loop_function
112inline CoordinateVector loop_lattice_size(void) {
113 return _d_size();
114}
115
116#pragma hila loop_function
117inline int64_t loop_lattice_volume(void) {
118 return _d_volume();
119}
120
121#ifndef EVEN_SITES_FIRST
122
123#pragma hila loop_function
124inline const CoordinateVector backend_lattice_struct::coordinates(unsigned idx) const {
126 unsigned vdiv, ndiv;
127
128 vdiv = idx;
129 for (int d = 0; d < NDIM - 1; ++d) {
130 ndiv = vdiv / _d_nodesize()[d];
131 c[d] = vdiv - ndiv * _d_nodesize()[d] + _d_nodemin()[d];
132 vdiv = ndiv;
133 }
134 c[NDIM - 1] = vdiv + _d_nodemin()[NDIM - 1];
135
136 return c;
137}
138
139#pragma hila loop_function
140inline int backend_lattice_struct::coordinate(unsigned idx, Direction dir) const {
141 return (idx / _d_nodefactor()[dir]) % _d_nodesize()[dir] + _d_nodemin()[dir];
142}
143
144#endif // not EVEN_SITES_FIRST
145
146
147#endif
Global variable class within hila namespace.
Definition globals.h:97
This header file defines:
Parity
Parity enum with values EVEN, ODD, ALL; refers to parity of the site. Parity of site (x,...
#define foralldir(d)
Macro to loop over (all) Direction(s)
Definition coordinates.h:80
constexpr unsigned NDIRS
Number of directions.
Definition coordinates.h:57
Direction
Enumerator for direction that assigns integer to direction to be interpreted as unit vector.
Definition coordinates.h:34
Definition of global variable class.
Helper class for loading the vectorized lattice.
void setup(lattice_struct *lattice)
unsigned * d_neighb[NDIRS]
Storage for the neighbour indexes. Stored on device.