Moved files to opm/simulators/ subdirs.

This commit is contained in:
Atgeirr Flø Rasmussen
2019-05-07 13:06:02 +02:00
parent 589db89b09
commit c17adf788f
69 changed files with 153 additions and 153 deletions

View File

@@ -0,0 +1,97 @@
/*
Copyright 2018 SINTEF Digital, Mathematics and Cybernetics.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <opm/simulators/utils/DeferredLogger.hpp>
namespace Opm
{
void DeferredLogger::info(const std::string& tag, const std::string& message)
{
messages_.push_back({Log::MessageType::Info, tag, message});
}
void DeferredLogger::warning(const std::string& tag, const std::string& message)
{
messages_.push_back({Log::MessageType::Warning, tag, message});
}
void DeferredLogger::error(const std::string& tag, const std::string& message)
{
messages_.push_back({Log::MessageType::Error, tag, message});
}
void DeferredLogger::problem(const std::string& tag, const std::string& message)
{
messages_.push_back({Log::MessageType::Problem, tag, message});
}
void DeferredLogger::bug(const std::string& tag, const std::string& message)
{
messages_.push_back({Log::MessageType::Bug, tag, message});
}
void DeferredLogger::debug(const std::string& tag, const std::string& message)
{
messages_.push_back({Log::MessageType::Debug, tag, message});
}
void DeferredLogger::note(const std::string& tag, const std::string& message)
{
messages_.push_back({Log::MessageType::Note, tag, message});
}
void DeferredLogger::info(const std::string& message)
{
messages_.push_back({Log::MessageType::Info, "", message});
}
void DeferredLogger::warning(const std::string& message)
{
messages_.push_back({Log::MessageType::Warning, "", message});
}
void DeferredLogger::error(const std::string& message)
{
messages_.push_back({Log::MessageType::Error, "", message});
}
void DeferredLogger::problem(const std::string& message)
{
messages_.push_back({Log::MessageType::Problem, "", message});
}
void DeferredLogger::bug(const std::string& message)
{
messages_.push_back({Log::MessageType::Bug, "", message});
}
void DeferredLogger::debug(const std::string& message)
{
messages_.push_back({Log::MessageType::Debug, "", message});
}
void DeferredLogger::note(const std::string& message)
{
messages_.push_back({Log::MessageType::Note, "", message});
}
void DeferredLogger::logMessages()
{
for (const auto& m : messages_) {
OpmLog::addTaggedMessage(m.flag, m.tag, m.text);
}
messages_.clear();
}
void DeferredLogger::clearMessages()
{
messages_.clear();
}
} // namespace Opm

View File

@@ -0,0 +1,77 @@
/*
Copyright 2018 SINTEF Digital, Mathematics and Cybernetics.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_DEFERREDLOGGER_HEADER_INCLUDED
#define OPM_DEFERREDLOGGER_HEADER_INCLUDED
#include <opm/common/OpmLog/OpmLog.hpp>
#include <string>
#include <vector>
namespace Opm
{
/** This class implements a deferred logger:
* 1) messages can be pushed back to a vector
* 2) a call to logMessages adds the messages to OpmLog backends
* */
class DeferredLogger
{
public:
struct Message
{
int64_t flag;
std::string tag;
std::string text;
};
void info(const std::string& tag, const std::string& message);
void warning(const std::string& tag, const std::string& message);
void error(const std::string& tag, const std::string& message);
void problem(const std::string& tag, const std::string& message);
void bug(const std::string& tag, const std::string& message);
void debug(const std::string& tag, const std::string& message);
void note(const std::string& tag, const std::string& message);
void info(const std::string& message);
void warning(const std::string& message);
void error(const std::string& message);
void problem(const std::string& message);
void bug(const std::string& message);
void debug(const std::string& message);
void note(const std::string& message);
/// Log all messages to the OpmLog backends,
/// and clear the message container.
void logMessages();
/// Clear the message container without logging them.
void clearMessages();
private:
std::vector<Message> messages_;
friend Opm::DeferredLogger gatherDeferredLogger(const Opm::DeferredLogger& local_deferredlogger);
};
} // namespace Opm
#endif // OPM_DEFERREDLOGGER_HEADER_INCLUDED

