Skip to content
Snippets Groups Projects
Commit 89775bbb authored by Dmitri Naumov's avatar Dmitri Naumov
Browse files

Merge pull request #510 from norihiro-w/log-MPI

Logog target for MPI computation
parents 7f75bd93 916832b9
No related branches found
No related tags found
No related merge requests found
/**
* \copyright
* Copyright (c) 2012-2014, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#ifndef LOGOGCUSTOMCOUT_H_
#define LOGOGCUSTOMCOUT_H_
#include "logog/include/logog.hpp"
#ifdef USE_MPI
#include <mpi.h>
#endif
namespace BaseLib
{
/// Custom target for logog output
class LogogCustomCout : public logog::Target
{
public:
#ifdef USE_MPI
/**
* Constructor when MPI is involved
*
* @param all_rank_output_level Minimum level to output messages from all MPI processes
* @param mpi_comm MPI communicator
*/
LogogCustomCout(LOGOG_LEVEL_TYPE all_rank_output_level = LOGOG_LEVEL_INFO, MPI_Comm mpi_comm = MPI_COMM_WORLD)
: _all_rank_output_level(all_rank_output_level), _is_rank0 (getRank(mpi_comm)==0)
{}
#endif
virtual int Receive( const logog::Topic &topic )
{
#ifdef USE_MPI
if (topic.Level() > _all_rank_output_level && !_is_rank0)
return 0;
#endif
return logog::Target::Receive(topic);
}
virtual int Output( const LOGOG_STRING &data )
{
LOGOG_COUT << (const LOGOG_CHAR *)data;
return 0;
}
private:
#ifdef USE_MPI
int getRank(MPI_Comm mpi_comm) const
{
int rank = 0;
MPI_Comm_rank(mpi_comm, &rank);
return rank;
}
const LOGOG_LEVEL_TYPE _all_rank_output_level;
const bool _is_rank0;
#endif
};
#endif // LOGOGCUSTOMCOUT_H_
} // namespace BaseLib
......@@ -12,13 +12,15 @@
*
*/
#include "StringTools.h"
#include <algorithm>
#include <cctype>
#include <iomanip>
// ThirdParty/logog
#include "logog/include/logog.hpp"
#include "StringTools.h"
namespace BaseLib
{
......@@ -75,6 +77,14 @@ std::string stringToUpper(std::string const& str)
std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) std::toupper);
return s;
}
std::string padLeft(std::string const& str, int maxlen, char ch)
{
std::stringstream ss(str);
ss << std::right << std::setw(maxlen) << std::setfill(ch) << str;
return ss.str();
}
} // end namespace BaseLib
#ifdef MSVC
......
......@@ -74,6 +74,11 @@ void simplify(std::string &str);
*/
std::string stringToUpper(std::string const& str);
/**
* Returns the string which is right aligned with padding on the left.
*/
std::string padLeft(std::string const& str, int maxlen, char ch=' ');
} // end namespace BaseLib
#ifdef MSVC
......
/**
* \copyright
* Copyright (c) 2012-2014, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
namespace BaseLib {
#ifdef USE_MPI
template <int T_SUPPPRESS_TOPIC_FLAG>
TemplateLogogFormatterSuppressedGCC<T_SUPPPRESS_TOPIC_FLAG>
::TemplateLogogFormatterSuppressedGCC(MPI_Comm mpi_comm)
{
int rank = 0;
MPI_Comm_rank(mpi_comm, &rank);
int size = 0;
MPI_Comm_size(mpi_comm, &size);
int digits = size/10 + 1;
_str_mpi_rank = "rank " + padLeft(std::to_string(rank), digits) + "| ";
}
#endif
template <int T_SUPPPRESS_TOPIC_FLAG>
LOGOG_STRING &
TemplateLogogFormatterSuppressedGCC<T_SUPPPRESS_TOPIC_FLAG>
::Format( const logog::Topic &topic, const logog::Target &target )
{
TOPIC_FLAGS flags;
flags = GetTopicFlags( topic );
m_sMessageBuffer.clear();
#ifdef USE_MPI
m_sMessageBuffer.append(_str_mpi_rank.c_str());
#endif
if ( flags & TOPIC_FILE_NAME_FLAG )
{
m_sMessageBuffer.append( topic.FileName() );
m_sMessageBuffer.append( ':' );
}
if ( flags & TOPIC_LINE_NUMBER_FLAG )
{
m_sIntBuffer.assign( topic.LineNumber() );
m_sMessageBuffer.append( m_sIntBuffer );
m_sMessageBuffer.append( LOGOG_CONST_STRING(": "));
}
RenderTimeOfDay();
if ( flags & TOPIC_LEVEL_FLAG )
{
m_sMessageBuffer.append( ErrorDescription( topic.Level()));
m_sMessageBuffer.append( LOGOG_CONST_STRING(": "));
}
if ( flags & TOPIC_GROUP_FLAG )
{
m_sMessageBuffer.append( LOGOG_CONST_STRING("{") );
m_sMessageBuffer.append( topic.Group() );
m_sMessageBuffer.append( LOGOG_CONST_STRING("} ") );
}
if ( flags & TOPIC_CATEGORY_FLAG )
{
m_sMessageBuffer.append( LOGOG_CONST_STRING("["));
m_sMessageBuffer.append( topic.Category() );
m_sMessageBuffer.append( LOGOG_CONST_STRING("] "));
}
if ( flags & TOPIC_MESSAGE_FLAG )
{
m_sMessageBuffer.append( topic.Message() );
m_sMessageBuffer.append( (LOGOG_CHAR)'\n' );
}
if ( target.GetNullTerminatesStrings() )
m_sMessageBuffer.append( (LOGOG_CHAR)NULL );
return m_sMessageBuffer;
}
} // namespace BaseLib
......@@ -15,8 +15,14 @@
#ifndef TEMPLATELOGOGFORMATTERSUPPRESSEDGCC_H_
#define TEMPLATELOGOGFORMATTERSUPPRESSEDGCC_H_
// ** INCLUDES **
#include <string>
#include "logog/include/logog.hpp"
#ifdef USE_MPI
#include <mpi.h>
#endif
#include "StringTools.h"
namespace BaseLib {
......@@ -28,6 +34,10 @@ namespace BaseLib {
template <int T_SUPPPRESS_TOPIC_FLAG>
class TemplateLogogFormatterSuppressedGCC : public logog::FormatterGCC
{
public:
#ifdef USE_MPI
TemplateLogogFormatterSuppressedGCC(MPI_Comm mpi_comm = MPI_COMM_WORLD);
#endif
virtual TOPIC_FLAGS GetTopicFlags( const logog::Topic &topic )
{
......@@ -35,8 +45,16 @@ class TemplateLogogFormatterSuppressedGCC : public logog::FormatterGCC
~( T_SUPPPRESS_TOPIC_FLAG ));
}
};
virtual LOGOG_STRING &Format( const logog::Topic &topic, const logog::Target &target );
#endif // TEMPLATELOGOGFORMATTERSUPPRESSEDGCC_H_
private:
#ifdef USE_MPI
std::string _str_mpi_rank;
#endif
};
} // namespace BaseLib
#include "TemplateLogogFormatterSuppressedGCC-impl.h"
#endif // TEMPLATELOGOGFORMATTERSUPPRESSEDGCC_H_
......@@ -66,6 +66,9 @@ OPTION(OGS_NO_EXTERNAL_LIBS "Builds OGS without any external dependencies." OFF)
OPTION(OGS_USE_LIS "Use Lis" OFF)
OPTION(OGS_USE_PETSC "Use PETSc routines" OFF)
# Paralleization
OPTION(OGS_USE_MPI "Use MPI" OFF)
# Eigen
OPTION(OGS_USE_EIGEN "Use EIGEN for local matrix and vector" ON)
OPTION(EIGEN_NO_DEBUG "Disables Eigen's assertions" ON)
......@@ -118,6 +121,11 @@ ENDIF()
IF(OGS_USE_PETSC)
ADD_DEFINITIONS(-DUSE_PETSC)
SET(OGS_USE_MPI ON)
ENDIF()
IF(OGS_USE_MPI)
ADD_DEFINITIONS(-DUSE_MPI)
ENDIF()
IF(OGS_USE_EIGEN)
......
......@@ -61,6 +61,10 @@ IF (OGS_USE_PETSC)
TARGET_LINK_LIBRARIES( testrunner ${PETSC_LIBRARIES})
ENDIF (OGS_USE_PETSC)
IF (OGS_USE_MPI)
TARGET_LINK_LIBRARIES( testrunner ${MPI_CXX_LIBRARIES})
ENDIF ()
IF(OGS_BUILD_GUI)
TARGET_LINK_LIBRARIES(testrunner
QtDataView
......
......@@ -16,6 +16,10 @@
#include "gtest/gtest.h"
#include "logog/include/logog.hpp"
#ifdef USE_MPI
#include <mpi.h>
#endif
#ifdef USE_LIS
#include <lis.h>
#endif
......@@ -24,6 +28,7 @@
#include <petscksp.h>
#endif
#include "BaseLib/LogogCustomCout.h"
#include "BaseLib/TemplateLogogFormatterSuppressedGCC.h"
#ifdef QT4_FOUND
#include <QApplication>
......@@ -33,12 +38,15 @@
int main(int argc, char* argv[])
{
#ifdef QT4_FOUND
QApplication app(argc, argv, false);
QApplication app(argc, argv, false);
#endif
int ret = 0;
LOGOG_INITIALIZE();
{
logog::Cout out;
#ifdef USE_MPI
MPI_Init(&argc, &argv);
#endif
BaseLib::LogogCustomCout out;
BaseLib::TemplateLogogFormatterSuppressedGCC<TOPIC_LEVEL_FLAG | TOPIC_FILE_NAME_FLAG | TOPIC_LINE_NUMBER_FLAG> custom_format;
out.SetFormatter(custom_format);
......@@ -78,6 +86,10 @@ int main(int argc, char* argv[])
PetscFinalize();
#endif
#ifdef USE_MPI
MPI_Finalize();
#endif
} // make sure no logog objects exist when LOGOG_SHUTDOWN() is called.
LOGOG_SHUTDOWN();
......
......@@ -134,6 +134,10 @@ IF(OGS_USE_PETSC)
include_directories(
${PETSC_INCLUDES}
)
ENDIF()
FIND_PACKAGE(MPI REQUIRED)
IF(OGS_USE_MPI)
FIND_PACKAGE( MPI REQUIRED )
INCLUDE_DIRECTORIES(SYSTEM ${MPI_CXX_INCLUDE_PATH})
ENDIF()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment