Skip to content
Open
10 changes: 6 additions & 4 deletions src/coreComponents/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ Also provides commonly used components for such as logging, formatting, memory a
#
set( common_headers
${CMAKE_BINARY_DIR}/include/common/GeosxConfig.hpp
format/table/TableLayout.hpp
format/table/TableFormatter.hpp
format/table/TableData.hpp
format/table/TableFormatter.hpp
format/table/TableLayout.hpp
format/table/TableMpiComponents.hpp
format/EnumStrings.hpp
format/LogPart.hpp
format/Format.hpp
Expand Down Expand Up @@ -71,9 +72,10 @@ endif( )
# Specify all sources
#
set( common_sources
format/table/TableLayout.cpp
format/table/TableFormatter.cpp
format/table/TableData.cpp
format/table/TableFormatter.cpp
format/table/TableLayout.cpp
format/table/TableMpiComponents.cpp
format/LogPart.cpp
format/StringUtilities.cpp
logger/GeosExceptions.cpp
Expand Down
105 changes: 105 additions & 0 deletions src/coreComponents/common/MpiWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,111 @@ template<> MPI_Datatype getMpiPairType< double, double >()

} /* namespace internal */


template<>
void MpiWrapper::gatherStringSet< stdVector >( stdVector< string > const & strSet,
stdVector< string > & result,
MPI_Comm MPI_PARAM( comm ))
{
int rank = MpiWrapper::commRank();
int size = MpiWrapper::commSize();

string localAllStrings;
stdVector< int > localStrSizes;
localAllStrings.reserve( strSet.size() * 32 );

for( auto const & str : strSet )
{
localAllStrings += str;
localStrSizes.push_back( static_cast< int >(str.size()));
}

//1 - We gather the metadata across all ranks
struct MetaData
{
int count;
int size;
};
MetaData localMeta = { static_cast< int >(strSet.size()), static_cast< int >(localAllStrings.size()) };

stdVector< MetaData > allMeta;
if( rank == 0 )
{
allMeta.resize( size );
}

int localMetaArr[2] = { localMeta.count, localMeta.size };
stdVector< int > allMetaArr;

if( rank == 0 )
allMetaArr.resize( size * 2 );

MpiWrapper::gather( localMetaArr, 2, allMetaArr.data(), 2, 0, comm );

//2 - Prepare displacement arrays for rank 0
stdVector< int > allStrSizes;
string allStrings;
stdVector< int > counts_counts( size );
stdVector< int > displs_counts( size );
stdVector< int > counts_chars( size );
stdVector< int > displs_chars( size );

int totalStrCount = 0;

if( rank == 0 )
{
int currentCountOffset = 0;
int currentCharOffset = 0;

for( int currRank = 0; currRank < size; ++currRank )
{
int c_count = allMetaArr[2*currRank];
int c_size = allMetaArr[2*currRank + 1];

counts_counts[currRank] = c_count;
displs_counts[currRank] = currentCountOffset;

counts_chars[currRank] = c_size;
displs_chars[currRank] = currentCharOffset;

currentCountOffset += c_count;
currentCharOffset += c_size;
}

totalStrCount = currentCountOffset;

allStrSizes.resize( totalStrCount );
allStrings.resize( currentCharOffset );
}

// 3. Gatherv des tailles de chaînes
MpiWrapper::gatherv( localStrSizes.data(), localMeta.count,
allStrSizes.data(), counts_counts.data(), displs_counts.data(),
0, comm );

// 4. Gatherv du contenu (chars)
MpiWrapper::gatherv( localAllStrings.c_str(), localMeta.size,
allStrings.data(), counts_chars.data(), displs_chars.data(),
0, comm );

// 5. Reconstruction
if( rank == 0 )
{
const char * dataPtr = allStrings.c_str();
int currentOffset = 0;
for( int i = 0; i < totalStrCount; ++i )
{
int len = allStrSizes[i];

std::string const temp( dataPtr + currentOffset, len );

result.emplace( result.end(), temp );
currentOffset += len;
}
}

}

} /* namespace geos */

#if defined(__clang__)
Expand Down
6 changes: 6 additions & 0 deletions src/coreComponents/common/MpiWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "common/DataTypes.hpp"
#include "common/Span.hpp"
#include "common/StdContainerWrappers.hpp"
#include "common/TypesHelpers.hpp"

#include <numeric>
Expand Down Expand Up @@ -331,6 +332,11 @@ struct MpiWrapper
*/
static int nodeCommSize();

template< template< class > class CONTAINER = stdVector >
void static gatherStringSet( CONTAINER< string > const & strSet,
CONTAINER< string > & result,
MPI_Comm MPI_PARAM( comm ));

