From 63b0176094e84b14fb8012cea6a6e2afd46f804f Mon Sep 17 00:00:00 2001 From: Paul Heinzlreiter Date: Wed, 6 Jul 2016 12:33:07 +0200 Subject: [PATCH] * working test communication --- bh_tree_mpi/main.cpp | 80 +------------------ bh_tree_mpi/parallelization/MpiBodyComm.cpp | 35 +++++--- bh_tree_mpi/parallelization/MpiBodyComm.hpp | 3 +- bh_tree_mpi/parallelization/MpiSimulation.cpp | 21 +++++ bh_tree_mpi/parallelization/MpiSimulation.hpp | 3 + 5 files changed, 53 insertions(+), 89 deletions(-) diff --git a/bh_tree_mpi/main.cpp b/bh_tree_mpi/main.cpp index 065ba56..888063d 100644 --- a/bh_tree_mpi/main.cpp +++ b/bh_tree_mpi/main.cpp @@ -6,85 +6,7 @@ using namespace nbody; using namespace std; int main(int argc, char* argv[]) { - if (argc == 2) { - MpiSimulation simulation(argc, argv); - MpiBodyComm comm; - vector bodies; + MpiSimulation simulation(argc, argv); - if (simulation.getProcessId() == 0) { - bodies = Tree::dubinskiParse(argv[1]); - comm.sendBlocking(1, bodies); - } else { - comm.recvBlocking(0, bodies); - } - - /* - MPI::Datatype Btype; - MPI::Datatype type[3] = {MPI::DOUBLE, MPI::DOUBLE, MPI::DOUBLE}; - int blocks[3] = {3, 3, 1}; - MPI::Aint disp[3]; - Body tb; - - if (simulation.getProcessId() == 0) { - bodies = Tree::dubinskiParse(argv[1]); - } else { - bodies.reserve(128); - } - disp[0] = MPI::Get_address(&(tb.position[0])); - disp[1] = MPI::Get_address(&(tb.velocity[0])); - disp[2] = MPI::Get_address(&(tb.mass)); - - Btype = Btype.Create_struct(3, blocks, disp, type); - Btype.Commit(); - - Btype.Free(); - - disp[0] = MPI::Get_address(bodies[0].position); - disp[1] = MPI::Get_address(bodies[0].velocity); - disp[2] = MPI::Get_address(&bodies[0].mass); - Btype = Btype.Create_struct(3, blocks, disp, type); - Btype.Commit(); - //MPI::COMM_WORLD.Bcast(MPI::BOTTOM, 128, Btype, 0); - - if (simulation.getProcessId() == 0) { - MPI::COMM_WORLD.Send(MPI::BOTTOM, 128, Btype, 1, 0); - } else if (simulation.getProcessId() == 1) { - MPI::COMM_WORLD.Recv(MPI::BOTTOM, 128, Btype, 0, 0); - } - - Btype.Free(); - */ - - - /* - if (simulation.getProcessId() == 0) { - BarnesHutTree tree; - vector bodies = Tree::dubinskiParse(argv[1]); - - cout << bodies.size() << endl; - tree.build(bodies); - if (!tree.isCorrect()) { - cout << "Tree not correct" << endl; - return -1; - } - tree.print(); - tree.computeForces(); - tree.moveBodies(); - tree.rebuildTree(); - if (!tree.isCorrect()) { - cout << "Tree not correct" << endl; - return -1; - } - tree.print(); - comm.sendBlocking(1, bodies); - } else if (simulation.getProcessId() == 1) { - vector recved; - - comm.recvBlocking(0, recved); - } - */ - } else { - std::cout << "You need to specify the body input file." << endl; - } return 0; } diff --git a/bh_tree_mpi/parallelization/MpiBodyComm.cpp b/bh_tree_mpi/parallelization/MpiBodyComm.cpp index 8b51947..c89bc6d 100644 --- a/bh_tree_mpi/parallelization/MpiBodyComm.cpp +++ b/bh_tree_mpi/parallelization/MpiBodyComm.cpp @@ -6,20 +6,14 @@ namespace nbody { MpiBodyComm::MpiBodyComm() { //setup initial data type and initialize request for tracking status of unblocking send - this->buffer.push_back(Body()); - this->buffer.back().mass = 0.0; this->datatypes[0] = MPI::DOUBLE; this->datatypes[1] = MPI::DOUBLE; this->datatypes[2] = MPI::DOUBLE; this->blocklengths[0] = 3; this->blocklengths[1] = 3; this->blocklengths[2] = 1; - this->displacements[0] = MPI::Get_address(&(this->buffer[0].position[0])); - this->displacements[1] = MPI::Get_address(&(this->buffer[0].velocity[0])); - this->displacements[2] = MPI::Get_address(&(this->buffer[0].mass)); - this->commBodyType = this->commBodyType.Create_struct(3, this->blocklengths, this->displacements, this->datatypes); - this->commBodyType.Commit(); this->request = MPI::REQUEST_NULL; + this->commBodyType = MPI::DATATYPE_NULL; } MpiBodyComm::~MpiBodyComm() { @@ -28,12 +22,16 @@ namespace nbody { this->request.Wait(); this->request.Free(); } - this->commBodyType.Free(); + if (this->commBodyType != MPI::DATATYPE_NULL) { + this->commBodyType.Free(); + } } void MpiBodyComm::setupDatatype(vector bodies) { //re-setup data type for current input data's memory locations - this->commBodyType.Free(); + if (this->commBodyType != MPI::DATATYPE_NULL) { + this->commBodyType.Free(); + } this->displacements[0] = MPI::Get_address(bodies[0].position); this->displacements[1] = MPI::Get_address(bodies[0].velocity); this->displacements[2] = MPI::Get_address(&(bodies[0].mass)); @@ -144,4 +142,23 @@ namespace nbody { return MPI::COMM_WORLD.Iprobe(source, 0); } + void MpiBodyComm::testing(vector& data, int myRank) { + MPI::Datatype btype = MPI::DATATYPE_NULL; + int bodySize = data.size(); + + MPI::COMM_WORLD.Bcast(&bodySize, 1, MPI::INT, 0); + if (myRank != 0) { + data.clear(); + data.reserve(bodySize); + } + this->displacements[0] = MPI::Get_address(data[0].position); + this->displacements[1] = MPI::Get_address(data[0].velocity); + this->displacements[2] = MPI::Get_address(&(data[0].mass)); + btype = btype.Create_struct(3, this->blocklengths, this->displacements, this->datatypes); + btype.Commit(); + + MPI::COMM_WORLD.Bcast(MPI::BOTTOM, bodySize, btype, 0); + + btype.Free(); + } } diff --git a/bh_tree_mpi/parallelization/MpiBodyComm.hpp b/bh_tree_mpi/parallelization/MpiBodyComm.hpp index 7ad610d..4f993a0 100644 --- a/bh_tree_mpi/parallelization/MpiBodyComm.hpp +++ b/bh_tree_mpi/parallelization/MpiBodyComm.hpp @@ -14,9 +14,9 @@ namespace nbody { MPI::Datatype datatypes[3]; int blocklengths[3]; MPI::Aint displacements[3]; - vector buffer; MPI::Status status; MPI::Request request; + vector buffer; virtual void setupDatatype(vector bodies); public: @@ -30,6 +30,7 @@ namespace nbody { virtual bool recvUnblockingAnySource(int& source, vector& bodies); virtual bool sendReady(); virtual bool recvReady(int source); + virtual void testing(vector& data, int myRank); }; } diff --git a/bh_tree_mpi/parallelization/MpiSimulation.cpp b/bh_tree_mpi/parallelization/MpiSimulation.cpp index 66c1774..ed7b64f 100644 --- a/bh_tree_mpi/parallelization/MpiSimulation.cpp +++ b/bh_tree_mpi/parallelization/MpiSimulation.cpp @@ -1,11 +1,32 @@ #include "../datastructures/Body.hpp" #include "MpiSimulation.hpp" +#include "../datastructures/Tree.hpp" namespace nbody { MpiSimulation::MpiSimulation(int& argc, char**& argv) { MPI::Init(argc, argv); this->mpiSize = MPI::COMM_WORLD.Get_size(); this->mpiRank = MPI::COMM_WORLD.Get_rank(); + this->correctState = true; + + if (this->mpiRank == 0) { + if (argc != 2) { + this->correctState = false; + } else { + this->bodies = Tree::dubinskiParse(string(argv[1])); + if (bodies.empty()) { + this->correctState = false; + } + } + } + MPI::COMM_WORLD.Bcast(&this->correctState, 1, MPI::BOOL, 0); + MpiBodyComm comm; + + comm.testing(this->bodies, this->mpiRank); + } + + bool MpiSimulation::stateCorrect() { + return this->correctState; } MpiSimulation::~MpiSimulation() { diff --git a/bh_tree_mpi/parallelization/MpiSimulation.hpp b/bh_tree_mpi/parallelization/MpiSimulation.hpp index 3c3bd8d..33be85e 100644 --- a/bh_tree_mpi/parallelization/MpiSimulation.hpp +++ b/bh_tree_mpi/parallelization/MpiSimulation.hpp @@ -14,12 +14,15 @@ namespace nbody { int mpiSize; int mpiRank; vector comms; + bool correctState; + vector bodies; public: MpiSimulation(int& argc, char**& argv); virtual ~MpiSimulation(); virtual int getNumberOfProcesses(); virtual int getProcessId(); + virtual bool stateCorrect(); }; } -- GitLab