#ifndef MPI_SIMULATION_HPP
#define MPI_SIMULATION_HPP


#include <mpi.h>
#include <Simulation.hpp>
#include <BarnesHutTree.hpp>
#include <cstddef>
#include <vector>

namespace nbody {
	struct SendStore {
		Body* bodies;
		MPI_Request request;
		int size;
	};

	//MPI simulation
	class MpiSimulation : public Simulation {
	protected:
		MPI_Datatype bodyType;
		MPI_Datatype boxType;
		std::vector<Box> domains;
		Box overallDomain;
		std::vector<SendStore> sendStores;

		virtual SendStore* availableSendStore(int numElems);
		virtual void send(std::vector<Body> bodies, int target);
		virtual int recv(std::vector<Body>& bodies, int source);
	public:
		MpiSimulation();
		virtual ~MpiSimulation();
		virtual void initialize(const std::string& inputFile);
		virtual void cleanup();
		virtual int getNumberOfProcesses() const;
		virtual int getProcessId() const;
		virtual bool stateCorrect();
		virtual void distributeBodies();
		virtual void distributeDomains(const std::vector<Body>& localBodies);
		virtual void distributeDomains(const Box& localDomain);
		virtual void distributeDomains();
		virtual void distributeLETs();
		virtual void rebuildTree();
		virtual void buildTree();
		virtual void runStep();
		virtual MPI_Datatype* getDatatype();
	};
}

#endif