/**
* @brief Strongly typed wrapper around MPI_Allgather.
* @tparam T_SEND The pointer type for \p sendbuf
Expand Down
10 changes: 0 additions & 10 deletions src/coreComponents/common/format/table/TableData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,6 @@ void TableData::clear()
getErrorsList().clear();
}

stdVector< stdVector< TableData::CellData > > const & TableData::getTableDataRows() const
{
return m_rows;
}

stdVector< stdVector< TableData::CellData > > & TableData::getTableDataRows()
{
return m_rows;
}

void TableData2D::collectTableValues( arrayView1d< real64 const > dim0AxisCoordinates,
arrayView1d< real64 const > dim1AxisCoordinates,
arrayView1d< real64 const > values,
Expand Down
21 changes: 7 additions & 14 deletions src/coreComponents/common/format/table/TableData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,6 @@ class TableData
void clearErrors()
{ m_errors->clear(); }

/**
* @return The const rows of the table
*/
stdVector< stdVector< CellData > > const & getTableDataRows() const;

/**
* @return The rows of the table
*/
stdVector< stdVector< CellData > > & getTableDataRows();

/**
* @brief Get all error messages
* @return The vector of error messages
Expand All @@ -133,16 +123,19 @@ class TableData
DataRows const & getCellsData() const
{ return m_rows; }

/**
* @return The const table data rows
*/
DataRows & getCellsData()
{ return m_rows; }

/**
* @brief Comparison operator for data rows
* @param comparingTable The tableData values to compare
* @return The comparison result
*/
inline bool operator==( TableData const & comparingTable ) const
{

return getCellsData() == comparingTable.getCellsData();
}
{ return getCellsData() == comparingTable.getCellsData(); }

/**
* @brief Get all error messages
Expand Down
70 changes: 44 additions & 26 deletions src/coreComponents/common/format/table/TableFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
* @file TableFormatter.cpp
*/

#include "TableFormatter.hpp"
#include <numeric>
#include "common/format/StringUtilities.hpp"
#include "common/logger/Logger.hpp"
#include "TableFormatter.hpp"

namespace geos
{
Expand Down Expand Up @@ -157,7 +157,8 @@ string TableCSVFormatter::headerToString() const

string TableCSVFormatter::dataToString( TableData const & tableData ) const
{
RowsCellInput const rowsValues( tableData.getTableDataRows() );

RowsCellInput const rowsValues( tableData.getCellsData() );
string result;
size_t total_size = 0;
for( auto const & row : rowsValues )
Expand Down Expand Up @@ -232,12 +233,13 @@ string TableTextFormatter::toString< TableData >( TableData const & tableData )

initalizeTableGrids( m_tableLayout, tableData,
headerCellsLayout, dataCellsLayout, errorCellsLayout,
tableTotalWidth );
outputTable( m_tableLayout, tableOutput,
headerCellsLayout, dataCellsLayout, errorCellsLayout,
tableTotalWidth );
tableTotalWidth, nullptr );

string const sepLine = string( tableTotalWidth, m_horizontalLine );
outputTableHeader( tableOutput, m_tableLayout, headerCellsLayout, sepLine );
outputTableData( tableOutput, m_tableLayout, dataCellsLayout );
outputTableFooter( tableOutput, m_tableLayout, errorCellsLayout, sepLine, !dataCellsLayout.empty() );

getErrorsList().clear();
return tableOutput.str();
}