View File

@@ -0,0 +1,76 @@
/*
Copyright 2019 SINTEF Digital, Mathematics and Cybernetics.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_DEFERREDLOGGINGERRORHELPERS_HPP
#define OPM_DEFERREDLOGGINGERRORHELPERS_HPP
#include <opm/simulators/utils/DeferredLogger.hpp>
#include <dune/common/version.hh>
#include <dune/common/parallel/mpihelper.hh>
#include <string>
#include <sstream>
#include <exception>
#include <stdexcept>
// Macro to throw an exception.
// Inspired by ErrorMacros.hpp in opm-common.
// NOTE: For this macro to work, the
// exception class must exhibit a constructor with the signature
// (const std::string &message). Since this condition is not fulfilled
// for the std::exception, you should use this macro with some
// exception class derived from either std::logic_error or
// std::runtime_error.
//
// Usage: OPM_DEFLOG_THROW(ExceptionClass, "Error message " << value, DeferredLogger);
#define OPM_DEFLOG_THROW(Exception, message, deferred_logger) \
do { \
std::ostringstream oss__; \
oss__ << "[" << __FILE__ << ":" << __LINE__ << "] " << message; \
deferred_logger.error(oss__.str()); \
throw Exception(oss__.str()); \
} while (false)
inline void checkForExceptionsAndThrow(int exception_thrown, const std::string& message)
{
const auto& cc = Dune::MPIHelper::getCollectiveCommunication();
if (cc.max(exception_thrown) == 1) {
throw std::logic_error(message);
}
}
inline void logAndCheckForExceptionsAndThrow(Opm::DeferredLogger& deferred_logger, int exception_thrown, const std::string& message, const bool terminal_output)
{
Opm::DeferredLogger global_deferredLogger = gatherDeferredLogger(deferred_logger);
if (terminal_output) {
global_deferredLogger.logMessages();
}
// Now that all messages have been logged, they are automatically
// cleared from the global logger, but we must also clear them
// from the local logger.
deferred_logger.clearMessages();
const auto& cc = Dune::MPIHelper::getCollectiveCommunication();
if (cc.max(exception_thrown) == 1) {
throw std::logic_error(message);
}
}
#endif // OPM_DEFERREDLOGGINGERRORHELPERS_HPP

View File

@@ -0,0 +1,152 @@
/*
Copyright 2016 Dr. Blatt - HPC-Simulation-Software & Services
Copyright 2016 STATOIL AS.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_PARALLELFILEMERGER_HEADER_INCLUDED
#define OPM_PARALLELFILEMERGER_HEADER_INCLUDED
#include <memory>
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/regex.hpp>
namespace Opm
{
namespace detail
{
namespace fs = boost::filesystem;
/// \brief A functor that merges multiple files of a parallel run to one file.
///
/// Without care multiple processes might log messages in a parallel run.
/// Non-root processes will do that to seperate files
/// <basename>.<rank>.<extension. This functor will append those file
/// to usual ones and delete the other files.
class ParallelFileMerger
{
public:
/// \brief Constructor
/// \param output_dir The output directory to use for reading/Writing.
/// \param deckanme The name of the deck.
ParallelFileMerger(const fs::path& output_dir,
const std::string& deckname,
bool show_fallout = false)
: debugFileRegex_(deckname+"\\.\\d+\\.DBG"),
logFileRegex_(deckname+"\\.\\d+\\.PRT"),
fileWarningRegex_(deckname+"\\.(\\d+)\\.[^.]+"),
show_fallout_(show_fallout)
{
if ( show_fallout_ )
{
auto debugPath = output_dir;
debugPath /= (deckname + ".DBG");
debugStream_.reset(new fs::ofstream(debugPath,
std::ofstream::app));
auto logPath = output_dir;
logPath /= ( deckname + ".PRT");
logStream_.reset(new fs::ofstream(logPath,
std::ofstream::app));
}
}
void operator()(const fs::path& file)
{
boost::smatch matches;
std::string filename = file.filename().native();
if ( boost::regex_match(filename, matches, fileWarningRegex_) )
{
std::string rank = boost::regex_replace(filename, fileWarningRegex_, "\\1");
if( boost::regex_match(filename, logFileRegex_) )
{
if ( show_fallout_ ){
appendFile(*logStream_, file, rank);
}else{
fs::remove(file);
}
}
else
{
if (boost::regex_match(filename, debugFileRegex_) )
{
if ( show_fallout_ ){
appendFile(*debugStream_, file, rank);
}else{
fs::remove(file);
}
}
else
{
if ( show_fallout_ ){
std::cerr << "WARNING: Unrecognized file with name "
<< filename
<< " that might stem from a parallel run."
<< std::endl;
}
}
}
}
}
private:
/// \brief Append contents of a file to a stream
/// \brief of The output stream to use.
/// \brief file The file whose content to append.
/// \brief rank The rank that wrote the file.
void appendFile(fs::ofstream& of, const fs::path& file, const std::string& rank)
{
if( fs::file_size(file) )
{
std::cerr << "WARNING: There has been logging to file "
<< file.string() <<" by process "
<< rank << std::endl;
fs::ifstream in(file);
of<<std::endl<< std::endl;
of<<"=======================================================";
of<<std::endl<<std::endl;
of << " Output written by rank " << rank << " to file " << file.string();
of << ":" << std::endl << std::endl;
of << in.rdbuf() << std::endl << std::endl;
of << "======================== end output =====================";
of << std::endl;
in.close();
}
fs::remove(file);
}
/// \brief Regex to capture *.DBG
boost::regex debugFileRegex_;
/// \brief Regex to capture *.PRT
boost::regex logFileRegex_;
/// \brief Regex to capture CASENAME.[0-9]+.[A-Z]+
boost::regex fileWarningRegex_;
/// \brief Stream to *.DBG file
std::unique_ptr<fs::ofstream> debugStream_;
/// \brief Stream to *.PRT file
std::unique_ptr<fs::ofstream> logStream_;
/// \brief Whether to show any logging fallout
bool show_fallout_;
};
} // end namespace detail
} // end namespace OPM
#endif // end header guard

View File

@@ -0,0 +1,173 @@
/*
Copyright 2019 SINTEF Digital, Mathematics and Cybernetics.
Copyright 2019 Equinor.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <opm/simulators/utils/gatherDeferredLogger.hpp>
#if HAVE_MPI
#include <cassert>
#include <cstdint>
#include <numeric>
#include <mpi.h>
namespace
{
void packMessages(const std::vector<Opm::DeferredLogger::Message>& local_messages, std::vector<char>& buf, int& offset)
{
int messagesize = local_messages.size();
MPI_Pack(&messagesize, 1, MPI_UNSIGNED, buf.data(), buf.size(), &offset, MPI_COMM_WORLD);
for (const auto lm : local_messages) {
MPI_Pack(static_cast<void*>(const_cast<std::int64_t*>(&lm.flag)), 1, MPI_INT64_T, buf.data(), buf.size(), &offset, MPI_COMM_WORLD);
int tagsize = lm.tag.size();
MPI_Pack(&tagsize, 1, MPI_UNSIGNED, buf.data(), buf.size(), &offset, MPI_COMM_WORLD);
if (tagsize>0) {
MPI_Pack(const_cast<char*>(lm.tag.c_str()), lm.tag.size(), MPI_CHAR, buf.data(), buf.size(), &offset, MPI_COMM_WORLD);
}
int textsize = lm.text.size();
MPI_Pack(&textsize, 1, MPI_UNSIGNED, buf.data(), buf.size(), &offset, MPI_COMM_WORLD);
if (textsize>0) {
MPI_Pack(const_cast<char*>(lm.text.c_str()), lm.text.size(), MPI_CHAR, buf.data(), buf.size(), &offset, MPI_COMM_WORLD);
}
}
}
Opm::DeferredLogger::Message unpackSingleMessage(const std::vector<char>& recv_buffer, int& offset)
{
int64_t flag;
auto* data = const_cast<char*>(recv_buffer.data());
MPI_Unpack(data, recv_buffer.size(), &offset, &flag, 1, MPI_INT64_T, MPI_COMM_WORLD);
// unpack tag
unsigned int tagsize;
MPI_Unpack(data, recv_buffer.size(), &offset, &tagsize, 1, MPI_UNSIGNED, MPI_COMM_WORLD);
std::string tag;
if (tagsize>0) {
std::vector<char> tagchars(tagsize);
MPI_Unpack(data, recv_buffer.size(), &offset, tagchars.data(), tagsize, MPI_CHAR, MPI_COMM_WORLD);
tag = std::string(tagchars.data(), tagsize);
}
// unpack text
unsigned int textsize;
MPI_Unpack(data, recv_buffer.size(), &offset, &textsize, 1, MPI_UNSIGNED, MPI_COMM_WORLD);
std::string text;
if (textsize>0) {
std::vector<char> textchars(textsize);
MPI_Unpack(data, recv_buffer.size(), &offset, textchars.data(), textsize, MPI_CHAR, MPI_COMM_WORLD);
text = std::string (textchars.data(), textsize);
}
return Opm::DeferredLogger::Message({flag, tag, text});
}
std::vector<Opm::DeferredLogger::Message> unpackMessages(const std::vector<char>& recv_buffer, const std::vector<int>& displ)
{
std::vector<Opm::DeferredLogger::Message> messages;
const int num_processes = displ.size() - 1;
auto* data = const_cast<char*>(recv_buffer.data());
for (int process = 0; process < num_processes; ++process) {
int offset = displ[process];
// unpack number of messages
unsigned int messagesize;
MPI_Unpack(data, recv_buffer.size(), &offset, &messagesize, 1, MPI_UNSIGNED, MPI_COMM_WORLD);
for (unsigned int i=0; i<messagesize; i++) {
messages.push_back(unpackSingleMessage(recv_buffer, offset));
}
assert(offset == displ[process + 1]);
}
return messages;
}
} // anonymous namespace
namespace Opm
{
/// combine (per-process) messages
Opm::DeferredLogger gatherDeferredLogger(const Opm::DeferredLogger& local_deferredlogger)
{
int num_messages = local_deferredlogger.messages_.size();
int int64_mpi_pack_size;
MPI_Pack_size(1, MPI_INT64_T, MPI_COMM_WORLD, &int64_mpi_pack_size);
int unsigned_int_mpi_pack_size;
MPI_Pack_size(1, MPI_UNSIGNED, MPI_COMM_WORLD, &unsigned_int_mpi_pack_size);
// store number of messages;
int message_size = unsigned_int_mpi_pack_size;
// store 1 int64 per message for flag
message_size += num_messages*int64_mpi_pack_size;
// store 2 unsigned ints per message for length of tag and length of text
message_size += num_messages*2*unsigned_int_mpi_pack_size;
for (const auto lm : local_deferredlogger.messages_) {
int string_mpi_pack_size;
MPI_Pack_size(lm.tag.size(), MPI_CHAR, MPI_COMM_WORLD, &string_mpi_pack_size);
message_size += string_mpi_pack_size;
MPI_Pack_size(lm.text.size(), MPI_CHAR, MPI_COMM_WORLD, &string_mpi_pack_size);
message_size += string_mpi_pack_size;
}
// Pack local messages.
std::vector<char> buffer(message_size);
int offset = 0;
packMessages(local_deferredlogger.messages_, buffer, offset);
assert(offset == message_size);
// Get message sizes and create offset/displacement array for gathering.
int num_processes = -1;
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
std::vector<int> message_sizes(num_processes);
MPI_Allgather(&message_size, 1, MPI_INT, message_sizes.data(), 1, MPI_INT, MPI_COMM_WORLD);
std::vector<int> displ(num_processes + 1, 0);
std::partial_sum(message_sizes.begin(), message_sizes.end(), displ.begin() + 1);
// Gather.
std::vector<char> recv_buffer(displ.back());
MPI_Allgatherv(buffer.data(), buffer.size(), MPI_PACKED,
const_cast<char*>(recv_buffer.data()), message_sizes.data(),
displ.data(), MPI_PACKED,
MPI_COMM_WORLD);
// Unpack.
Opm::DeferredLogger global_deferredlogger;
global_deferredlogger.messages_ = unpackMessages(recv_buffer, displ);
return global_deferredlogger;
}
} // namespace Opm
#else // HAVE_MPI
namespace Opm
{
Opm::DeferredLogger gatherDeferredLogger(const Opm::DeferredLogger& local_deferredlogger)
{
return local_deferredlogger;
}
} // namespace Opm
#endif // HAVE_MPI

View File

@@ -0,0 +1,35 @@
/*
Copyright 2019 SINTEF Digital, Mathematics and Cybernetics.
Copyright 2019 Equinor.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_GATHERDEFERREDLOGGER_HEADER_INCLUDED
#define OPM_GATHERDEFERREDLOGGER_HEADER_INCLUDED
#include <opm/simulators/utils/DeferredLogger.hpp>
namespace Opm
{
/// Create a global log combining local logs
Opm::DeferredLogger gatherDeferredLogger(const Opm::DeferredLogger& local_deferredlogger);
} // namespace Opm
#endif // OPM_GATHERDEFERREDLOGGER_HEADER_INCLUDED

View File

@@ -0,0 +1,49 @@
/*
Copyright 2015 SINTEF ICT, Applied Mathematics.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <opm/simulators/utils/moduleVersion.hpp>
#include "project-version.h"
namespace Opm
{
/// Return the version name of the module, for example "2015.10"
/// (for a release branch) or "2016.04-pre" (for a master branch).
std::string moduleVersionName()
{
return PROJECT_VERSION_NAME;
}
/// Return a (short) git hash for the current version of the
/// module if this is a Release build (as defined by CMake), or
/// "debug" for Debug builds.
std::string moduleVersionHash()
{
return PROJECT_VERSION_HASH;
}
/// Return a string containing both the name and hash, if N is the
/// name and H is the hash it will be "N (H)". For example
/// "2016.04-pre (f15be17)" or "2016.04-pre (debug)".
std::string moduleVersion()
{
return PROJECT_VERSION;
}
} // namespace Opm

View File

@@ -0,0 +1,44 @@
/*
Copyright 2015 SINTEF ICT, Applied Mathematics.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_MODULEVERSION_HEADER_INCLUDED
#define OPM_MODULEVERSION_HEADER_INCLUDED
#include <string>
namespace Opm
{
/// Return the version name of the module, for example "2015.10"
/// (for a release branch) or "2016.04-pre" (for a master branch).
std::string moduleVersionName();
/// Return a (short) git hash for the current version of the
/// module if this is a Release build (as defined by CMake), or
/// "debug" for Debug builds.
std::string moduleVersionHash();
/// Return a string containing both the name and hash, if N is the
/// name and H is the hash it will be "N (H)". For example
/// "2016.04-pre (f15be17)" or "2016.04-pre (debug)".
std::string moduleVersion();
} // namespace Opm
#endif // OPM_MODULEVERSION_HEADER_INCLUDED