libROM  v1.0
Data-driven physical simulation library
Utilities.cpp
1 
11 // Description: Utility functions for error reporting, etc.
12 
13 #include "Utilities.h"
14 #include "ParallelBuffer.h"
15 
16 #include "mpi.h"
17 
18 #include <iomanip>
19 #include <stdlib.h>
20 #include <sys/stat.h>
21 
22 namespace CAROM {
23 
24 void
26  const std::string& message,
27  const std::string& filename,
28  int line)
29 {
30  ParallelBuffer perr_buffer;
31  std::ostream perr(&perr_buffer);
32  perr << "Program abort called in file ``" << filename
33  << "'' at line " << line << std::endl;
34  perr << "ERROR MESSAGE: " << std::endl << message.c_str() << std::endl;
35  perr << std::flush;
36 
37  int mpi_init;
38  MPI_Initialized(&mpi_init);
39  if (mpi_init) {
40  int size;
41  MPI_Comm_size(MPI_COMM_WORLD, &size);
42  if (size > 1) {
43  MPI_Abort(MPI_COMM_WORLD, -1);
44  }
45  else {
46  exit(-1);
47  }
48  }
49  else {
50  exit(-1);
51  }
52 }
53 
54 std::string
56  int processorID)
57 {
58  std::ostringstream os;
59  if (processorID < 0) {
60  os << '-' << std::setw(6) << std::setfill('0') << -processorID;
61  } else {
62  os << std::setw(7) << std::setfill('0') << processorID;
63  }
64  os << std::flush;
65 
66  return os.str();
67 }
68 
69 bool
71  const std::string& filename)
72 {
73  struct stat buffer;
74  return (stat(filename.c_str(), &buffer) == 0);
75 }
76 
77 }
static std::string processorToString(int processorID)
Converts a processor ID to a string. Use this to ensure same width is used when converting a processo...
Definition: Utilities.cpp:55
static bool file_exist(const std::string &filename)
Verifies if a file exists.
Definition: Utilities.cpp:70
static void abort(const std::string &message, const std::string &filename, int line)
Cleanly ends the program when something horrible happened and prints a message about what took place....
Definition: Utilities.cpp:25