Expand All @@ -246,10 +248,11 @@ void TableTextFormatter::initalizeTableGrids( PreparedTableLayout const & tableL
CellLayoutRows & headerCellsLayout,
CellLayoutRows & dataCellsLayout,
CellLayoutRows & errorCellsLayout,
size_t & tableTotalWidth ) const
size_t & tableTotalWidth,
ColumnWidthModifier columnWidthModifier ) const
{
RowsCellInput const & inputDataValues( tableInputData.getCellsData() );
bool const hasColumnLayout = tableLayout.getColumnLayersCount() > 0;
RowsCellInput const & inputDataValues( tableInputData.getTableDataRows() );
size_t const inputDataRowsCount = !inputDataValues.empty() ? inputDataValues.front().size() : 0;
size_t const nbVisibleColumns = std::max( size_t( 1 ), ( hasColumnLayout ?
tableLayout.getVisibleLowermostColumnCount() :
Expand Down Expand Up @@ -282,6 +285,9 @@ void TableTextFormatter::initalizeTableGrids( PreparedTableLayout const & tableL
stretchColumnsByMergedCellsWidth( columnsWidth, dataCellsLayout, tableLayout, true );
stretchColumnsByMergedCellsWidth( columnsWidth, errorCellsLayout, tableLayout, true );

if( columnWidthModifier )
columnWidthModifier( columnsWidth );

// the columns width array is now sized after all the table, we can compute the total table width
tableTotalWidth = tableLayout.getBorderMargin() * 2 + 2;
for( size_t columnId = 0; columnId < columnsWidth.size(); ++columnId )
Expand All @@ -308,14 +314,14 @@ void TableTextFormatter::populateTitleCellsLayout( PreparedTableLayout const & t
// the title row consists in a row of cells merging with the last cell containing the title text
headerCellsLayout.emplace_back() = {
stdVector< TableLayout::CellLayout >( nbVisibleColumns,
TableLayout::CellLayout( CellType::MergeNext ) ), // cells
TableLayout::CellLayout( CellType::MergeNext ) ), // cells
titleInput.getHeight(), // sublinesCount
};
headerCellsLayout.back().cells.back() = titleInput;

headerCellsLayout.emplace_back() = {
stdVector< TableLayout::CellLayout >( nbVisibleColumns,
TableLayout::CellLayout( CellType::Separator ) ), // cells
TableLayout::CellLayout( CellType::Separator ) ), // cells
1, // sublinesCount
};
}
Expand Down Expand Up @@ -494,8 +500,8 @@ void TableTextFormatter::populateDataCellsLayout( PreparedTableLayout const & ta
string_view( &m_horizontalLine, 1 ) :
string_view( inputCell.value );
TableLayout::Alignment const alignment = inputCell.type == CellType::Header ?
tableLayout.defaultHeaderAlignment :
tableLayout.defaultValueAlignment;
tableLayout.getDefaultHeaderAlignment() :
tableLayout.getDefaultValueAlignment();

TableLayout::CellLayout & outputCell = outputRow.cells[idxColumn];
outputCell = TableLayout::CellLayout( inputCell.type, alignment );
Expand Down Expand Up @@ -702,34 +708,44 @@ void TableTextFormatter::applyColumnsWidth( stdVector< size_t > const & columnsW
}
}

void TableTextFormatter::outputTable( PreparedTableLayout const & tableLayout,
std::ostream & tableOutput,
CellLayoutRows const & headerCellsLayout,
CellLayoutRows const & dataCellsLayout,
CellLayoutRows & errorCellsLayout,
size_t const tableTotalWidth ) const
void TableTextFormatter::outputTableHeader( std::ostream & tableOutput,
PreparedTableLayout const & tableLayout,
CellLayoutRows const & headerCellsLayout,
string_view sepLine ) const
{
string const sepLine = string( tableTotalWidth, m_horizontalLine );
if( tableLayout.isLineBreakEnabled())
{
tableOutput << '\n';
}
tableOutput << sepLine << '\n';
tableOutput << tableLayout.getIndentationStr() << sepLine << '\n';
outputLines( tableLayout, headerCellsLayout, tableOutput );
}

if( !dataCellsLayout.empty())
void TableTextFormatter::outputTableData( std::ostream & tableOutput,
PreparedTableLayout const & tableLayout,
CellLayoutRows const & dataCellsLayout ) const
{
if( !dataCellsLayout.empty() )
{
outputLines( tableLayout, dataCellsLayout, tableOutput );
}
}

void TableTextFormatter::outputTableFooter( std::ostream & tableOutput,
PreparedTableLayout const & tableLayout,
CellLayoutRows & errorCellsLayout,
string_view sepLine,
bool hasData ) const
{
if( !errorCellsLayout.empty())
{
outputErrors( tableLayout, errorCellsLayout, tableOutput );
}

if( !dataCellsLayout.empty() || getErrorsList().hasErrors())
tableOutput << sepLine;

if( hasData || !errorCellsLayout.empty() )
{
tableOutput << tableLayout.getIndentationStr() << sepLine;
}

if( tableLayout.isLineBreakEnabled())
{
Expand Down Expand Up @@ -816,6 +832,7 @@ void TableTextFormatter::outputLines( PreparedTableLayout const & tableLayout,
if( isLeftBorderCell )
{ // left table border
isLeftBorderCell=false;
tableOutput << tableLayout.getIndentationStr();
tableOutput << m_verticalLine << string( nbBorderSpaces, cellSpaceChar );
}
else
Expand Down Expand Up @@ -845,4 +862,5 @@ void TableTextFormatter::outputLines( PreparedTableLayout const & tableLayout,
idxRow++;
}
}
}

} /* namespace geos */
Loading
Loading