PlasCom2  1.0
XPACC Multi-physics simluation application
PC2ParallelTestDriver.C
Go to the documentation of this file.
1 #include "ComLine.H"
11 #include "TestPlasCom2.H"
12 #include "COMM.H"
13 
14 
15 namespace plascom2 {
16 
50  int ParallelTest(int argc,char *argv[])
51  {
52 
53  // The default verbosity is 0
54  int verblevel = 0;
55 
56  // This sets everything up with MPI
57  MPI_Comm myComm = MPI_COMM_WORLD;
58  plascom2::CommType communicator(myComm);
59  int rank = communicator.Rank();
60  int nproc = communicator.Size();
61  bool do_stdout = !rank;
62 
63  // This line creates the PlasCom2::TestComLine object and passes in
64  // the command line arguments as a (const char **).
65  TestComLine comline((const char **)(argv));
66  // The call to comline.Initialize() reads the command line arguments
67  // from the array passed in the previous line.
68  comline.Initialize();
69 
70 
71  // The ProcessOptions() call does detailed examination of the command
72  // line arguments to check for user errors or other problems. This call
73  // will return non-zero if there were errors on the commandline.
74  int clerr = comline.ProcessOptions();
75  // Check if the user just wanted to get the help and exit
76  if(!comline.GetOption("help").empty()){
77  // Print out the "long usage" (i.e. help) message to stdout
78  if(do_stdout){
79  std::cout << comline.LongUsage() << std::endl;
80  if(verblevel > 1)
81  std::cout << "PlasCom2::ParallelTest: Exiting test function (success)"
82  << std::endl;
83  }
84  communicator.SetExit(1);
85  }
86  if(communicator.Check())
87  return(1);
88  if(clerr){
89  if(do_stdout){
90  std::cout << comline.ErrorReport() << std::endl
91  << std::endl << comline.ShortUsage() << std::endl;
92  if(verblevel > 2)
93  std::cout << "PlasCom2::ParallelTest: Exiting test function (fail)" << std::endl;
94  }
95  communicator.SetExit(1);
96  }
97  if(communicator.Check())
98  return(2);
99 
100  // These outstreams allow the output to file to be set up and separated
101  // from the stdout.
102  std::ofstream Ouf;
103  std::ostream *Out = NULL;
104  if(do_stdout)
105  Out = &std::cout;
106 
107  // The next few lines populate some strings based on the
108  // users input from the commandline.
109  std::string OutFileName(comline.GetOption("output"));
110  std::string TestName(comline.GetOption("name"));
111  std::string ListName(comline.GetOption("list"));
112  std::string sverb(comline.GetOption("verblevel"));
113 
114  // The following block parses and sets the verbosity level
115  if(!sverb.empty()){
116  verblevel = 1;
117  if(sverb != ".true."){
118  std::istringstream Istr(sverb);
119  Istr >> verblevel;
120  if(verblevel < 0)
121  verblevel = 1;
122  }
123  }
124 
125  // This block sets up the output file if the user specified one
126  std::ostringstream outStream;
127  if(!OutFileName.empty()){
128  if(Out)
129  Out = &outStream;
130  }
131 
132  if(verblevel > 1 && Out)
133  *Out << "PlasCom2::ParallelTest: Entering test function" << std::endl;
134 
135  // Make an instance of the PlasCom2 testing object, PlasCom2::ParallelTestingObject
137  // Make an instance of the PlasCom2 results object, PlasCom2::TestResults
138  plascom2::TestResults results;
139 
140  // If the user specified a name, then run only the named test
141  if(!TestName.empty()){
142  // This call runs a test by name
143  test.RunTest(TestName,results);
144  }
145  // Otherwise, if the user specified a list, then read the list and
146  // run the listed tests.
147  else if(!ListName.empty()){
148  std::ifstream ListInf;
149  ListInf.open(ListName.c_str());
150  if(!ListInf){
151  if(Out)
152  *Out << "PlasCom2::ParallelTest> Error: Could not open list of tests in file "
153  << ListName << ". Exiting (fail)." << std::endl;
154  communicator.SetExit(1);
155  }
156  if(communicator.Check())
157  return(3);
158  std::string testname;
159  while(std::getline(ListInf,testname))
160  test.RunTest(testname,results);
161  ListInf.close();
162  }
163  else {
164  // This call runs all the tests for the PlasCom2 namespace.
165  test.Process(results);
166  }
167  if(Out)
168  *Out << results << std::endl;
169 
170  // if(Out && Ouf)
171  // Ouf.close();
172 
173  if((verblevel > 1) && Out)
174  *Out << "PlasCom2::ParallelTest: Exiting test function (success)" << std::endl;
175 
176  if(!OutFileName.empty()){
177  if(Out){
178  Ouf.open(OutFileName.c_str(),std::ios::app);
179  if(!Ouf){
180  std::cout << "PlasCom2::ParallelTest> Error: Could not open output file, "
181  << OutFileName << " for test output. Exiting (fail)." << std::endl;
182  communicator.SetExit(1);
183  } else {
184  Ouf << outStream.str();
185  Ouf.close();
186  }
187  }
188  }
189  if(communicator.Check())
190  return(4);
191  communicator.Barrier();
192 
193  return(0);
194  }
195 };
196 
197 int main(int argc,char *argv[])
198 {
199  int provided;
200 
201  // Make the MPI runtime aware that we might want to run OpenMP threads,
202  // but will do MPI calls only from the master thread.
203  MPI_Init_thread(&argc, &argv, MPI_THREAD_FUNNELED, &provided);
204  assert(provided == MPI_THREAD_FUNNELED);
205 
206  // As HDF5 might run MPI functions as part of its shutdown, we can not call
207  // MPI_Finalize from main directly.
208  atexit((void (*)())MPI_Finalize);
209 
210  int returnCode = plascom2::ParallelTest(argc,argv);
211  return(returnCode);
212 }
int ProcessOptions()
Processes all command line tokens.
Definition: ComLine.C:330
std::string ErrorReport()
Error reporting.
Definition: ComLine.C:473
int ParallelTest(int argc, char *argv[])
Drives the PlasCom2::TestObject.
std::string GetOption(const char &s)
Get the value of an option.
Definition: ComLine.H:291
Communication utilities.
virtual void Process(ResultsType &result)
Runs all tests implemented by the PlasCom2::TestingObject.
Definition: PlasCom2Test.H:400
Definition: PC2IO.H:10
Project-specific parallel testing object.
Definition: PlasCom2Test.H:356
Encapsulating class for collections of test results.
Definition: Testing.H:18
int Check(comm::Ops op=comm::MAXOP)
Definition: COMM.C:355
Main encapsulation of MPI.
Definition: COMM.H:62
ComLineObject header.
void Initialize()
virtual function for program specific Initialization.
Definition: TestPlasCom2.H:25
ComLineObject for PlasCom2 testing command-line interface.
Definition: TestPlasCom2.H:16
int main(int argc, char *argv[])
Testing utilities for PlasCom2.
std::string LongUsage()
Generate long usage string.
Definition: ComLine.C:188
int SetExit(int errin)
Definition: COMM.H:109
virtual void RunTest(const std::string &name, ResultsType &result)
Runs a test specified by name.
Definition: PlasCom2Test.H:510
ix::comm::CommunicatorObject CommType
Convenience typedef for CommunicatorObject.
std::string ShortUsage()
Generate short usage string.
Definition: ComLine.C:244