Skip to content

Commit

Permalink
refactor: Refactor log & CSV statistics (#3406)
Browse files Browse the repository at this point in the history
* add case for fill all column in case of table 2D

* refactor build separator

* method display layout added

* doc + code clarification

* remove addErrorMsg function

* refactor table function for more clarity

* Table well formatted

* getter margin adjustment

* fix margin if title largest

* adding setMargin + small cleaning

* add const, remove topSeparator, csv opti

* merged row updated + multiphase case updated

---------

Co-authored-by: MelReyCG <[email protected]>
Co-authored-by: Matteo Cusini <[email protected]>
  • Loading branch information
3 people authored Jan 16, 2025
1 parent 8f9126c commit 9365098
Show file tree
Hide file tree
Showing 25 changed files with 2,451 additions and 1,073 deletions.
54 changes: 47 additions & 7 deletions src/coreComponents/common/MpiWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ struct MpiWrapper
int sendcount,
T_RECV * recvbuf,
int recvcount,
MPI_Comm comm );
MPI_Comm comm = MPI_COMM_GEOS );

/**
* @brief Strongly typed wrapper around MPI_Allgatherv.
Expand All @@ -335,7 +335,7 @@ struct MpiWrapper
T_RECV * recvbuf,
int * recvcounts,
int * displacements,
MPI_Comm comm );
MPI_Comm comm = MPI_COMM_GEOS );

/**
* @brief Convenience function for MPI_Allgather.
Expand Down Expand Up @@ -422,10 +422,10 @@ struct MpiWrapper


template< typename T >
static int scan( T const * sendbuf, T * recvbuf, int count, MPI_Op op, MPI_Comm comm );
static int scan( T const * sendbuf, T * recvbuf, int count, MPI_Op op, MPI_Comm comm = MPI_COMM_GEOS );

template< typename T >
static int exscan( T const * sendbuf, T * recvbuf, int count, MPI_Op op, MPI_Comm comm );
static int exscan( T const * sendbuf, T * recvbuf, int count, MPI_Op op, MPI_Comm comm = MPI_COMM_GEOS );

/**
* @brief Strongly typed wrapper around MPI_Bcast.
Expand All @@ -436,7 +436,7 @@ struct MpiWrapper
* @return The return value of the underlying call to MPI_Bcast().
*/
template< typename T >
static int bcast( T * buffer, int count, int root, MPI_Comm comm );
static int bcast( T * buffer, int count, int root, MPI_Comm comm = MPI_COMM_GEOS );


/**
Expand Down Expand Up @@ -466,7 +466,28 @@ struct MpiWrapper
TR * const recvbuf,
int recvcount,
int root,
MPI_Comm comm );
MPI_Comm comm = MPI_COMM_GEOS );

/**
* @brief Strongly typed wrapper around MPI_Gather().
* @tparam TS The pointer type for \p sendbuf
* @tparam TR The pointer type for \p recvbuf
* @param[in] sendbuf The pointer to the sending buffer.
* @param[out] recvbuf The pointer to the receive buffer.
* @param[in] recvcount The number of values to receive.
* @param[in] root The rank recieving the data.
* @param[in] comm The MPI_Comm over which the gather operates.
* @return
*/
template< typename T, typename DST_CONTAINER,
typename = std::enable_if_t<
std::is_trivially_copyable_v< T > &&
std::is_same_v< decltype(std::declval< DST_CONTAINER >().size()), std::size_t > &&
std::is_same_v< decltype(std::declval< DST_CONTAINER >().data()), T * > > >
static int gather( T const & value,
DST_CONTAINER & destValuesBuffer,
int root,
MPI_Comm comm = MPI_COMM_GEOS );

