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

Add barrier and simple broadcast for integer arrays

parent 03dc3f3b
Branches
Tags
1 merge request!11Feature/mpi
...@@ -50,3 +50,23 @@ Messenger:: ...@@ -50,3 +50,23 @@ Messenger::
MPI_Finalize(); MPI_Finalize();
#endif // PASTIS_HAS_MPI #endif // PASTIS_HAS_MPI
} }
void Messenger::barrier() const
{
#ifdef PASTIS_HAS_MPI
MPI_Barrier(MPI_COMM_WORLD);
#endif // PASTIS_HAS_MPI
}
Array<int> Messenger::
_broadcast(Array<int>& array, int root_rank) const
{
int size = array.size();
MPI_Bcast(&size, 1, MPI_INT, root_rank, MPI_COMM_WORLD);
if (commRank() != root_rank) {
array = Array<int>(size);
}
MPI_Bcast(&(array[0]), array.size(), MPI_INT, root_rank, MPI_COMM_WORLD);
return array;
}
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include <PastisMacros.hpp> #include <PastisMacros.hpp>
#include <PastisAssert.hpp> #include <PastisAssert.hpp>
#include <Array.hpp>
class Messenger class Messenger
{ {
private: private:
...@@ -12,6 +14,9 @@ class Messenger ...@@ -12,6 +14,9 @@ class Messenger
int m_rank{0}; int m_rank{0};
int m_size{1}; int m_size{1};
Array<int> _broadcast(Array<int>& array, int root_rank) const;
public: public:
static void create(int& argc, char* argv[]); static void create(int& argc, char* argv[]);
static void destroy(); static void destroy();
...@@ -35,6 +40,20 @@ class Messenger ...@@ -35,6 +40,20 @@ class Messenger
return m_size; return m_size;
} }
void barrier() const;
template <typename DataType>
PASTIS_INLINE
Array<DataType> broadcast(const Array<DataType>& array, int root_rank) const
{
if constexpr(std::is_same<DataType, int>()) {
Array<int> int_array = array;
return _broadcast(int_array, root_rank);
} else {
static_assert(std::is_same<DataType, int>(), "unexpected type of data");
}
}
Messenger(const Messenger&) = delete; Messenger(const Messenger&) = delete;
~Messenger(); ~Messenger();
}; };
...@@ -59,5 +78,17 @@ const int& commSize() ...@@ -59,5 +78,17 @@ const int& commSize()
return messenger().size(); return messenger().size();
} }
PASTIS_INLINE
void barrier()
{
return messenger().barrier();
}
template <typename DataType>
PASTIS_INLINE
Array<DataType> broadcast(const Array<DataType>& array, int root_rank)
{
return messenger().broadcast(array, root_rank);
}
#endif // MESSENGER_HPP #endif // MESSENGER_HPP
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment