CHROMA
syssolver_linop_OPTeigbicg.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /*! \file
3  * \brief Solve a M*psi=chi linear system by EigBiCG
4  */
5 
6 #ifndef __syssolver_OPTeigbicg_h__
7 #define __syssolver_OPTeigbicg_h__
8 #include "chroma_config.h"
9 
10 #include "handle.h"
11 #include "syssolver.h"
12 #include "linearop.h"
13 #include "named_obj.h"
15 
19 
20 #include "util/info/unique_id.h"
21 
22 namespace Chroma
23 {
24 
25  //! Eigenstd::vector accelerated biCG system solver namespace
26  namespace LinOpSysSolverOptEigBiCGEnv
27  {
28  //! Register the syssolver
29  bool registerAll();
30  }
31 
32 
33  //! Solve a M*psi=chi linear system by biCG with eigenvectors
34  /*! \ingroup invert
35  */
36  template<typename T>
38  {
39  public:
40 
41  //! Write out an OptEigInfo Type
43  StopWatch swatch;
44  swatch.reset();
45  swatch.start();
46  // A shorthand for the object
47  //const LinAlg::OptEigBiInfo<WordType<T>::Type_t>& obj =
48  // TheNamedObjMap::Instance().getData<LinAlg::OptEigBiInfo<WordType<T>::Type_t> >(invParam.eigen_id);
49 
52 
53  // File XML
54  XMLBufferWriter file_xml;
55  push(file_xml, "OptEigBiInfo");
56  write(file_xml, "id", uniqueId());
57  write(file_xml, "N", obj.N);
58  write(file_xml, "ncurEvals", obj.ncurEvals);
59  write(file_xml, "restartTol", obj.restartTol);
60  write(file_xml, "lde", obj.lde);
61  write(file_xml, "ldh", obj.evals.size());
62  pop(file_xml);
63 
64  // Open file
65  QDPFileWriter to(file_xml,
68  QDPIO_SERIAL,QDPIO_OPEN);
69 
70  for(int v(0);v<obj.ncurEvals;v++){
71  LatticeFermion lf ;
72  obj.CvToLatFerm(lf,subset(),v,"L");//left
73  {
74  XMLBufferWriter record_xml;
75  push(record_xml, "LeftEigenVector");
76  write(record_xml,"no",v);
77  pop(record_xml);
78  write(to, record_xml, lf);
79  }
80  obj.CvToLatFerm(lf,subset(),v,"R");//Right
81  {
82  XMLBufferWriter record_xml;
83  push(record_xml, "RightEigenVector");
84  write(record_xml,"no",v);
85  pop(record_xml);
86  write(to, record_xml, lf);
87  }
88  }
89 
90  {
91  XMLBufferWriter record_xml;
92  push(record_xml, "EigenValues");
93  pop(record_xml);
94  //write(to, record_xml, obj.evals);
95 
96  multi1d<Complex> foo(obj.evals.size()) ;
97  for(int i(0);i<obj.evals.size();i++)
98  foo[i].elem().elem().elem() = obj.evals[i] ;
99  write(to, record_xml, foo);
100  }
101  {
102  XMLBufferWriter record_xml;
103  push(record_xml, "H");
104  pop(record_xml);
105  //write(to, record_xml, obj.H);
106  multi1d<Complex> foo(obj.H.size()) ;
107  for(int i(0);i<obj.H.size();i++)
108  foo[i].elem().elem().elem() = obj.H[i] ;
109  write(to, record_xml, foo);
110  }
111 
112  // Close
113  close(to);
114  swatch.stop();
115  QDPIO::cout<<" QIOWriteOptEvecs: Time to write evecs= "
116  << swatch.getTimeInSeconds() <<" secs "<<std::endl ;
117  }
118 
119  //-----------------------------------------------------------------------
120  //! Read a OptEigInfo Type
122  {
123 
124  StopWatch swatch;
125  swatch.reset();
126  swatch.start();
127 
128  // File XML
129  XMLReader file_xml;
130 
131  // Open file
132  QDPFileReader to(file_xml,invParam.file.file_name,QDPIO_SERIAL);
133 
134  // A shorthand for the object
137 
138  XMLReader record_xml;
139  //TheNamedObjMap::Instance().get(buffer_id).setRecordXML(record_xml);
140 
141  int ldh,lde,N ;
142  float restartTol ;
143  read(file_xml, "/OptEigBiInfo/ldh", ldh);
144  read(file_xml, "/OptEigBiInfo/lde", lde);
145  read(file_xml, "/OptEigBiInfo/N", N);
146  read(file_xml, "/OptEigBiInfo/ncurEvals", obj.ncurEvals);
147  read(file_xml, "/OptEigBiInfo/restartTol", restartTol);
148 
149  QDPIO::cout <<__func__ << " : Reading object with following properties"<<std::endl ;
150  QDPIO::cout <<__func__ << " : lde= " << lde << std::endl;
151  QDPIO::cout <<__func__ << " : ldh= " << ldh << std::endl;
152  QDPIO::cout <<__func__ << " : N = " << N << std::endl;
153 
154  QDPIO::cout << __func__ << " : ncurEvals = " << obj.ncurEvals<< std::endl;
155  QDPIO::cout << __func__ << " : restartTol = " << restartTol<< std::endl;
156 
157  if(obj.evals.size()< obj.ncurEvals){
158  QDPIO::cerr<<__func__<< " : ldh of the current object is not large enough to hold the vectors" ;
159  QDP_abort(1);
160  }
161 
162  for(int v(0);v<obj.ncurEvals;v++){
163  LatticeFermion lf ;
164  XMLReader record_xml;
165  read(to, record_xml, lf);
166  obj.CvToEigCGvec(lf, subset(), v,"L") ;
167  read(to, record_xml, lf);
168  obj.CvToEigCGvec(lf, subset(), v,"R") ;
169  }
170  //this is just fine
171  {
172  XMLReader record_xml;
173  multi1d<Complex> evals(ldh) ;
174  multi1d<Complex> H(ldh*ldh) ;
175  read(to, record_xml, evals);
176  read(to, record_xml, H);
177  if(ldh<=obj.evals.size()){
178  for(int i(0);i<ldh;i++){
179  obj.evals[i] = evals[i].elem().elem().elem() ;
180  }
181  for(int i(0);i<H.size();i++){
182  obj.H[i] = H[i].elem().elem().elem() ;
183  }
184  }
185  else{
186  QDPIO::cerr<<__func__<< " : ldh of the current object is not large enough to hold the Cholesky factors" ;
187  QDP_abort(1);
188  }
189  }
190 
191  // Close
192  close(to);
193 
194  swatch.stop();
195  QDPIO::cout<<" QIOReadOptEvecs: Time to read evecs= "
196  << swatch.getTimeInSeconds() <<" secs "<<std::endl ;
197  }
198 
199  //! Constructor
200  /*!
201  * \param M_ Linear operator ( Read )
202  * \param invParam_ inverter parameters ( Read )
203  */
205  const SysSolverOptEigBiCGParams& invParam_) :
206  A(A_), invParam(invParam_)
207  {
208  numMatvecs = 0 ;
209  // NEED to grab the eignvectors from the named buffer here
211  {
215  int N = Layout::sitesOnNode()*Nc*Ns ;
216  int VectorSpaceSize = Nc*Ns*(A->subset()).numSiteTable();
217  EigInfo.init(invParam.Neig_max, N, VectorSpaceSize) ;
218  EigInfo.restartTol = invParam.restartTol.elem().elem().elem().elem();
219  if(invParam.file.read){
220  QDPIO::cout<<"LinOpSysSolverOptEigBiCG : reading evecs from disk"<<std::endl ;
221  QIOReadOptEvecs() ;
222  }
223  }
224  }
225 
226  //! Destructor is automatic
228  {
229  if(invParam.file.write){
230  QDPIO::cout<<"LinOpSysSolverOptEigBiCG : writing evecs to disk"<<std::endl ;
231  QIOWriteOptEvecs() ;
232  }
234  {
236  }
237  }
238 
239  //! Return the subset on which the operator acts
240  const Subset& subset() const {return A->subset();}
241 
242  //! Solver the linear system
243  /*!
244  * \param psi solution ( Modify )
245  * \param chi source ( Read )
246  * \return syssolver results
247  *
248  * Definitions supplied in the correspond .cc file
249  */
251 
252 
253  /*********** NO CHRONO FOR BICGSTAB ***********
254  //! Solve the linear system starting with a chrono guess
255  /*!
256  * \param psi solution (Write)
257  * \param chi source (Read)
258  * \param predictor a chronological predictor (Read)
259  * \return syssolver results
260  */
261  /*********** NO CHRONO FOR BICGSTAB ***********
262  SystemSolverResults_t operator()(T& psi, const T& chi,
263  AbsChronologicalPredictor4D<T>& predictor) const
264  {
265 
266  START_CODE();
267 
268  // I need to predict with A
269  {
270  predictor(psi, A, chi);
271  }
272  // Do solve
273  SystemSolverResults_t res=(*this)(psi,chi);
274 
275  // Store result
276  predictor.newVector(psi);
277  END_CODE();
278  return res;
279  }
280  **********************/
281 
282 
283  private:
284 
285  // Hide default constructor
290  };
291 
292 } // End namespace
293 
294 
295 
296 #endif
297 
Class for counted reference semantics.
Definition: handle.h:33
void init(int ldh, int lde_)
Definition: containers.h:134
Solve a M*psi=chi linear system by biCG with eigenvectors.
void QIOWriteOptEvecs()
Write out an OptEigInfo Type
LinOpSysSolverOptEigBiCG(Handle< LinearOperator< T > > A_, const SysSolverOptEigBiCGParams &invParam_)
Constructor.
const Subset & subset() const
Return the subset on which the operator acts.
~LinOpSysSolverOptEigBiCG()
Destructor is automatic.
SystemSolverResults_t operator()(T &psi, const T &chi) const
Solver the linear system.
void QIOReadOptEvecs()
Read a OptEigInfo Type
SystemSolver disambiguator.
Linear Operator.
Definition: linearop.h:27
static T & Instance()
Definition: singleton.h:432
void read(XMLReader &xml, const std::string &path, AsqtadFermActParams &param)
Read parameters.
void write(XMLWriter &xml, const std::string &path, const AsqtadFermActParams &param)
Writer parameters.
std::string uniqueId()
Return a unique id.
Definition: unique_id.cc:18
Class for counted reference semantics.
Handle< MapObject< int, EVPair< LatticeColorVector > > > obj
Linear Operators.
Named object support.
Named object function std::map.
bool registerAll()
Register all the factories.
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
push(xml_out,"Condensates")
LinOpSysSolverMGProtoClover::T T
int i
Definition: pbg5p_w.cc:55
multi1d< LatticeFermion > chi(Ncb)
LatticeFermion psi
Definition: mespbg5p_w.cc:35
pop(xml_out)
struct Chroma::SysSolverOptEigBiCGParams::File_t file
Holds return info from SystemSolver call.
Definition: syssolver.h:17
Linear system solvers.
Solve a biCG system.
Disambiguator for LinOp system solvers.
Generate a unique id.