/**
* @brief Strongly typed wrapper around MPI_Gatherv.
Expand All @@ -489,7 +510,7 @@ struct MpiWrapper
const int * recvcounts,
const int * displs,
int root,
MPI_Comm comm );
MPI_Comm comm = MPI_COMM_GEOS );

/**
* @brief Returns an MPI_Op associated with our strongly typed Reduction enum.
Expand Down Expand Up @@ -900,6 +921,25 @@ int MpiWrapper::gather( TS const * const sendbuf,
#endif
}

template< typename T, typename DST_CONTAINER, typename >
int MpiWrapper::gather( T const & value,
DST_CONTAINER & destValuesBuffer,
int root,
MPI_Comm MPI_PARAM( comm ) )
{
if( commRank() == 0 )
GEOS_ERROR_IF_LT_MSG( destValuesBuffer.size(), size_t( commSize() ),
"Receive buffer is not large enough to contain the values to receive." );
#ifdef GEOS_USE_MPI
return MPI_Gather( &value, sizeof( T ), internal::getMpiType< uint8_t >(),
destValuesBuffer.data(), sizeof( T ), internal::getMpiType< uint8_t >(),
root, comm );
#else
memcpy( destValuesBuffer.data(), &value, sendBufferSize );
return 0;
#endif
}

template< typename TS, typename TR >
int MpiWrapper::gatherv( TS const * const sendbuf,
int sendcount,
Expand Down
42 changes: 19 additions & 23 deletions src/coreComponents/common/format/table/TableData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,38 @@
*/

#include "TableData.hpp"
#include "common/logger/Logger.hpp"

