From b7fdd949a072ef938159c960ac99319c7336b865 Mon Sep 17 00:00:00 2001 From: Paul Heinzlreiter Date: Mon, 11 Jul 2016 12:30:36 +0200 Subject: [PATCH] * body distribution --- bh_tree_mpi/main.cpp | 1 + bh_tree_mpi/mpitest.cpp | 70 ++--------------- bh_tree_mpi/parallelization/MpiSimulation.cpp | 76 +++++++++++++++---- bh_tree_mpi/parallelization/MpiSimulation.hpp | 3 + 4 files changed, 72 insertions(+), 78 deletions(-) diff --git a/bh_tree_mpi/main.cpp b/bh_tree_mpi/main.cpp index 9accae8..6f205c7 100644 --- a/bh_tree_mpi/main.cpp +++ b/bh_tree_mpi/main.cpp @@ -10,6 +10,7 @@ int main(int argc, char* argv[]) { MPI::Init(argc, argv); MpiSimulation simulation(argc, argv); + simulation.distributeBodies(); simulation.cleanup(); MPI::Finalize(); diff --git a/bh_tree_mpi/mpitest.cpp b/bh_tree_mpi/mpitest.cpp index 20aa04c..120d3db 100644 --- a/bh_tree_mpi/mpitest.cpp +++ b/bh_tree_mpi/mpitest.cpp @@ -2,6 +2,8 @@ #include #include #include +#include "parallelization/MpiSimulation.hpp" +#include "parallelization/MpiBodyComm.hpp" #include "datastructures/Body.hpp" typedef struct _A { @@ -49,78 +51,22 @@ int main(int argc, char** argv) { b[0].print(); b[1].print(); cout << "---" << endl; + MPI::Request request; MPI::COMM_WORLD.Send(b, 2, btype, 1, 0); + request = MPI::COMM_WORLD.Isend(b, 2, btype, 1, 0); + request.Wait(); } else if (MPI::COMM_WORLD.Get_rank() == 1) { + MPI::COMM_WORLD.Recv(b, 2, btype, 0, 0); MPI::COMM_WORLD.Recv(b, 2, btype, 0, 0); b[0].print(); b[1].print(); } btype.Free(); - MPI::Finalize(); - /* - A a[4]; - int blocklengths[7] = {1, 3, 3, 3, 1, 2, 1}; - MPI::Datatype datatypes[7] = {MPI::LB, MPI::DOUBLE, MPI::DOUBLE, MPI::DOUBLE, MPI::DOUBLE, MPI::DOUBLE, MPI::UB}; - MPI::Aint displacements[7]; - MPI::Datatype restype = MPI::DATATYPE_NULL; - //MPI::Aint doublelb; - //MPI::Aint doubleex; - - MPI::Init(argc, argv); - //datatypes[1].Get_extent(doublelb, doubleex); - displacements[0] = MPI::Get_address(&(a[0])) - MPI::Get_address(&(a[0])); - displacements[1] = MPI::Get_address(&(a[0].a[0])) - MPI::Get_address(&(a[0])); - displacements[2] = MPI::Get_address(&(a[0].b[0])) - MPI::Get_address(&(a[0])); - displacements[3] = MPI::Get_address(&(a[0].leap[0])) - MPI::Get_address(&(a[0])); - displacements[4] = MPI::Get_address(&(a[0].c)) - MPI::Get_address(&(a[0])); - displacements[5] = MPI::Get_address(&(a[0].d[0])) - MPI::Get_address(&(a[0])); - displacements[6] = MPI::Get_address(&(a[1])) - MPI::Get_address(&(a[0])); - restype = restype.Create_struct(7, blocklengths, displacements, datatypes); - restype.Commit(); - srandom(1234); - if (MPI::COMM_WORLD.Get_rank() == 0) { - for (int j = 0; j < 4; j++) { - for (int i = 0; i < 4; i++) { - a[j].a[i] = (float) random() / (float) random(); - a[j].b[i] = (float) random() / (float) random(); - } - a[j].c = (float) random() / (float) random(); - } - - for (int j = 0; j < 4; j++) { - cout << "0: "; - for (int i = 0; i < 4; i++) { - cout << a[j].a[i] << " "; - } - cout << "| "; - for (int i = 0; i < 4; i++) { - cout << a[j].b[i] << " "; - } - cout << "| "; - cout << a[j].c << endl; - } - MPI::COMM_WORLD.Send(&a[0], 4, restype, 1, 0); - } else if (MPI::COMM_WORLD.Get_rank() == 1) { - MPI::COMM_WORLD.Recv(&a[0], 4, restype, 0, 0); + //MpiSimulation simulation(argc, argv); + //MpiBodyComm comm(simulation.getDatatype()); - for (int j = 0; j < 4; j++) { - cout << "1: "; - for (int i = 0; i < 4; i++) { - cout << a[j].a[i] << " "; - } - cout << "| "; - for (int i = 0; i < 4; i++) { - cout << a[j].b[i] << " "; - } - cout << "| "; - cout << a[j].c << endl; - } - - } - restype.Free(); MPI::Finalize(); - */ return 0; } diff --git a/bh_tree_mpi/parallelization/MpiSimulation.cpp b/bh_tree_mpi/parallelization/MpiSimulation.cpp index f8a372a..ddb3f1a 100644 --- a/bh_tree_mpi/parallelization/MpiSimulation.cpp +++ b/bh_tree_mpi/parallelization/MpiSimulation.cpp @@ -10,6 +10,27 @@ namespace nbody { using namespace std; MpiSimulation::MpiSimulation(int& argc, char**& argv) { + Body b[2]; + int blocklengths[7] = {1, 3, 3, 3, 1, 1, 1}; + MPI::Datatype datatypes[7] = {MPI::LB, MPI::DOUBLE, MPI::DOUBLE, MPI::DOUBLE, MPI::DOUBLE, MPI::BOOL, MPI::UB}; + MPI::Aint displacements[7]; + this->bodyType = MPI::DATATYPE_NULL; + + //MPI::Init(argc, argv); + displacements[0] = MPI::Get_address(&(b[0])) - MPI::Get_address(&(b[0])); + displacements[1] = MPI::Get_address(&(b[0].position[0])) - MPI::Get_address(&(b[0])); + displacements[2] = MPI::Get_address(&(b[0].velocity[0])) - MPI::Get_address(&(b[0])); + displacements[3] = MPI::Get_address(&(b[0].acceleration[0])) - MPI::Get_address(&(b[0])); + displacements[4] = MPI::Get_address(&(b[0].mass)) - MPI::Get_address(&(b[0])); + displacements[5] = MPI::Get_address(&(b[0].refinement)) - MPI::Get_address(&(b[0])); + displacements[6] = MPI::Get_address(&(b[1])) - MPI::Get_address(&(b[0])); + this->bodyType = this->bodyType.Create_struct(7, blocklengths, displacements, datatypes); + this->bodyType.Commit(); + this->mpiSize = MPI::COMM_WORLD.Get_size(); + this->mpiRank = MPI::COMM_WORLD.Get_rank(); + this->correctState = true; + + /* Body b[2]; //number of (array) elements in struct elements int blocklengths[7] = {1, 3, 3, 3, 1, 1, 1}; @@ -31,18 +52,14 @@ namespace nbody { displacements[6] = MPI::Get_address(&(b[1])) - MPI::Get_address(&(b[0])); this->bodyType = this->bodyType.Create_struct(7, blocklengths, displacements, datatypes); this->bodyType.Commit(); + */ - if (this->mpiRank == 0) { - if (argc != 2) { - this->correctState = false; - } else { - this->bodies = Tree::dubinskiParse(string(argv[1])); - if (this->bodies.empty()) { - this->correctState = false; - cerr << "Body file parsing failed" << endl; - } - } + if (argc == 2) { + this->correctState = this->readInputData(string(argv[1])); + } else { + this->correctState = false; } + MPI::COMM_WORLD.Bcast(&this->correctState, 1, MPI::BOOL, 0); if (!this->correctState) { cerr << "Error occurred: terminating ..." << endl; @@ -50,6 +67,21 @@ namespace nbody { MPI::Finalize(); exit(-1); } + if (this->mpiRank == 0) { + MPI::COMM_WORLD.Send(b, 2, this->bodyType, 1, 0); + } else if (this->mpiRank == 1) { + MPI::COMM_WORLD.Recv(b, 2, this->bodyType, 0, 0); + } + } + + bool MpiSimulation::readInputData(string filename) { + if (this->mpiRank == 0) { + this->bodies = Tree::dubinskiParse(filename); + if (this->bodies.empty()) { + return false; + } + } + return true; } void MpiSimulation::cleanup() { @@ -75,8 +107,11 @@ namespace nbody { return this->mpiRank; } + MPI::Datatype* MpiSimulation::getDatatype() { + return &this->bodyType; + } + void MpiSimulation::distributeBodies() { - this->comms.push_back(MpiBodyComm(&this->bodyType)); if (this->mpiRank == 0) { vector nodes; Box bb; @@ -111,19 +146,28 @@ namespace nbody { nodes.erase(nodes.begin() + mostBodiesIndex + 2); } this->bodies = nodes[0].getBodies(); - /* + + for (unsigned int i = 0; i < nodes.size(); i++) { + cout << i << ": " << nodes[i].getBodies().size() << endl; + } for (unsigned int i = 1; i < nodes.size(); i++) { - vector::iterator it = this->comms.begin(); + this->comms.push_back(MpiBodyComm(&this->bodyType)); + this->comms.back().sendBlocking(i, nodes[i].getBodies()); + /* + //vector::iterator it = this->comms.begin(); + - while (!it->sendUnblocking(i, nodes[i].getBodies()) && it != this->comms.end()) it++; + //while (!it->sendUnblocking(i, nodes[i].getBodies()) && it != this->comms.end()) it++; if (it == this->comms.end()) { this->comms.push_back(MpiBodyComm(&this->bodyType)); this->comms.back().sendUnblocking(i, nodes[i].getBodies()); } + it->sendBlocking(i, nodes[i].getBodies()); + */ } - */ } else { - //this->comms[0].recvBlocking(0, this->bodies); + this->comms.push_back(MpiBodyComm(&this->bodyType)); + this->comms[0].recvBlocking(0, this->bodies); } cout << this->mpiRank << ": SIZE: " << this->bodies.size() << endl; } diff --git a/bh_tree_mpi/parallelization/MpiSimulation.hpp b/bh_tree_mpi/parallelization/MpiSimulation.hpp index d5102dc..e170240 100644 --- a/bh_tree_mpi/parallelization/MpiSimulation.hpp +++ b/bh_tree_mpi/parallelization/MpiSimulation.hpp @@ -25,7 +25,10 @@ namespace nbody { virtual int getNumberOfProcesses(); virtual int getProcessId(); virtual bool stateCorrect(); + virtual bool readInputData(string filename); virtual void distributeBodies(); + virtual MPI::Datatype* getDatatype(); + }; } -- GitLab