Skip to content
Snippets Groups Projects
Commit 67f83665 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Write broadcast of any trivial type values

Remind that TinyVector, TinyMatrix, CellType,... are trivial types
parent f8b39f8a
No related branches found
No related tags found
1 merge request!11Feature/mpi
......@@ -66,12 +66,3 @@ _allGather(int& data) const
return gather;
}
int Messenger::
_broadcast_value(int& data, int root_rank) const
{
#ifdef PASTIS_HAS_MPI
MPI_Bcast(&data, 1, MPI_INT, root_rank, MPI_COMM_WORLD);
#endif // PASTIS_HAS_MPI
return data;
}
......@@ -84,13 +84,26 @@ class Messenger
Array<int> _allGather(int& data) const;
int _broadcast_value(int& data, int root_rank) const;
template <typename DataType>
void _broadcast_value(DataType& data, int root_rank) const
{
#ifdef PASTIS_HAS_MPI
static_assert(not std::is_const_v<DataType>);
static_assert(std::is_arithmetic_v<DataType>);
MPI_Datatype mpi_datatype
= Messenger::helper::mpiType<DataType>();
MPI_Bcast(&data, 1, mpi_datatype, root_rank, MPI_COMM_WORLD);
#endif // PASTIS_HAS_MPI
}
template <typename ArrayType>
void _broadcast_array(ArrayType& array, int root_rank) const
{
using DataType = typename ArrayType::data_type;
static_assert(not std::is_const_v<DataType>);
static_assert(std::is_arithmetic_v<DataType>);
#ifdef PASTIS_HAS_MPI
MPI_Datatype mpi_datatype
......@@ -260,10 +273,26 @@ class Messenger
PASTIS_INLINE
void broadcast(DataType& data, int root_rank) const
{
if constexpr(std::is_same<DataType, int>()) {
return _broadcast_value(data, root_rank);
static_assert(not std::is_const_v<DataType>,
"cannot broadcast const data");
if constexpr(std::is_arithmetic_v<DataType>) {
_broadcast_value(data, root_rank);
} else if constexpr(std::is_trivial_v<DataType>) {
using CastType = helper::split_cast_t<DataType>;
if constexpr(sizeof(CastType) == sizeof(DataType)) {
CastType& cast_data = reinterpret_cast<CastType&>(data);
_broadcast_value(cast_data, root_rank);
} else {
static_assert(std::is_same<DataType, int>(), "unexpected type of data");
#ifdef PASTIS_HAS_MPI
MPI_Datatype mpi_datatype
= Messenger::helper::mpiType<CastType>();
MPI_Bcast(reinterpret_cast<CastType*>(&data), sizeof(DataType)/sizeof(CastType),
mpi_datatype, root_rank, MPI_COMM_WORLD);
#endif // PASTIS_HAS_MPI
}
} else {
static_assert(std::is_trivial_v<DataType>,
"unexpected non trivial type of data");
}
}
......@@ -351,9 +380,9 @@ void barrier()
template <typename DataType>
PASTIS_INLINE
DataType broadcast(const DataType& data, int root_rank)
void broadcast(DataType& data, int root_rank)
{
return messenger().broadcast(data, root_rank);
messenger().broadcast(data, root_rank);
}
template <typename DataType>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment