00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef OPM_PARALLELFILEMERGER_HEADER_INCLUDED
00022 #define OPM_PARALLELFILEMERGER_HEADER_INCLUDED
00023
00024 #include <memory>
00025 #include <iostream>
00026
00027 #include <boost/filesystem.hpp>
00028 #include <boost/filesystem/fstream.hpp>
00029 #include <boost/regex.hpp>
00030
00031 namespace Opm
00032 {
00033 namespace detail
00034 {
00035
00036 namespace fs = boost::filesystem;
00037
00044 class ParallelFileMerger
00045 {
00046 public:
00050 ParallelFileMerger(const fs::path& output_dir,
00051 const std::string& deckname)
00052 : debugFileRegex_("\\."+deckname+"\\.\\d+\\.DEBUG"),
00053 logFileRegex_(deckname+"\\.\\d+\\.PRT")
00054 {
00055 auto debugPath = output_dir;
00056 debugPath /= (std::string(".") + deckname + ".DEBUG");
00057 debugStream_.reset(new fs::ofstream(debugPath,
00058 std::ofstream::app));
00059 auto logPath = output_dir;
00060 logPath /= ( deckname + ".PRT");
00061 logStream_.reset(new fs::ofstream(logPath,
00062 std::ofstream::app));
00063 }
00064
00065 void operator()(const fs::path& file)
00066 {
00067 const static boost::regex regex(".+\\.(\\d+)\\..+");
00068 boost::smatch matches;
00069 std::string filename = file.filename().native();
00070
00071 if ( boost::regex_match(filename, matches, regex) )
00072 {
00073 std::string rank = boost::regex_replace(filename, regex, "\\1");
00074
00075 if( boost::regex_match(filename, logFileRegex_) )
00076 {
00077 appendFile(*logStream_, file, rank);
00078 }
00079 else
00080 {
00081 if (boost::regex_match(filename, debugFileRegex_) )
00082 {
00083 appendFile(*debugStream_, file, rank);
00084 }
00085 else
00086 {
00087 std::cerr << "WARNING: Unrecognized file with name "
00088 << filename
00089 << " that might stem from a parallel run."
00090 << std::endl;
00091 }
00092 }
00093 }
00094 }
00095 private:
00100 void appendFile(fs::ofstream& of, const fs::path& file, const std::string& rank)
00101 {
00102 if( fs::file_size(file) )
00103 {
00104 std::cerr << "WARNING: There has been logging to file "
00105 << file.string() <<" by process "
00106 << rank << std::endl;
00107
00108 fs::ifstream in(file);
00109 of<<std::endl<< std::endl;
00110 of<<"=======================================================";
00111 of<<std::endl<<std::endl;
00112 of << " Output written by rank " << rank << " to file " << file.string();
00113 of << ":" << std::endl << std::endl;
00114 of << in.rdbuf() << std::endl << std::endl;
00115 of << "======================== end output =====================";
00116 of << std::endl;
00117 in.close();
00118 }
00119 fs::remove(file);
00120 }
00121
00123 boost::regex debugFileRegex_;
00125 boost::regex logFileRegex_;
00127 std::unique_ptr<fs::ofstream> debugStream_;
00129 std::unique_ptr<fs::ofstream> logStream_;
00130 };
00131 }
00132 }
00133 #endif // end header guard