CHROMA
chroma_init.cc
Go to the documentation of this file.
1 /*! \file
2  * \brief Initialization of Chroma
3  */
4 
5 #include "chroma_config.h"
6 
7 #include "init/chroma_init.h"
8 #include "io/xmllog_io.h"
9 
10 #include "qdp_init.h"
11 
12 #if defined(BUILD_JIT_CLOVER_TERM)
13 #if defined(QDPJIT_IS_QDPJITPTX)
14 #include "../actions/ferm/linop/clover_term_ptx_w.h"
15 #endif
16 #endif
17 
18 #ifdef BUILD_QUDA
19 #include <quda.h>
20 #endif
21 
22 // Indlude it no-matter what
23 #ifdef BUILD_QPHIX
24 #include "../qphix_singleton.h"
25 #ifdef CHROMA_BUILDING_QPHIX_DSLASH
26 #include "qdp_datalayout.h"
27 #include "qphix/geometry.h"
29 #endif
30 #endif
31 
32 #ifdef BUILD_MGPROTO
33 #include "MG_config.h"
34 #include "utils/memory.h"
35 #include "utils/initialize.h"
36 #ifdef MG_ENABLE_TIMERS
37 #include "utils/timer.h"
38 #endif
39 #endif
40 
41 namespace Chroma
42 {
43 
44  //! Private (anonymous) namespace
45  namespace
46  {
47  //! The "DATA" filename
48  std::string input_filename = "DATA";
49 
50  //! The "XMLDAT" filename
51  std::string output_filename = "XMLDAT";
52 
53  //! The "XMLLOG" filename
54  std::string log_filename = "XMLLOG";
55 
56  //! The "current working directory" -- prepended to above if set
57  std::string cwd = ".";
58 
59  //! Has the xml output instance been created?
60  bool xmlOutputP = false;
61 
62  //! Has the xml log instance been created?
63  bool xmlLogP = false;
64 
65  //! Has the input instance been created?
66  // bool xmlInputP = false;
67 
68 
69  // Internal
70  std::string constructFileName(const std::string& filename)
71  {
72  std::string ret_val;
73  if ( filename[0] == '.' || filename[0]=='/' ) {
74  // Fully qualified pathname
75  ret_val = filename;
76  }
77  else {
78  // Prepend CWD
79  ret_val = getCWD() + "/" + filename;
80  }
81  return ret_val;
82  }
83 
84 
85  } // End anonymous namespace
86 
87  //! Get input file name
88  std::string getXMLInputFileName() {return constructFileName(input_filename);}
89 
90  //! Get output file name
91  std::string getXMLOutputFileName() {return constructFileName(output_filename);}
92 
93  //! Get log file name
94  std::string getXMLLogFileName() {return constructFileName(log_filename);}
95 
96  //! Get current working directory
97  std::string getCWD() {return cwd;}
98 
99 
100  //! Set input file name
101  void setXMLInputFileName(const std::string& name) {input_filename = name;}
102 
103  //! Set output file name
104  void setXMLOutputFileName(const std::string& name) {output_filename = name;}
105 
106  //! Set output logfile name
107  void setXMLLogFileName(const std::string& name) {log_filename = name;}
108 
109  //! Set current working directory
110  void setCWD(const std::string& name) {cwd = name;}
111 
112 
113  //! Chroma initialisation routine
114  void initialize(int* argc, char ***argv)
115  {
116 #if defined QDPJIT_IS_QDPJITPTX || defined QDPJIT_IS_QDPJITNVVM
117  if (! QDP_isInitialized())
118  QDP_initialize_CUDA(argc, argv);
119 #else
120  if (! QDP_isInitialized())
121  QDP_initialize(argc, argv);
122 #endif
123 
124  for(int i=0; i < *argc; i++)
125  {
126  // Get argv[i] into a std::string
127  std::string argv_i = std::string( (*argv)[i] );
128 
129  // Search for -i or --chroma-i
130  if( argv_i == std::string("-h") || argv_i == std::string("--help") )
131  {
132  QDPIO::cerr << "Usage: " << (*argv)[0] << " <options>" << std::endl
133  << " -h help\n"
134  << " --help help\n"
135  << " -i [" << getXMLInputFileName() << "] xml input file name\n"
136  << " --chroma-i [" << getXMLInputFileName() << "] xml input file name\n"
137  << " -o [" << getXMLOutputFileName() << "] xml output file name\n"
138  << " --chroma-p [" << getXMLOutputFileName() << "] xml output file name\n"
139  << " -l [" << getXMLLogFileName() << "] xml log file name\n"
140  << " --chroma-l [" << getXMLLogFileName() << "] xml log file name\n"
141  << " -cwd [" << getCWD() << "] xml working directory\n"
142  << " --chroma-cwd [" << getCWD() << "] xml working directory\n"
143 
144 
145  << std::endl;
146  QDP_abort(0);
147  }
148 
149  // Search for -i or --chroma-i
150  if( argv_i == std::string("-i") || argv_i == std::string("--chroma-i") )
151  {
152  if( i + 1 < *argc )
153  {
154  setXMLInputFileName(std::string( (*argv)[i+1] ));
155  // Skip over next
156  i++;
157  }
158  else
159  {
160  // i + 1 is too big
161  QDPIO::cerr << "Error: dangling -i specified. " << std::endl;
162  QDP_abort(1);
163  }
164  }
165 
166  // Search for -o or --chroma-o
167  if( argv_i == std::string("-o") || argv_i == std::string("--chroma-o") )
168  {
169  if( i + 1 < *argc ) {
170  setXMLOutputFileName(std::string( (*argv)[i+1] ));
171  // Skip over next
172  i++;
173  }
174  else {
175  // i + 1 is too big
176  QDPIO::cerr << "Error: dangling -o specified. " << std::endl;
177  QDP_abort(1);
178  }
179  }
180 
181  // Search for -l or --chroma-l
182  if( argv_i == std::string("-l") || argv_i == std::string("--chroma-l") )
183  {
184  if( i + 1 < *argc ) {
185  setXMLLogFileName(std::string( (*argv)[i+1] ));
186  // Skip over next
187  i++;
188  }
189  else {
190  // i + 1 is too big
191  QDPIO::cerr << "Error: dangling -l specified. " << std::endl;
192  QDP_abort(1);
193  }
194  }
195 
196  // Search for -cwd or --chroma-cwd
197  if( argv_i == std::string("-cwd") || argv_i == std::string("--chroma-cwd") )
198  {
199  if( i + 1 < *argc ) {
200  setCWD(std::string( (*argv)[i+1] ));
201  // Skip over next
202  i++;
203  }
204  else {
205  // i + 1 is too big
206  QDPIO::cerr << "Error: dangling -cwd specified. " << std::endl;
207  QDP_abort(1);
208  }
209  }
210 
211  }
212 
213 
214 #if defined QDPJIT_IS_QDPJITPTX || defined QDPJIT_IS_QDPJITNVVM
215 #ifdef BUILD_QUDA
216  std::cout << "Setting CUDA device" << std::endl;
217 #ifndef QDP_USE_COMM_SPLIT_INIT
218  int cuda_device = QDP_setGPU();
219 #endif
220  std::cout << "Initializing QMP part" << std::endl;
221  QDP_initialize_QMP(argc, argv);
222 #ifdef QDP_USE_COMM_SPLIT_INIT
223  int cuda_device = QDP_setGPUCommSplit();
224 #endif
225  setVerbosityQuda(QUDA_SUMMARIZE, "", stdout);
226 
227  QDPIO::cout << "Initializing QUDA device (using CUDA device no. " << cuda_device << ")" << std::endl;
228 
229  initQudaDevice(cuda_device);
230  QDPIO::cout << "Initializing QDP-JIT GPUs" << std::endl;
231  QDP_startGPU();
232  QDPIO::cout << "Initializing QUDA memory" << std::endl;
233  initQudaMemory();
234 #else
235  std::cout << "Setting device" << std::endl;
236 #ifndef QDP_USE_COMM_SPLIT_INIT
237  QDP_setGPU();
238 #endif
239  std::cout << "Initializing QMP part" << std::endl;
240  QDP_initialize_QMP(argc, argv);
241 #ifdef QDP_USE_COMM_SPLIT_INIT
242  QDP_setGPUCommSplit();
243 #endif
244  QDPIO::cout << "Initializing start GPUs" << std::endl;
245  QDP_startGPU();
246 #endif
247 #else
248 #ifdef BUILD_QUDA
249  std::cout << "Initializing QUDA" << std::endl;
250  initQuda(-1);
251 #endif
252 #endif
253 
254 
255 #ifdef BUILD_QPHIX
256  QDPIO::cout << "Initializing QPhiX CLI Args" << std::endl;
257  QPhiX::QPhiXCLIArgs& QPhiXArgs = TheQPhiXParams::Instance();
258  QPhiXArgs.init((*argc),(*argv));
259  QDPIO::cout << "QPhiX CLI Args Initialized" << std::endl;
260  QDPIO::cout << " QPhiX: By="<< QPhiXArgs.getBy() << std::endl;
261  QDPIO::cout << " QPhiX: Bz="<< QPhiXArgs.getBz() << std::endl;
262  QDPIO::cout << " QPhiX: Pxy="<< QPhiXArgs.getPxy() << std::endl;
263  QDPIO::cout << " QPhiX: Pxyz="<< QPhiXArgs.getPxyz() << std::endl;
264  QDPIO::cout << " QPhiX: NCores="<< QPhiXArgs.getNCores() << std::endl;
265  QDPIO::cout << " QPhiX: Sy="<< QPhiXArgs.getSy() << std::endl;
266  QDPIO::cout << " QPhiX: Sz="<< QPhiXArgs.getSz() << std::endl;
267  QDPIO::cout << " QPhiX: MinCt="<< QPhiXArgs.getMinCt() << std::endl;
268 
269  int num_threads = QPhiXArgs.getNCores()*QPhiXArgs.getSy()*QPhiXArgs.getSz();
270  if( qdpNumThreads() != num_threads ) {
271  QDPIO::cerr << "ChromaInit: qdpNumThreads is different from NCores*Sy*Sz" << std::endl;
272  QDP_abort(1);
273  }
274 
275  #if defined(QDP_IS_QDPJIT)
276  // For QDP-JIT We insist on matchig QDP-JIT layout and QPhiX Layout for now
277 
278  // Want ocsri layout. So pos_o = 0, pos_s=2, pos_c=1, pos_r=3, pos_i=4
279  if (! QDP_assert_jit_datalayout(0,2,1,3,4) ) {
280  QDPIO::cerr << "ChromaInit: DataLayout Ordering Mismatch. Wanted ocsri layout, but have ";
281  QDP_print_jit_datalayout();
282  QDP_abort(1);
283  }
284 
285  // Check QDP Inner length is the SOALEN
286  int64_t layout_inner_size=QDP::getDataLayoutInnerSize();
287  if( layout_inner_size != CHROMA_QPHIX_SOALEN ) {
288  QDPIO::cout << "ChromaInit: Our SOA Length is " << CHROMA_QPHIX_SOALEN << " but QDP-JIT has inner="<< layout_inner_size << std::endl;
289  QDP_abort(1);
290  }
291 #endif
292 #endif
293 
294 #ifdef BUILD_MGPROTO
295  // Initialzie MG Proto memory
296  QDPIO::cout << "Initializing MG_proto memory system" << std::endl;
297  MG::InitMemory(argc,argv);
298 #ifdef BUILD_QPHIX
299  QDPIO::cout << "Initializing QPhiX CLI Args for MG_proto" << std::endl;
300  MG::InitCLIArgs(argc,argv);
301 #endif
302 #endif
303 
304  }
305 
306 
307  //! Chroma finalization routine
308  void finalize(void)
309  {
310 
311 #ifdef BUILD_QUDA
312  endQuda();
313 #endif
314 
315 #ifdef BUILD_MGPROTO
316 #ifdef MG_ENABLE_TIMERS
317  (MG::Timer::TimerAPI::getInstance())->reportAllTimer();
318 #endif
319 
320  MG::FinalizeMemory();
321 #endif
322 
323 #if defined(BUILD_JIT_CLOVER_TERM)
324 #if defined(QDPJIT_IS_QDPJITPTX)
325  QDP_info_primary("Time for packForQUDA: %f sec",PackForQUDATimer::Instance().get() / 1.0e6);
326 #endif
327 #endif
328 
329  if (! QDP_isInitialized())
330  return;
331 
332 
333  /*
334  if( xmlInputP ) {
335  Chroma::getXMLInputInstance().close();
336  }
337  */
338  if( xmlOutputP ) {
340  }
341  if( xmlLogP ) {
342  Chroma::getXMLLogInstance().close();
343  }
344 
345  QDP_finalize();
346 
347 
348  }
349 
350 
351  //! Chroma abort routine
352  void abort(int i)
353  {
354  QDP_abort(i);
355  }
356 
357 
358  //! Get xml output instance
359  XMLFileWriter& getXMLOutputInstance()
360  {
361  if (! xmlOutputP)
362  {
363  try {
365  }
366  catch(...) {
367  QDPIO::cerr << "Unable to open " << getXMLOutputFileName() << std::endl;
368  QDP_abort(1);
369  }
370 
371  xmlOutputP = true;
372  }
373 
375  }
376 
377  //! Get xml log instance
378  XMLFileWriter& getXMLLogInstance()
379  {
380  if (! xmlLogP)
381  {
382  try {
384  }
385  catch(...) {
386  QDPIO::cerr << "Unable to open " << getXMLLogFileName() << std::endl;
387  QDP_abort(1);
388  }
389 
390  xmlLogP = true;
391  }
392 
393  return TheXMLLogWriter::Instance();
394  }
395 
396  /*
397  //! Get xml input instance
398  XMLReader& getXMLInputInstance()
399  {
400  if(! xmlInputP ) {
401  try {
402  TheXMLInputReader::Instance().open(getXMLInputFileName());
403  }
404  catch() {
405  QDPIO::cerr << "Unable to open " << pathname << std::endl;
406  QDP_abort(1);
407  }
408  xmlInputP = true;
409  }
410 
411  return TheXMLInputReader::Instance();
412  }
413  */
414 
415 
416 }
Initialization of Chroma.
static T & Instance()
Definition: singleton.h:432
static PackForQUDATimer & Instance()
void get(multi1d< LatticeColorMatrix > &u, XMLBufferWriter &file_xml, XMLBufferWriter &record_xml)
Get the default gauge field.
const std::string name
Name to be used.
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
std::string getXMLOutputFileName()
Get output file name.
Definition: chroma_init.cc:91
XMLFileWriter & getXMLLogInstance()
Get xml log instance.
Definition: chroma_init.cc:378
void setCWD(const std::string &name)
Set current working directory.
Definition: chroma_init.cc:110
void setXMLOutputFileName(const std::string &name)
Set output file name.
Definition: chroma_init.cc:104
int i
Definition: pbg5p_w.cc:55
void initialize(int *argc, char ***argv)
Chroma initialisation routine.
Definition: chroma_init.cc:114
void finalize(void)
Chroma finalization routine.
Definition: chroma_init.cc:308
std::string getCWD()
Get current working directory.
Definition: chroma_init.cc:97
std::string getXMLInputFileName()
Get input file name.
Definition: chroma_init.cc:88
std::string getXMLLogFileName()
Get log file name.
Definition: chroma_init.cc:94
void setXMLInputFileName(const std::string &name)
Set input file name.
Definition: chroma_init.cc:101
void abort(int i)
Chroma abort routine.
Definition: chroma_init.cc:352
void setXMLLogFileName(const std::string &name)
Set output logfile name.
Definition: chroma_init.cc:107
XMLFileWriter & getXMLOutputInstance()
Get xml output instance.
Definition: chroma_init.cc:359
::std::string string
Definition: gtest.h:1979
Singleton instances of xml output.