WIP: mpi...

This commit is contained in:
Christian Zimmermann 2024-03-12 20:13:18 +01:00
parent 6edd39de37
commit 93ff79194e
5 changed files with 237 additions and 1 deletions

View file

@ -0,0 +1,106 @@
// -*- C++ -*-
/**
@file opt/mpi/include/mpi_wrappers.cc.h
@brief MPI wrapper template implementations.
Copyright (c) 2024 Christian Zimmermann. All rights reserved.
Mail: chizeta@f3l.de
**/
#ifndef __cxz_mpi_wrappers_cc_h__
#define __cxz_mpi_wrappers_cc_h__
#include "mpi_wrappers.h"
namespace CNORXZ
{
namespace mpi
{
template <typename T>
void BCast<T>::bcast(T& d, SizeT root)
{
static_assert( TypeMap<T>::exists, "no bcast implementation for given type" );
const int ret = MPI_Bcast( reinterpret_cast<void*>(&d), 1,
TypeMap<T>::value, MPI_COMM_WORLD );
CXZ_ASSERT(ret == MPI_SUCCESS, "got bcast error = " << ret);
return;
}
// preliminary solutions, use cereal serialization in the future!!!
template <typename T>
void BCast<Vector<T>>::bcast(Vector<T>& d, SizeT root)
{
SizeT size = d.size();
const int ret = MPI_Bcast( reinterpret_cast<void*>(&size), 1,
MPI_UNSIGNED_LONG, MPI_COMM_WORLD );
CXZ_ASSERT(ret == MPI_SUCCESS, "got bcast error = " << ret);
if(size != d.size()){
d.resize(size);
}
if constexpr( BCast<T>::special ){
for(auto& x: d){
bcast(x, root);
}
}
else {
const int ret2 = MPI_Bcast( reinterpret_cast<void*>(d.data()), size,
TypeMap<T>::value, MPI_COMM_WORLD );
CXZ_ASSERT(ret2 == MPI_SUCCESS, "got bcast error = " << ret2);
}
}
template <typename T, SizeT N>
void BCast<Arr<T,N>>::bcast(Arr<T,N>& d, SizeT root)
{
if constexpr( BCast<T,N>::special ){
for(auto& x: d){
bcast(x, root);
}
}
else {
const int ret = MPI_Bcast( reinterpret_cast<void*>(d.data()), N,
TypeMap<T>::value, MPI_COMM_WORLD );
CXZ_ASSERT(ret == MPI_SUCCESS, "got bcast error = " << ret2);
}
}
inline void BCast<String>::bcast(String& d, SizeT root)
{
SizeT size = d.size();
const int ret = MPI_Bcast( reinterpret_cast<void*>(&size), 1,
MPI_UNSIGNED_LONG, MPI_COMM_WORLD );
CXZ_ASSERT(ret == MPI_SUCCESS, "got bcast error = " << ret);
if(size != d.size()){
d.resize(size);
}
const int ret = MPI_Bcast( reinterpret_cast<void*>(d.data()), size,
MPI_CHAR, MPI_COMM_WORLD );
CXZ_ASSERT(ret == MPI_SUCCESS, "got bcast error = " << ret);
}
template <typename... Ts>
void BCast<Tuple<Ts...>>::bcast(Tuple<Ts...>& d, SizeT root)
{
if constexpr( ( BCast<Ts>::special or ... ) ){
ifor<0,sizeof...(Ts)>( [&](auto i) { bcast( std::get<i>(d), root ); }, NoF {} );
}
else {
const int ret = MPI_Bcast( reinterpret_cast<void*>(&d), sizeof(d),
MPI_BYTE, MPI_COMM_WORLD );
CXZ_ASSERT(ret == MPI_SUCCESS, "got bcast error = " << ret);
}
}
template <typename T>
void bcast(T& d, SizeT root)
{
return BCast<T>(d, root);
}
} // namespace mpi
} // namespace CNORXZ
#endif

View file

@ -0,0 +1,66 @@
// -*- C++ -*-
/**
@file opt/mpi/include/mpi_wrappers.h
@brief MPI wrapper template delarations.
Copyright (c) 2024 Christian Zimmermann. All rights reserved.
Mail: chizeta@f3l.de
**/
#ifndef __cxz_mpi_wrappers_h__
#define __cxz_mpi_wrappers_h__
#include "mpi.h"
#include "cnorxz.h"
#include "typemap.h"
namespace CNORXZ
{
namespace mpi
{
template <typename T>
struct BCast
{
static constexpr bool special = false;
static void bcast(T& d, SizeT root);
};
template <typename T>
struct BCast<Vector<T>>
{
static constexpr bool special = true;
static void bcast(Vector<T>& d, SizeT root);
};
template <typename T, SizeT N>
struct BCast<Arr<T,N>>
{
static constexpr bool special = true;
static void bcast(Arr<T,N>& d, SizeT root);
};
template <>
struct BCast<String>
{
static constexpr bool special = true;
static inline void bcast(String& d, SizeT root);
};
template <typename... Ts>
struct BCast<Tuple<Ts...>>
{
static constexpr bool special = true;
static void bcast(Tuple<Ts...>& d, SizeT root);
};
template <typename T>
void bcast(T& d, SizeT root);
} // namespace mpi
} // namespace CNORXZ
#endif

View file

@ -13,6 +13,7 @@
#define __cxz_mpi_rrange_cc_h__
#include "rrange.h"
#include "mpi_wrappers.h"
namespace CNORXZ
{

View file

@ -35,7 +35,6 @@ namespace CNORXZ
INDEX_RANDOM_ACCESS_ITERATOR_DEFS(MetaType);
// constructors!!!
/** Default constructor. */
RIndex() = default;

View file

@ -0,0 +1,64 @@
#ifndef __cxz_mpi_typemap_h__
#define __cxz_mpi_typemap_h__
#include "mpi.h"
namespace CNORXZ
{
namespace mpi
{
template <typename T>
struct Typemap
{
static constexpr bool exists = false;
};
template <>
struct Typemap<int>
{
static constexpr bool exists = true;
static constexpr MPI_Datatype value = MPI_INT;
};
template <>
struct Typemap<unsigned>
{
static constexpr bool exists = true;
static constexpr MPI_Datatype value = MPI_UNSIGNED;
};
template <>
struct Typemap<long int>
{
static constexpr bool exists = true;
static constexpr MPI_Datatype value = MPI_LONG;
};
template <>
struct Typemap<unsigned long>
{
static constexpr bool exists = true;
static constexpr MPI_Datatype value = MPI_UNSIGNED_LONG;
};
template <>
struct Typemap<double>
{
static constexpr bool exists = true;
static constexpr MPI_Datatype value = MPI_DOUBLE;
};
template <>
struct Typemap<float>
{
static constexpr bool exists = true;
static constexpr MPI_Datatype value = MPI_FLOAT;
};
// further !!!
} // namespace mpi
} // namespace CNORXZ
#endif