namespace geos
{

void TableData::addRow( std::vector< string > const & row )
void TableData::addRow( std::vector< TableData::CellData > const & row )
{
if( m_rows.size() != 0 && row.size() != m_rows[m_rows.size() - 1].size() )
m_rows.push_back( row );
}

void TableData::addSeparator()
{
if( m_rows.empty())
{
string msg = "Remarks : some cells may be missing";
if( std::find( m_errorsMsg.begin(), m_errorsMsg.end(), msg ) == m_errorsMsg.end())
{
m_errorsMsg.push_back( msg );
}
GEOS_ERROR( "Bad use of a Tabledata::addSeparator(). Make sure you have added values in TableData" );
}
m_rows.push_back( row );

integer rowSize = m_rows[0].size();
m_rows.emplace_back( std::vector< TableData::CellData >( rowSize, { CellType::Separator, "-" } ));

}

void TableData::clear()
{
m_rows.clear();
m_errorsMsg.clear();
}

std::vector< std::vector< string > > const & TableData::getTableDataRows() const
std::vector< std::vector< TableData::CellData > > const & TableData::getTableDataRows() const
{
return m_rows;
}

std::vector< string > const & TableData::getErrorMsgs() const
{
return m_errorsMsg;
}

void TableData2D::collectTableValues( arraySlice1d< real64 const > rowAxisValues,
arraySlice1d< real64 const > columnAxisValues,
arrayView1d< real64 const > values )
Expand All @@ -75,7 +74,6 @@ TableData2D::TableDataHolder TableData2D::convertTable2D( arrayView1d< real64 co
{
string const rowFmt = GEOS_FMT( "{} = {{}}", rowAxisDescription );
string const columnFmt = GEOS_FMT( "{} = {{}}", columnAxisDescription );

collectTableValues( coordinates[0], coordinates[1], values );
return buildTableData( string( units::getDescription( valueUnit )),
rowFmt,
Expand All @@ -87,7 +85,6 @@ TableData2D::TableDataHolder TableData2D::buildTableData( string_view targetUnit
string_view columnFmt ) const
{
TableData2D::TableDataHolder tableData1D;
std::vector< size_t > rowsLength;

tableData1D.headerNames.push_back( string( targetUnit ) );

Expand All @@ -99,23 +96,22 @@ TableData2D::TableDataHolder TableData2D::buildTableData( string_view targetUnit
// insert row value and row cell values
for( auto const & [rowValue, rowMap] : m_data )
{
std::vector< string > currentRowValues;
std::vector< TableData::CellData > currentRowValues;
currentRowValues.reserve( rowMap.size() );
currentRowValues.push_back( GEOS_FMT( rowFmt, rowValue ) );
currentRowValues.push_back( {CellType::Value, GEOS_FMT( rowFmt, rowValue )} );

std::set< real64 >::const_iterator columnIt = m_columnValues.begin();
for( auto const & [columnValue, cellValue] : rowMap )
{
// if a column value(s) is/are missing, insert empty entry(ies)
while( columnValue > *( columnIt++ ) && columnIt != m_columnValues.end() )
{
currentRowValues.push_back( "" );
currentRowValues.push_back( {CellType::Value, ""} );
}
currentRowValues.push_back( GEOS_FMT( "{}", cellValue ) );
currentRowValues.push_back( {CellType::Value, GEOS_FMT( "{}", cellValue )} );
}

tableData1D.tableData.addRow( std::move( currentRowValues ) );
rowsLength.push_back( currentRowValues.size() );
tableData1D.tableData.addRow( currentRowValues );
}

return tableData1D;
Expand Down
62 changes: 48 additions & 14 deletions src/coreComponents/common/format/table/TableData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "common/Units.hpp"
#include "common/DataTypes.hpp"
#include "common/format/Format.hpp"
#include "TableTypes.hpp"

namespace geos
{
Expand All @@ -33,10 +34,22 @@ namespace geos
class TableData
{
public:

/**
* @brief Representing a data in TableData
*/
struct CellData
{
/// The cell type
CellType type;
/// The cell value
string value = "";
};

/**
* @brief Add a row to the table.
* The values passed to addRow (can be any type).
* @param args Cell values to be added to the row.
* @param args CellData values to be added to the row.
*/
template< typename ... Args >
void addRow( Args const & ... args );
Expand All @@ -45,7 +58,13 @@ class TableData
* @brief Add a row to the table
* @param row A vector of string representing a row
*/
void addRow( std::vector< string > const & row );
void addRow( std::vector< CellData > const & row );

/**
* @brief Add a line separator to the table
* You must have filled values in TableData before using it
*/
void addSeparator();

/**
* @brief Reset data in the table
Expand All @@ -55,7 +74,7 @@ class TableData
/**
* @return The rows of the table
*/
std::vector< std::vector< string > > const & getTableDataRows() const;
std::vector< std::vector< CellData > > const & getTableDataRows() const;

/**
* @brief Get all error messages
Expand All @@ -66,10 +85,7 @@ class TableData
private:

/// vector containing all rows with cell values
std::vector< std::vector< string > > m_rows;

/// store error if there are any inconsistencies related to the table
std::vector< string > m_errorsMsg;
std::vector< std::vector< CellData > > m_rows;

};

Expand Down Expand Up @@ -98,7 +114,7 @@ class TableData2D
/**
* @brief Add a cell to the table. If necessary, create automatically the containing column & row.
* @tparam T The value passed to addCell (can be any type).
* @param value Cell value to be added.
* @param value CellData value to be added.
* @param rowValue The value of the row containing the cell.
* @param columnValue The value of the column containing the cell.
*/
Expand Down Expand Up @@ -149,17 +165,35 @@ class TableData2D
std::set< real64 > m_columnValues;
};

/**
* @brief Trait to check is the args is a special type of cell
* @tparam T The type of a cell
*/
template< typename T >
constexpr bool isCellType = std::is_same_v< T, CellType >;

template< typename ... Args >
void TableData::addRow( Args const &... args )
{
std::vector< string > m_cellsValue;
std::vector< CellData > cells;
( [&] {
static_assert( has_formatter_v< decltype(args) >, "Argument passed in addRow cannot be converted to string" );
string const cellValue = GEOS_FMT( "{}", args );
m_cellsValue.push_back( cellValue );
static_assert( has_formatter_v< decltype(args) > || isCellType< std::decay_t< decltype(args) > >, "Argument passed in addRow cannot be converted to string nor a CellType" );
if constexpr (std::is_same_v< Args, CellType >) {
if( args == CellType::Separator )
{
cells.push_back( {CellType::Separator} );
}
else
{
cells.push_back( {CellType::MergeNext} );
}
}
else
{
cells.push_back( {CellType::Value, GEOS_FMT( "{}", args )} );
}
} (), ...);

addRow( m_cellsValue );
addRow( cells );
}

template< typename T >
Expand Down
Loading

0 comments on commit 9365098

Please sign in to comment.