8 #include <fei_sstream.hpp>
9 #include <fei_fstream.hpp>
11 #include <fei_test_utils.hpp>
13 #include <snl_fei_Utils.hpp>
14 #include <fei_VectorSpace.hpp>
15 #include <fei_MatrixGraph.hpp>
16 #include <fei_Matrix.hpp>
21 #define fei_file "fei_test_utils.cpp"
22 #include <fei_ErrMacros.hpp>
24 namespace fei_test_utils {
28 std::string path(
".");
32 int dashDarg = whichArg(argc, argv,
"-d");
34 int dashIarg = whichArg(argc, argv,
"-i");
37 fei::console_out() <<
"fei_test_utils::construct_filename: argument '-i' not found."
42 if (dashDarg > -1 && argc > dashDarg+1) {
43 if (argv[dashDarg+1] != 0) {
44 path = argv[dashDarg+1];
48 if (argc > dashIarg+1) {
49 if (argv[dashIarg+1] != 0) {
50 filename = argv[dashIarg+1];
54 FEI_OSTRINGSTREAM osstr;
57 std::size_t string_length = path.size();
58 if (path[string_length-1] !=
'/') osstr <<
"/";
62 return( osstr.str() );
68 if (MPI_Init(&argc, &argv) != MPI_SUCCESS) ERReturn(-1);
69 if (MPI_Comm_rank(MPI_COMM_WORLD, &localProc) != MPI_SUCCESS) ERReturn(-1);
70 if (MPI_Comm_size(MPI_COMM_WORLD, &numProcs) != MPI_SUCCESS) ERReturn(-1);
78 bool bool_arg(
const char* flag,
int argc,
char** argv,
81 int arg_index = whichArg(argc, argv, flag);
82 bool result = default_result;
84 if (argc > arg_index+1) {
85 if (argv[arg_index+1] != 0) {
86 std::string strarg(argv[arg_index+1]);
87 if (strarg ==
"yes" || strarg ==
"Yes" || strarg ==
"YES" ||
88 strarg ==
"true" || strarg ==
"True" || strarg ==
"TRUE") {
92 if (strarg ==
"no" || strarg ==
"No" || strarg ==
"NO" ||
93 strarg ==
"false" || strarg ==
"False" || strarg ==
"FALSE") {
106 int arg_index = whichArg(argc, argv, flag);
107 if (arg_index > -1) {
108 if (argc > arg_index+1) {
109 if (argv[arg_index+1] != 0) {
110 result = argv[arg_index+1];
123 int numprocs = 1, localproc = 0;
124 MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
125 MPI_Comm_rank(MPI_COMM_WORLD, &localproc);
130 if (localproc == root) {
131 int length = strg.size();
132 MPI_Bcast(&length, 1, MPI_INT, root, comm);
133 char* cstr =
const_cast<char*
>(strg.c_str());
134 MPI_Bcast(cstr, strg.size(), MPI_CHAR, root, comm);
138 MPI_Bcast(&length, 1, MPI_INT, root, comm);
139 char* charstr =
new char[length+1];
140 MPI_Bcast(charstr, length, MPI_CHAR, root, comm);
141 charstr[length] =
'\0';
151 std::vector<std::string>& file_contents)
153 FEI_IFSTREAM infile(filename);
155 FEI_OSTRINGSTREAM osstr;
156 osstr <<
"fei_test_utils::read_file_lines_into_strings ERROR, couldn't open file '"
158 throw std::runtime_error(osstr.str());
161 file_contents.clear();
165 getline(infile, line);
166 while(!infile.eof()) {
167 file_contents.push_back(line);
170 getline(infile, line);
175 MPI_Comm comm,
int localProc,
176 std::vector<std::string>& stdstrings)
178 std::string filename;
179 if (localProc == 0) {
186 catch(std::runtime_error& exc) {
196 std::vector<std::string>& file_contents)
201 MPI_Comm_rank(comm, &localProc);
202 MPI_Comm_size(comm, &numProcs);
205 if (localProc == 0) {
210 int num = file_contents.size();
212 MPI_Bcast(&num, 1, MPI_INT, 0, comm);
214 for(
int j=0; j<num; ++j) {
215 const char* cptr = file_contents[j].c_str();
216 int length = strlen(cptr) + 1;
217 MPI_Bcast(&length, 1, MPI_INT, 0, comm);
218 MPI_Bcast((
void*)cptr, length, MPI_CHAR, 0, comm);
226 MPI_Bcast(&num, 1, MPI_INT, 0, comm);
228 file_contents.resize(0);
230 for(
int j=0; j<num; ++j) {
232 MPI_Bcast(&length, 1, MPI_INT, 0, comm);
233 char* newstring =
new char[length];
234 MPI_Bcast(newstring, length, MPI_CHAR, 0, comm);
235 std::string strg(newstring);
236 file_contents.push_back(strg);
244 const char* testname)
246 std::vector<std::string> file_contents;
249 double file_benchmark = 0.0;
250 int err2 = snl_fei::getDoubleParamValue(testname,
254 throw std::runtime_error(
"fei_test_utils::get_file_benchmark failed to find named benchmark");
257 return(file_benchmark);
264 double maxval = std::abs(value1) > std::abs(value2) ? std::abs(value1) : std::abs(value2);
265 if (maxval < 1.e-14) {
270 double difference = std::abs(value2 - value1);
271 double lhs = 100.0*(difference/maxval);
272 double rhs = 1.0*margin;
273 return( lhs <= rhs );
276 int whichArg(
int argc,
const char*
const* argv,
const char* findarg)
278 for(
int i=0; i<argc; i++) {
279 if (argv[i] != NULL) {
280 if (strcmp(findarg, argv[i]) == 0)
return(i);
286 bool check_and_cout_test_result(std::string testname,
292 FEI_COUT << testname <<
" " << value <<
" ";
296 FEI_COUT <<
"within "<<margin<<
"% of file value " << file_value <<
". ";
299 FEI_COUT <<
"passed."<<FEI_ENDL;
302 FEI_COUT <<
"FAILED."<<FEI_ENDL;
307 std::string check_test_result(
double value,
312 return( result ?
"passed" :
"FAILED");
315 int compare_with_file_benchmark(
const char* name,
317 const char* filename)
319 if (name == NULL)
return(-1);
322 #if defined(FEI_PLATFORM) && defined(FEI_OPT_LEVEL)
324 FEI_OSTRINGSTREAM testname;
325 testname << name<<
"_"<<FEI_PLATFORM<<
"_"<<FEI_OPT_LEVEL;
327 double file_benchmark = 0.0;
328 bool file_benchmark_available =
true;
332 catch (std::runtime_error& exc) {
333 file_benchmark_available =
false;
336 if (file_benchmark_available) {
337 bool test_passed = check_and_cout_test_result(testname.str(),
341 returnValue = test_passed ? 0 : 1;
342 if (returnValue != 0) {
348 FEI_COUT <<
"(compile with -DFEI_PLATFORM=<platform> -DFEI_OPT_LEVEL=<opt|dbg>)"<<FEI_ENDL;
354 int dirname(
const char* name,
char*& dir)
360 int i, len = strlen(name);
362 for(i=0; i<len; ++i) {
380 if (i==0) dir[i] =
'.';
385 void print_args(
int argc,
char** argv){
386 FEI_COUT <<
"argc: " << argc << FEI_ENDL;
388 for(
int i=0; i<argc; i++){
389 if (argv[i] != NULL) {
390 FEI_COUT <<
"argv["<<i<<
"]: " << argv[i] << FEI_ENDL;
393 FEI_COUT << FEI_ENDL;
396 int compareMatrices(fei::FillableMat& mat1, fei::FillableMat& mat2,
double tol)
398 if (mat1 == mat2)
return 0;
399 FEI_COUT <<
"compareMatrices returned not-equal, tol=="<<tol << FEI_ENDL;
403 int readMatrix(
const char* baseName,
int np, fei::FillableMat& matrix)
405 for(
int i=0; i<np; i++) {
406 FEI_OSTRINGSTREAM fileName;
407 fileName <<baseName<<
"."<<np<<
"."<<i;
408 FEI_IFSTREAM infile(fileName.str().c_str());
410 infile.setf(IOS_SCIENTIFIC, IOS_FLOATFIELD);
412 int row, col, tmp, numRows, numCols;
419 while(!infile.eof()) {
423 matrix.putCoef(row, col, value);
432 int readMatrix(
const char* fileName, fei::FillableMat& matrix)
436 FEI_IFSTREAM infile(fileName);
438 FEI_COUT <<
"ERROR opening file " << fileName << FEI_ENDL;
442 infile.setf(IOS_SCIENTIFIC, IOS_FLOATFIELD);
444 int row, col, tmp, numRows, numCols;
451 while(!infile.eof()) {
455 matrix.putCoef(row, col, value);
463 int writeMatrix(
const char* fileName, fei::FillableMat& matrix)
465 FEI_OFSTREAM outfile(fileName);
467 FEI_COUT <<
"ERROR opening file " << fileName << FEI_ENDL;
471 outfile.setf(IOS_SCIENTIFIC, IOS_FLOATFIELD);
478 int copy_feiMatrix_to_FillableMat(
fei::Matrix& feimat, fei::FillableMat& fmat)
488 std::vector<int> globalOffsets;
490 int numLocalRows = globalOffsets[localProc+1]-globalOffsets[localProc];
491 int firstLocalRow = globalOffsets[localProc];
493 for(
int i=0; i<numLocalRows; ++i) {
494 int row = firstLocalRow+i;
498 std::vector<int> colindices(rowLen);
499 std::vector<double> coefs(rowLen);
501 feimat.
copyOutRow(row, rowLen, &coefs[0], &colindices[0]);
503 fmat.putRow(row, &colindices[0], &coefs[0], rowLen);
MPI_Comm getCommunicator() const
std::string construct_filename(int argc, char **argv)
void read_file_lines_into_strings(const char *filename, std::vector< std::string > &file_contents)
bool bool_arg(const char *flag, int argc, char **argv, bool default_result)
virtual fei::SharedPtr< fei::VectorSpace > getRowSpace()=0
double get_file_benchmark(const char *filename, const char *testname)
int get_filename_and_read_input(int argc, char **argv, MPI_Comm comm, int localProc, std::vector< std::string > &stdstrings)
virtual int getRowLength(int row, int &length) const =0
int initialize_mpi(int argc, char **argv, int &localProc, int &numProcs)
virtual fei::SharedPtr< fei::MatrixGraph > getMatrixGraph() const =0
void read_input_file(const char *filename, MPI_Comm comm, std::vector< std::string > &file_contents)
void broadcast_string(MPI_Comm comm, int root, std::string &strg)
std::ostream & console_out()
void getGlobalIndexOffsets(std::vector< int > &globalOffsets) const
bool within_percentage_margin(double value1, double value2, unsigned margin)
int localProc(MPI_Comm comm)
std::string get_arg_value(const char *flag, int argc, char **argv)
virtual int copyOutRow(int row, int len, double *coefs, int *indices) const =0