21 #ifndef OPM_PARALLELFILEMERGER_HEADER_INCLUDED
22 #define OPM_PARALLELFILEMERGER_HEADER_INCLUDED
27 #include <boost/filesystem.hpp>
28 #include <boost/filesystem/fstream.hpp>
29 #include <boost/regex.hpp>
36 namespace fs = boost::filesystem;
51 const std::string& deckname)
52 : debugFileRegex_(
"\\."+deckname+
"\\.\\d+\\.DEBUG"),
53 logFileRegex_(deckname+
"\\.\\d+\\.PRT")
55 auto debugPath = output_dir;
56 debugPath /= (std::string(
".") + deckname +
".DEBUG");
57 debugStream_.reset(
new fs::ofstream(debugPath,
59 auto logPath = output_dir;
60 logPath /= ( deckname +
".PRT");
61 logStream_.reset(
new fs::ofstream(logPath,
65 void operator()(
const fs::path& file)
67 const static boost::regex regex(
".+\\.(\\d+)\\..+");
68 boost::smatch matches;
69 std::string filename = file.filename().native();
71 if ( boost::regex_match(filename, matches, regex) )
73 std::string rank = boost::regex_replace(filename, regex,
"\\1");
75 if( boost::regex_match(filename, logFileRegex_) )
77 appendFile(*logStream_, file, rank);
81 if (boost::regex_match(filename, debugFileRegex_) )
83 appendFile(*debugStream_, file, rank);
87 std::cerr <<
"WARNING: Unrecognized file with name "
89 <<
" that might stem from a parallel run."
100 void appendFile(fs::ofstream& of,
const fs::path& file,
const std::string& rank)
102 if( fs::file_size(file) )
104 std::cerr <<
"WARNING: There has been logging to file "
105 << file.string() <<
" by process "
106 << rank << std::endl;
108 fs::ifstream in(file);
109 of<<std::endl<< std::endl;
110 of<<
"=======================================================";
111 of<<std::endl<<std::endl;
112 of <<
" Output written by rank " << rank <<
" to file " << file.string();
113 of <<
":" << std::endl << std::endl;
114 of << in.rdbuf() << std::endl << std::endl;
115 of <<
"======================== end output =====================";
123 boost::regex debugFileRegex_;
125 boost::regex logFileRegex_;
127 std::unique_ptr<fs::ofstream> debugStream_;
129 std::unique_ptr<fs::ofstream> logStream_;
133 #endif // end header guard
ParallelFileMerger(const fs::path &output_dir, const std::string &deckname)
Constructor.
Definition: ParallelFileMerger.hpp:50
A functor that merges multiple files of a parallel run to one file.
Definition: ParallelFileMerger.hpp:44