Grid 0.7.0
GenericHMCrunner.h
Go to the documentation of this file.
1/*************************************************************************************
2
3Grid physics library, www.github.com/paboyle/Grid
4
5Source file: ./lib/qcd/hmc/GenericHmcRunner.h
6
7Copyright (C) 2015
8
9Author: paboyle <paboyle@ph.ed.ac.uk>
10Author: Guido Cossu <guido.cossu@ed.ac.uk>
11
12This program is free software; you can redistribute it and/or modify
13it under the terms of the GNU General Public License as published by
14the Free Software Foundation; either version 2 of the License, or
15(at your option) any later version.
16
17This program is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU General Public License for more details.
21
22You should have received a copy of the GNU General Public License along
23with this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 See the full license in the file "LICENSE" in the top level distribution
27 directory
28*************************************************************************************/
29 /* END LEGAL */
30#ifndef GRID_GENERIC_HMC_RUNNER
31#define GRID_GENERIC_HMC_RUNNER
32
33#include <unordered_map>
34
36
37// very ugly here but possibly resolved if we had a base Reader class
38template < class ReaderClass >
40public:
41 virtual void Run() = 0;
42 virtual void initialize(ReaderClass& ) = 0;
43};
44
45template <class Implementation,
46 template <typename, typename, typename> class Integrator,
47 class RepresentationsPolicy = NoHirep, class ReaderClass = XmlReader>
48class HMCWrapperTemplate: public HMCRunnerBase<ReaderClass> {
49public:
50 INHERIT_FIELD_TYPES(Implementation);
51 typedef Implementation ImplPolicy; // visible from outside
52 template <typename S = NoSmearing<Implementation> >
54
56 std::string ParameterFile;
58
59 // The set of actions (keep here for lower level users, for now)
61
62 HMCWrapperTemplate() = default;
63
67
68 void initialize(ReaderClass & TheReader){
69 std::cout << "Initialization of the HMC" << std::endl;
70 Resources.initialize(TheReader);
71
72 // eventually add smearing
73
74 Resources.GetActionSet(TheAction);
75 }
76
77
78 void ReadCommandLine(int argc, char **argv) {
79 std::string arg;
80
81 if (GridCmdOptionExists(argv, argv + argc, "--StartingType")) {
82 arg = GridCmdOptionPayload(argv, argv + argc, "--StartingType");
83
84 if (arg != "HotStart" && arg != "ColdStart" && arg != "TepidStart" &&
85 arg != "CheckpointStart") {
86 std::cout << GridLogError << "Unrecognized option in --StartingType\n";
87 std::cout
89 << "Valid [HotStart, ColdStart, TepidStart, CheckpointStart]\n";
90 exit(1);
91 }
92 Parameters.StartingType = arg;
93 std::cout <<GridLogMessage << " GenericHMCrunner --StartingType "<<arg<<std::endl;
94 }
95
96 if (GridCmdOptionExists(argv, argv + argc, "--StartingTrajectory")) {
97 arg = GridCmdOptionPayload(argv, argv + argc, "--StartingTrajectory");
98 std::vector<int> ivec(0);
99 GridCmdOptionIntVector(arg, ivec);
100 Parameters.StartTrajectory = ivec[0];
101 std::cout <<GridLogMessage << " GenericHMCrunner --StartingTrajectory "<<ivec[0]<<std::endl;
102 }
103
104 if (GridCmdOptionExists(argv, argv + argc, "--Trajectories")) {
105 arg = GridCmdOptionPayload(argv, argv + argc, "--Trajectories");
106 std::vector<int> ivec(0);
107 GridCmdOptionIntVector(arg, ivec);
108 Parameters.Trajectories = ivec[0];
109 std::cout << GridLogMessage<<" GenericHMCrunner Command Line --Trajectories "<<ivec[0]<<std::endl;
110 }
111
112 if (GridCmdOptionExists(argv, argv + argc, "--Thermalizations")) {
113 arg = GridCmdOptionPayload(argv, argv + argc, "--Thermalizations");
114 std::vector<int> ivec(0);
115 GridCmdOptionIntVector(arg, ivec);
116 Parameters.NoMetropolisUntil = ivec[0];
117 std::cout << GridLogMessage<<" GenericHMCrunner --Thermalizations "<<ivec[0]<<std::endl;
118 }
119 if (GridCmdOptionExists(argv, argv + argc, "--ParameterFile")) {
120 arg = GridCmdOptionPayload(argv, argv + argc, "--ParameterFile");
121 ParameterFile = arg;
122 }
123 }
124
125
126 template <class SmearingPolicy>
127 void Run(SmearingPolicy &S) {
128 Runner(S);
129 }
130
131 void Run(){
133 Runner(S);
134 }
135
136 //Use the checkpointer to initialize the RNGs and the gauge field, writing the resulting gauge field into U.
137 //This is called automatically by Run but may be useful elsewhere, e.g. for integrator tuning experiments
139 if(!Resources.haveRNGs()) Resources.AddRNGs();
140
141 if (Parameters.StartingType == "HotStart") {
142 // Hot start
143 Resources.SeedFixedIntegers();
144 Implementation::HotConfiguration(Resources.GetParallelRNG(), U);
145 } else if (Parameters.StartingType == "ColdStart") {
146 // Cold start
147 Resources.SeedFixedIntegers();
148 Implementation::ColdConfiguration(Resources.GetParallelRNG(), U);
149 } else if (Parameters.StartingType == "TepidStart") {
150 // Tepid start
151 Resources.SeedFixedIntegers();
152 Implementation::TepidConfiguration(Resources.GetParallelRNG(), U);
153 } else if (Parameters.StartingType == "CheckpointStart") {
154 // CheckpointRestart
155 Resources.GetCheckPointer()->CheckpointRestore(Parameters.StartTrajectory, U,
156 Resources.GetSerialRNG(),
157 Resources.GetParallelRNG());
158 } else if (Parameters.StartingType == "CheckpointStartReseed") {
159 // Same as CheckpointRestart but reseed the RNGs using the fixed integer seeding used for ColdStart and HotStart
160 // Useful for creating new evolution streams from an existing stream
161
162 // WARNING: Unfortunately because the checkpointer doesn't presently allow us to separately restore the RNG and gauge fields we have to load
163 // an existing RNG checkpoint first; make sure one is available and named correctly
164 Resources.GetCheckPointer()->CheckpointRestore(Parameters.StartTrajectory, U,
165 Resources.GetSerialRNG(),
166 Resources.GetParallelRNG());
167 Resources.SeedFixedIntegers();
168 } else {
169 // others
170 std::cout << GridLogError << "Unrecognized StartingType\n";
171 std::cout
172 << GridLogError
173 << "Valid [HotStart, ColdStart, TepidStart, CheckpointStart, CheckpointStartReseed]\n";
174 exit(1);
175 }
176 }
177
178
179
181
182private:
183 template <class SmearingPolicy>
184 void Runner(SmearingPolicy &Smearing) {
185 auto UGrid = Resources.GetCartesian();
186 Field U(UGrid);
187
189
190 typedef IntegratorType<SmearingPolicy> TheIntegrator;
191 TheIntegrator MDynamics(UGrid, Parameters.MD, TheAction, Smearing);
192
193 // Sets the momentum filter
194 MDynamics.setMomentumFilter(*(Resources.GetMomentumFilter()));
195
196 Smearing.set_Field(U);
197
199 Resources.GetSerialRNG(),
200 Resources.GetParallelRNG(),
201 Resources.GetObservables(), U);
202
203 // Run it
204 HMC.evolve();
205 }
206};
207
208// These are for gauge fields, default integrator MinimumNorm2
209template <template <typename, typename, typename> class Integrator>
211template <template <typename, typename, typename> class Integrator>
213template <template <typename, typename, typename> class Integrator>
215
216
217// These are for gauge fields, default integrator MinimumNorm2
218template <template <typename, typename, typename> class Integrator>
220template <template <typename, typename, typename> class Integrator>
222template <template <typename, typename, typename> class Integrator>
224
225
226
227template <class RepresentationsPolicy,
228 template <typename, typename, typename> class Integrator>
231
232// sp2n
233
234template <template <typename, typename, typename> class Integrator>
236
237template <class RepresentationsPolicy,
238 template <typename, typename, typename> class Integrator>
241
242
243
244template <class Implementation, class RepresentationsPolicy,
245 template <typename, typename, typename> class Integrator>
247
250
253
254template <int Colours>
255using ScalarNxNAdjGenericHMCRunner = HMCWrapperTemplate < ScalarNxNAdjImplR<Colours>, ForceGradient, ScalarNxNMatrixFields<Colours> >;
256
258
259#endif // GRID_GENERIC_HMC_RUNNER
std::vector< ActionLevel< GaugeField, R > > ActionSet
Definition ActionSet.h:108
HMCWrapperTemplate< PeriodicGimplD, Integrator > GenericHMCRunnerD
HMCWrapperTemplate< ScalarImplR, MinimumNorm2, ScalarFields > ScalarGenericHMCRunner
HMCWrapperTemplate< ConjugateGimplR, Integrator > ConjugateHMCRunner
HMCWrapperTemplate< ConjugateGimplD, Integrator > ConjugateHMCRunnerD
HMCWrapperTemplate< Implementation, Integrator, RepresentationsPolicy > GenericHMCRunnerTemplate
HMCWrapperTemplate< PeriodicGimplF, Integrator > GenericHMCRunnerF
HMCWrapperTemplate< ScalarNxNAdjImplR< Colours >, ForceGradient, ScalarNxNMatrixFields< Colours > > ScalarNxNAdjGenericHMCRunner
HMCWrapperTemplate< PeriodicGimplR, Integrator > GenericHMCRunner
HMCWrapperTemplate< PeriodicGimplR, Integrator, RepresentationsPolicy > GenericHMCRunnerHirep
HMCWrapperTemplate< ScalarAdjImplR, MinimumNorm2, ScalarMatrixFields > ScalarAdjGenericHMCRunner
HMCWrapperTemplate< ConjugateGimplF, Integrator > ConjugateHMCRunnerF
HMCWrapperTemplate< SpPeriodicGimplR, Integrator > GenericSpHMCRunner
HMCWrapperTemplate< SpPeriodicGimplR, Integrator, RepresentationsPolicy > GenericSpHMCRunnerHirep
bool GridCmdOptionExists(char **begin, char **end, const std::string &option)
Definition Init.cc:137
void GridCmdOptionIntVector(const std::string &str, VectorInt &vec)
Definition Init.cc:160
std::string GridCmdOptionPayload(char **begin, char **end, const std::string &option)
Definition Init.cc:128
GridLogger GridLogError(1, "Error", GridLogColours, "RED")
GridLogger GridLogMessage(1, "Message", GridLogColours, "NORMAL")
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
static INTERNAL_PRECISION U
Definition Zolotarev.cc:230
virtual void Run()=0
virtual void initialize(ReaderClass &)=0
HMCWrapperTemplate(HMCparameters Par)
HMCWrapperTemplate()=default
Implementation ImplPolicy
Integrator< Implementation, S, RepresentationsPolicy > IntegratorType
void initialize(ReaderClass &TheReader)
void initializeGaugeFieldAndRNGs(Field &U)
void Run(SmearingPolicy &S)
INHERIT_FIELD_TYPES(Implementation)
HMCResourceManager< PeriodicGimplR > Resources
void ReadCommandLine(int argc, char **argv)
void Runner(SmearingPolicy &Smearing)
void evolve(void)
Definition HMC.h:248
Class for Molecular Dynamics management.
Definition Integrator.h:67
Representations< FundamentalRepresentation > NoHirep
Definition hmc_types.h:61
Representations< EmptyRep< typename ScalarNxNAdjImplR< Colours >::Field > > ScalarNxNMatrixFields
Definition hmc_types.h:66