Skip to content
Communicator.hpp 1.26 KiB
Newer Older
#pragma once

#include <vector>

#include "gsl/multi_span"
#include <mpi.h>

#include "MpiEnvironment.hpp"
#include "Util.hpp"
#include "state.hpp"

class Communicator {
	constexpr static std::size_t NoNeighbors{8};

	std::vector<int> _neighbors;
	std::vector<int> _sizes;
	std::vector<MPI_Datatype> _sendTypes;
	const std::vector<MPI_Datatype>& _recvTypes{_sendTypes};
	std::vector<MPI_Aint> _sendDisplacements;
	std::vector<MPI_Aint> _recvDisplacements;
	MPI_Comm _commDistGraph{MPI_COMM_NULL};

	MPI_Datatype _haloRowType;
	MPI_Datatype _haloColumnType;
	MPI_Datatype _haloCornerType{MPI_CHAR};

  public:
	Communicator() = default;
	Communicator(const MpiEnvironment& env, const Size& gridSize,
	             const Size& tileSize);
	~Communicator();
	void swap(Communicator& first, Communicator& second);

	Communicator(Communicator&) = delete;
	Communicator& operator=(Communicator&) = delete;
	Communicator(Communicator&& other) noexcept;
	Communicator& operator=(Communicator&& other) noexcept;
	void Communicate(gsl::multi_span<State, -1, -1>& model);

	class MpiRequest {
		MPI_Request _req{MPI_REQUEST_NULL};
		bool finished{};

	  public:
		MpiRequest(MPI_Request req);
		void Wait();
		~MpiRequest();
	MpiRequest AsyncCommunicate(gsl::multi_span<State, -1, -1>& model);