Skip to content
Communicator.hpp 1.92 KiB
Newer Older
#include <boost/container/static_vector.hpp>
#include "Configuration.hpp"
#include "MpiEnvironment.hpp"
#include "State.hpp"
// creates the graph topology and does the communication
class Communicator {
	constexpr static std::size_t NoNeighbors{8};

	// for very small container with known max size, this is a performance
	// improvement
	template <typename T>
	using Vector = boost::container::static_vector<T, NoNeighbors>;

  public:
	// Life cycle handling for MPI_Requests
	class MpiRequest { // TODO: unnest
	  public:
		template <typename T>
		using DoubleVector =
		    boost::container::static_vector<T, NoNeighbors * 2>;

	  private:
		DoubleVector<MPI_Request> reqs_;
		bool finished{};

	  public:
		MpiRequest(DoubleVector<MPI_Request> reqs);
		MpiRequest(const MpiRequest&) = default;
		MpiRequest(MpiRequest&&) = default;
		MpiRequest& operator=(const MpiRequest&) = default;
		MpiRequest& operator=(MpiRequest&&) = default;
		void Wait();
		~MpiRequest();
	};

	// data members for graph topology
	Vector<int> neighbors_;
	Vector<int> sizes_;
	Vector<MPI_Datatype> sendTypes_;
	const Vector<MPI_Datatype>& recvTypes_{sendTypes_};
	Vector<MPI_Aint> sendDisplacements_;
	Vector<MPI_Aint> recvDisplacements_;
	MPI_Comm commDistGraph_{MPI_COMM_NULL};
	// data types
	MPI_Datatype haloRowType_{};
	MPI_Datatype haloColumnType_{};
	MPI_Datatype haloCornerType_{MPI_CHAR};
	Communicator() = default;
Thomas Steinreiter's avatar
Thomas Steinreiter committed
	Communicator(const MpiEnvironment& env,
	             const Size& gridSize, const Size& tileSize);
	void swap(Communicator& first, Communicator& second);
	Communicator(Communicator&) = delete;
	Communicator& operator=(Communicator&) = delete;
	Communicator(Communicator&& other) noexcept;
	Communicator& operator=(Communicator&& other) noexcept;
	virtual void Communicate(State* model) = 0;
	virtual MpiRequest AsyncCommunicate(State* model) = 0;