HILA
Loading...
Searching...
No Matches
globals.h
Go to the documentation of this file.
1#ifndef HILA_GLOBAL_H_
2#define HILA_GLOBAL_H_
3/**
4 * @file globals.h
5 * @brief Definition of global variable class
6 */
7
8namespace hila {
9/**
10 * @brief Global variable class within hila namespace
11 *
12 * @details Special wrapper for "simple" types (elementary types, simple structs etc.).
13 * Implemented on gpus automatically using `__constant__` memory. All global variables
14 * exist for the whole program lifetime.
15 *
16 * ## Variable declaration:
17 *
18 * Declaration is possible on top (file) level only, not inside functions!
19 * \code{.cpp}
20 * hila::global<type> globalvar;
21 * \endcode
22 * For example
23 * \code{.cpp}
24 * hila::global<Vector<4,Complex<double>> cvec1, cvec2;
25 * \endcode
26 *
27 * ## Variable assingment:
28 * \code{.cpp}
29 * globalvar = <value>;
30 * \endcode
31 *
32 * Assignment is not possible inside loops
33 * Only "full" assignment is possible, not e.g. by field records
34 *
35 * ## Variable access
36 *
37 * The value of the variable is obtained with function-like "()" call
38 * \code{.cpp}
39 * globalvar();
40 * \endcode
41 *
42 * Variable can be used everywhere in the source file.
43 *
44 * \code{.cpp}
45 * // at global file level
46 * struct param_t { double a,b[2]; };
47 *
48 * hila::global<param_t> params;
49 *
50 * // define some function
51 * double myfunc(double dv) {
52 * return cos( dv / params().a );
53 * }
54 *
55 * ...
56 * // inside main() or some other function - use helper struct to assign values
57 * param_t tmp;
58 * tmp.a = 3*M_PI;
59 * tmp.b[0] = ....
60 * tmp.b[1] = ...
61 * params = tmp; // do the full struct assignment to global
62 *
63 * ...
64 * Field<double> df;
65 * onsites(ALL) {
66 * df[X] = myfunc(X.x()) + params().b[0]/params().b[1];
67 * }
68 * hila::out0 << "Parameter a is " << params().a << '\n';
69 * \endcode
70 *
71 * ## Additional information
72 *
73 * "extern" and "static" can be used, if there are several source files, with the usual meaning:
74 *
75 * \code{.cpp}
76 * extern hila::global<double> a; // variable a (of identical type) is defined
77 * // in some other file
78 * static hila::global<mytype> par; // par is invisible to other files
79 * \endcode
80 *
81 * Declarations can be enclosed in namespaces:
82 * \code{.cpp}
83 * namespace myglobals {
84 * hila::global<double> a, b;
85 * }
86 * ...
87 *
88 * myglobals::a = 3;
89 * hila::out0 << "a has value " << myglobals::a() << '\n';
90 * \endcode
91 *
92 * Variables cannot be initialized in declaration, because (possible) GPUs are not initialized.
93 */
94template <typename T, typename custom = void>
95class global {
96
97 static_assert(std::is_trivial<T>::value && std::is_standard_layout<T>::value,
98 "hila::global<> expects only pod-type elements (plain old data): default "
99 "constructor, copy and delete");
100
101 private:
102 T val;
103
104 // empty stub, will be specialized by hilapp (if needed)
105 void copy_to_device() const {}
106
107 public:
108 // Get the value of the variable with operator()
109 const T &operator()() const {
110 return val;
111 }
112
113 // assignment is possible from compatible rhs values
114 template <typename S, std::enable_if_t<hila::is_assignable<T &, S>::value, int> = 0>
115 void operator=(const S &rhs) {
116 assert(hila::is_initialized && "Assign to global possible only after hila::initialize()");
117
118 val = rhs;
119
120 copy_to_device();
121 }
122};
123} // namespace hila
124
125#endif
Global variable class within hila namespace.
Definition globals.h:95
Implement hila::swap for gauge fields.
Definition array.h:981