CHROMA
mg_proto_helpers.cc
Go to the documentation of this file.
1 /*
2  * mg_proto_helpers.cpp
3  *
4  * Created on: Mar 24, 2017
5  * Author: bjoo
6  */
7 #include "chromabase.h"
9 
11 
12 #include "lattice/fine_qdpxx/mg_params_qdpxx.h"
13 #include "lattice/fine_qdpxx/mg_level_qdpxx.h"
14 #include "lattice/fine_qdpxx/vcycle_recursive_qdpxx.h"
15 #include "lattice/fine_qdpxx/wilson_clover_linear_operator.h"
16 
17 #include <vector>
18 #include <memory>
19 
20 using std::shared_ptr;
21 using std::make_shared;
22 using std::vector;
23 
24 using namespace QDP;
25 
26 
27 namespace Chroma {
28 namespace MGProtoHelpers {
29 
30 shared_ptr<const MG::QDPWilsonCloverLinearOperator>
31 createFineLinOp( const MGProtoSolverParams& params, const multi1d<LatticeColorMatrix>& u)
32 {
33  shared_ptr<const MG::QDPWilsonCloverLinearOperator> M_fine=nullptr;
34 
35  bool anisoP = params.CloverParams.anisoParam.anisoP;
36  double m_q=toDouble(params.CloverParams.Mass);
37  double u0 = toDouble(params.CloverParams.u0);
38 
39 #if 0
40  int t_bc = params.AntiPeriodicT ? -1 : 1; - ignore as links already have BC on.
41 #else
42  int t_bc = 1;
43 #endif
44 
45  if( !anisoP ) {
46  double c_sw=toDouble(params.CloverParams.clovCoeffR);
47  M_fine = make_shared<const MG::QDPWilsonCloverLinearOperator>(m_q,c_sw,t_bc,u);
48  }
49  else {
50  QDPIO::cout << "Using aniso interface" << std::endl;
51 
52  double xi0 = toDouble(params.CloverParams.anisoParam.xi_0);
53  double nu=toDouble(params.CloverParams.anisoParam.nu);
54  double c_sw_r = toDouble(params.CloverParams.clovCoeffR);
55  double c_sw_t = toDouble(params.CloverParams.clovCoeffT);
56  M_fine = make_shared<const MG::QDPWilsonCloverLinearOperator>(m_q,u0,xi0,nu,c_sw_r,c_sw_t,t_bc,u);
57  }
58 
59  return M_fine;
60 }
61 
62 void
63 createMGPreconditioner( const MGProtoSolverParams& params, const multi1d<LatticeColorMatrix>& u)
64 {
65  START_CODE();
66  StopWatch swatch;
67  swatch.reset();
68  swatch.start();
69 
70  const std::string& subspaceId = params.SubspaceId;
71  QDPIO::cout << "Creating MG_Proto preconditioner with subspaceID=" << subspaceId << std::endl;
72  // Look up in named object map
73  QDPIO::cout << "Looking Up " << subspaceId << " in the Named Onject Map" << std::endl;
74  if( TheNamedObjMap::Instance().check(subspaceId) ) {
75  // Found it... Delete it.
76  QDPIO::cout << " ... Subspace ID found... Deleting" <<std::endl;
77  deleteMGPreconditioner(subspaceId);
78  }
79 
80 
81  // Now make a new one.
82  shared_ptr<MG::MultigridLevels> mg_levels = make_shared<MG::MultigridLevels>();
83 
84  // First make M
85  QDPIO::cout << "Creating M..." ;
86  shared_ptr<const MG::QDPWilsonCloverLinearOperator> M_fine=createFineLinOp( params, u );
87 
88  QDPIO::cout << "Done" << std::endl;
89  QDPIO::cout << "Creating MG Levels ... " ;
90  MG::SetupParams level_params;
91  int n_levels = params.MGLevels;
92  level_params.n_levels = n_levels;
93  level_params.n_vecs.resize(n_levels-1);
94  level_params.null_solver_max_iter.resize(n_levels-1);
95  level_params.null_solver_rsd_target.resize(n_levels -1);
96  level_params.null_solver_verboseP.resize(n_levels -1);
97  for(int l=0; l < n_levels-1;++l) {
98  QDPIO::cout << "Level L=" << l << " Null Vecs=" << params.NullVecs[l] << std::endl;
99 
100  level_params.n_vecs[l] = params.NullVecs[l];
101  level_params.null_solver_max_iter[l]=params.NullSolverMaxIters[l];
102  level_params.null_solver_rsd_target[l]=toDouble(params.NullSolverRsdTarget[l]);
103  level_params.null_solver_verboseP[l]=toDouble(params.NullSolverVerboseP[l]);
104  }
105  level_params.block_sizes.resize(n_levels-1);
106  for(int l=0; l < n_levels-1;++l) {
107  for(int mu=0; mu < 4; ++mu) {
108  (level_params.block_sizes[l])[mu] = (params.Blocking[l])[mu];
109  }
110  }
111 
112  MG::SetupMGLevels(level_params, *mg_levels, M_fine );
113  QDPIO::cout << "... Done " << std::endl;
114 
115  QDPIO::cout << "Creating VCycle Parameters..." << std::endl;
116  vector<MG::VCycleParams> v_params(n_levels-1);
117  for(int l=0; l < n_levels-1;++l) {
118  QDPIO::cout << " Level " << l << std::endl;
119  v_params[l].pre_smoother_params.MaxIter=params.VCyclePreSmootherMaxIters[l];
120  v_params[l].pre_smoother_params.RsdTarget=toDouble(params.VCyclePreSmootherRsdTarget[l]);
121  v_params[l].pre_smoother_params.VerboseP =params.VCyclePreSmootherVerboseP[l];
122  v_params[l].pre_smoother_params.Omega =toDouble(params.VCyclePreSmootherRelaxOmega[l]);
123 
124  v_params[l].post_smoother_params.MaxIter=params.VCyclePostSmootherMaxIters[l];
125  v_params[l].post_smoother_params.RsdTarget=toDouble(params.VCyclePostSmootherRsdTarget[l]);
126  v_params[l].post_smoother_params.VerboseP =params.VCyclePostSmootherVerboseP[l];
127  v_params[l].post_smoother_params.Omega =toDouble(params.VCyclePostSmootherRelaxOmega[l]);
128 
129  v_params[l].bottom_solver_params.MaxIter=params.VCycleBottomSolverMaxIters[l];
130  v_params[l].bottom_solver_params.NKrylov = params.VCycleBottomSolverNKrylov[l];
131  v_params[l].bottom_solver_params.RsdTarget= toDouble(params.VCycleBottomSolverRsdTarget[l]);
132  v_params[l].bottom_solver_params.VerboseP = params.VCycleBottomSolverVerboseP[l];
133 
134  v_params[l].cycle_params.MaxIter=params.VCycleMaxIters[l];
135  v_params[l].cycle_params.RsdTarget=toDouble(params.VCycleRsdTarget[l]);
136  v_params[l].cycle_params.VerboseP = params.VCycleVerboseP[l];
137  }
138 
139  QDPIO::cout << "Creating VCycle Preconditioner...";
140 
141  shared_ptr<MG::VCycleRecursiveQDPXX> v_cycle=make_shared<MG::VCycleRecursiveQDPXX>(v_params, *mg_levels);
142 
143  QDPIO::cout << "Done";
144 
145  QDPIO::cout << "Saving in Map" << std::endl;
146  QDPIO::cout << "Creating Named Object Map Entry for subspace" << std::endl;
147  XMLBufferWriter file_xml;
148  push(file_xml, "FileXML");
149  pop(file_xml);
150 
151  XMLBufferWriter record_xml;
152  push(record_xml, "RecordXML");
153  write(record_xml, "InvertParam", params);
154  pop(record_xml);
155 
156  TheNamedObjMap::Instance().create<shared_ptr<MGPreconditioner>>(subspaceId);
157  TheNamedObjMap::Instance().get(subspaceId).setFileXML(file_xml);
158  TheNamedObjMap::Instance().get(subspaceId).setRecordXML(record_xml);
159  TheNamedObjMap::Instance().getData<shared_ptr<MGPreconditioner>>(subspaceId)=make_shared<MGPreconditioner>();
160  TheNamedObjMap::Instance().getData<shared_ptr<MGPreconditioner>>(subspaceId)->mg_levels = mg_levels;
161  TheNamedObjMap::Instance().getData<shared_ptr<MGPreconditioner>>(subspaceId)->v_cycle = v_cycle;
162 
163  swatch.stop();
164  QDPIO::cout << "MG_PROTO_SETUP: Subspace Creation Took : " << swatch.getTimeInSeconds() << " sec" << std::endl;
165 
166  END_CODE();
167 }
168 
169 void deleteMGPreconditioner(const std::string& subspaceId)
170 {
171 
172  QDPIO::cout << "Deleting MG_Proto preconditioner with subspaceID=" << subspaceId << std::endl;
173  // Look up in named object map
174  QDPIO::cout << "Looking Up " << subspaceId << " in the Named Onject Map" << std::endl;
175  if( TheNamedObjMap::Instance().check(subspaceId) ) {
176  // Found it... Delete it.
177  QDPIO::cout << " ... Subspace ID found... Deleting" <<std::endl;
178 
179  // This will erase the MGPreconditioner, which has in it shared pointers.
180  // If the shared pointers are destroyed, and there are no references remaining
181  // then MG Levels will be destroyed as weill v_cycle
182  // These only hold allocated data by shared pointer, so they too should be cleaned
183  TheNamedObjMap::Instance().erase(subspaceId);
184  }
185 }
186 
187 shared_ptr<MGPreconditioner> getMGPreconditioner(const std::string& subspaceId)
188 {
189  shared_ptr<MGPreconditioner> ret_val = nullptr;
190  if( TheNamedObjMap::Instance().check(subspaceId) ) {
191  // Found it... Delete it.
192  QDPIO::cout << " ... Subspace ID found... returning" <<std::endl;
193 
194  // This will erase the MGPreconditioner, which has in it shared pointers.
195  // If the shared pointers are destroyed, and there are no references remaining
196  // then MG Levels will be destroyed as weill v_cycle
197  // These only hold allocated data by shared pointer so they too should be cleaned
198  ret_val = TheNamedObjMap::Instance().getData<shared_ptr<MGPreconditioner>>(subspaceId);
199 
200  }
201  else {
202  QDPIO::cout << "Object Not Found... Returning Null" << std::endl;
203  ret_val = nullptr;
204 
205  }
206  return ret_val;
207 
208 }
209 
210 
211 
212 
213 }
214 
215 }
Primary include file for CHROMA library code.
#define END_CODE()
Definition: chromabase.h:65
#define START_CODE()
Definition: chromabase.h:64
Real u0
int mu
Definition: cool.cc:24
int nu
Definition: cool.cc:25
void write(XMLWriter &xml, const std::string &path, const AsqtadFermActParams &param)
Writer parameters.
Params params
Named object function std::map.
shared_ptr< MGPreconditioner > getMGPreconditioner(const std::string &subspaceId)
shared_ptr< const MG::QDPWilsonCloverLinearOperator > createFineLinOp(const MGProtoSolverParams &params, const multi1d< LatticeColorMatrix > &u)
void createMGPreconditioner(const MGProtoSolverParams &params, const multi1d< LatticeColorMatrix > &u)
void deleteMGPreconditioner(const std::string &subspaceId)
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
static multi1d< LatticeColorMatrix > u
static bool anisoP()
Definition: grelax.cc:17
::std::string string
Definition: gtest.h:1979
int l
Definition: pade_trln_w.cc:111
push(xml_out,"Cooled_Topology")
pop(xml_out)