21 #define fei_file "fei_test_utils.cpp"
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];
57 std::size_t string_length = path.size();
58 if (path[string_length-1] !=
'/') osstr <<
"/";
62 return( osstr.str() );
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;
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)
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);
176 std::vector<std::string>& stdstrings)
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;
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);
292 FEI_COUT << testname <<
" " << value <<
" ";
296 FEI_COUT <<
"within "<<margin<<
"% of file value " << file_value <<
". ";
312 return( result ?
"passed" :
"FAILED");
317 const char* filename)
319 if (name == NULL)
return(-1);
322 #if defined(FEI_PLATFORM) && defined(FEI_OPT_LEVEL)
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) {
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;
360 int i, len = strlen(name);
362 for(i=0; i<len; ++i) {
380 if (i==0) dir[i] =
'.';
388 for(
int i=0; i<argc; i++){
389 if (argv[i] != NULL) {
398 if (mat1 == mat2)
return 0;
405 for(
int i=0; i<np; i++) {
407 fileName <<baseName<<
"."<<np<<
"."<<i;
412 int row, col, tmp, numRows, numCols;
419 while(!infile.eof()) {
423 matrix.
putCoef(row, col, value);
444 int row, col, tmp, numRows, numCols;
451 while(!infile.eof()) {
455 matrix.
putCoef(row, col, value);
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
int readMatrix(const char *baseName, int np, fei::FillableMat &matrix)
int writeMatrix(const char *fileName, fei::FillableMat &matrix)
std::string construct_filename(int argc, char **argv)
std::string check_test_result(double value, double goldvalue, unsigned margin)
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)
int whichArg(int argc, const char *const *argv, const char *findarg)
int getDoubleParamValue(const char *key, int numParams, const char *const *params, double ¶mValue)
virtual fei::SharedPtr< fei::VectorSpace > getRowSpace()=0
int compareMatrices(fei::FillableMat &mat1, fei::FillableMat &mat2, double tol)
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)
void putCoef(int row, int col, double coef)
int copy_feiMatrix_to_FillableMat(fei::Matrix &feimat, fei::FillableMat &fmat)
virtual int getRowLength(int row, int &length) const =0
void putRow(int row, const int *cols, const double *coefs, unsigned len)
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)
void print_args(int argc, char **argv)
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)
int compare_with_file_benchmark(const char *name, double benchmark, const char *filename)
std::string get_arg_value(const char *flag, int argc, char **argv)
int dirname(const char *name, char *&dir)
#define FEI_OSTRINGSTREAM
virtual int copyOutRow(int row, int len, double *coefs, int *indices) const =0
int numProcs(MPI_Comm comm)
bool check_and_cout_test_result(std::string testname, double value, double file_value, unsigned margin)