From 4a8d4aab40792d8194c00150408ce343796a7cc9 Mon Sep 17 00:00:00 2001 From: Paul Heinzlreiter Date: Mon, 26 Sep 2016 19:10:04 +0200 Subject: [PATCH] * debugging --- bh_tree_mpi/datastructures/Body.cpp | 45 +++++- bh_tree_mpi/datastructures/Body.hpp | 2 + bh_tree_mpi/datastructures/Tree.cpp | 27 ---- bh_tree_mpi/datastructures/Tree.hpp | 2 - bh_tree_mpi/main.cpp | 185 ++++++----------------- bh_tree_mpi/simulation/MpiSimulation.cpp | 18 ++- bh_tree_mpi/simulation/Simulation.cpp | 2 +- 7 files changed, 101 insertions(+), 180 deletions(-) diff --git a/bh_tree_mpi/datastructures/Body.cpp b/bh_tree_mpi/datastructures/Body.cpp index 0ef843d..ab4ca82 100644 --- a/bh_tree_mpi/datastructures/Body.cpp +++ b/bh_tree_mpi/datastructures/Body.cpp @@ -1,6 +1,8 @@ #include "Body.hpp" #include #include +#include +#include #include #include #include @@ -90,14 +92,45 @@ namespace nbody { } void printBody(int parallelId, Body body) { - cout << parallelId << " Id: " << body.id << endl; - cout << parallelId << " Position: " << body.position[0] << " " << body.position[1] << " " << body.position[2] << endl; - cout << parallelId << " Velocity: " << body.velocity[0] << " " << body.velocity[1] << " " << body.velocity[2] << endl; - cout << parallelId << " Acceleration: " << body.acceleration[0] << " " << body.acceleration[1] << " " << body.acceleration[2] << endl; - cout << parallelId << " Mass: " << body.mass << endl; - cout << parallelId << " Refinement: " << body.refinement << endl << endl; + cout << parallelId << " " << body.id << " Position: " << body.position[0] << " " << body.position[1] << " " << body.position[2] << endl; + cout << parallelId << " " << body.id << " Velocity: " << body.velocity[0] << " " << body.velocity[1] << " " << body.velocity[2] << endl; + cout << parallelId << " " << body.id << " Acceleration: " << body.acceleration[0] << " " << body.acceleration[1] << " " << body.acceleration[2] << endl; + cout << parallelId << " " << body.id << " Mass: " << body.mass << endl; + cout << parallelId << " " << body.id << " Refinement: " << body.refinement << endl << endl; } + vector dubinskiParse(string filename) { + vector result; + string line; + ifstream infile(filename.c_str(), ifstream::in); + double mass, px, py, pz, vx, vy, vz; + unsigned long id = 0; + + while (std::getline(infile, line)) { + istringstream iss(line); + Body b; + + vx = vy = vz = 0.0; + iss >> mass >> px >> py >> pz >> vx >> vy >> vz; + b.position[0] = px; + b.position[1] = py; + b.position[2] = pz; + b.velocity[0] = vx; + b.velocity[1] = vy; + b.velocity[2] = vz; + b.acceleration[0] = 0.0; + b.acceleration[1] = 0.0; + b.acceleration[2] = 0.0; + b.refinement = 0; + b.mass = mass; + b.id = id++; + result.push_back(b); + } + infile.close(); + return result; + } + + void print(int parallelId, vector bodies) { for (vector::iterator it = bodies.begin(); it != bodies.end(); it++) { printBody(parallelId, *it); diff --git a/bh_tree_mpi/datastructures/Body.hpp b/bh_tree_mpi/datastructures/Body.hpp index 34bf2b3..e52102a 100644 --- a/bh_tree_mpi/datastructures/Body.hpp +++ b/bh_tree_mpi/datastructures/Body.hpp @@ -2,6 +2,7 @@ #define BODY_HPP #include +#include namespace nbody { using namespace std; @@ -31,6 +32,7 @@ namespace nbody { void printBody(int parallelId, Body body); void print(int parallelId, vector bodies); bool valid(vector bodies); + vector dubinskiParse(string filename); /* diff --git a/bh_tree_mpi/datastructures/Tree.cpp b/bh_tree_mpi/datastructures/Tree.cpp index a04a2a2..ecd3a15 100644 --- a/bh_tree_mpi/datastructures/Tree.cpp +++ b/bh_tree_mpi/datastructures/Tree.cpp @@ -89,33 +89,6 @@ namespace nbody { } } - vector Tree::dubinskiParse(string filename) { - vector result; - string line; - ifstream infile(filename.c_str(), ifstream::in); - double mass, px, py, pz, vx, vy, vz; - unsigned long id = 0; - - while (std::getline(infile, line)) { - istringstream iss(line); - Body b; - - vx = vy = vz = 0.0; - iss >> mass >> px >> py >> pz >> vx >> vy >> vz; - b.position[0] = px; - b.position[1] = py; - b.position[2] = pz; - b.velocity[0] = vx; - b.velocity[1] = vy; - b.velocity[2] = vz; - b.mass = mass; - b.id = id++; - result.push_back(b); - } - infile.close(); - return result; - } - vector Tree::extractLocalBodies() { vector result; diff --git a/bh_tree_mpi/datastructures/Tree.hpp b/bh_tree_mpi/datastructures/Tree.hpp index fd57265..2d695c2 100644 --- a/bh_tree_mpi/datastructures/Tree.hpp +++ b/bh_tree_mpi/datastructures/Tree.hpp @@ -21,8 +21,6 @@ namespace nbody { unsigned int maxLeafBodies; int parallelId; public: - static vector dubinskiParse(string filename); - Tree(int parallelId); virtual ~Tree(); virtual void clean(); diff --git a/bh_tree_mpi/main.cpp b/bh_tree_mpi/main.cpp index 6a4d384..7f3618e 100644 --- a/bh_tree_mpi/main.cpp +++ b/bh_tree_mpi/main.cpp @@ -5,94 +5,10 @@ #include #include #include -//#include "datastructures/Body.hpp" +#include "datastructures/Body.hpp" using namespace std; - -class Car { -public: - int shifts; - int topSpeed; - double pos[3]; -}; - -class Body { -public: - unsigned long id; - double position[3]; - double velocity[3]; - double acceleration[3]; - double mass; - int refinement; - - Body(); - Body(double positionX, double positionY, double positionZ); - Body(double positionX, double positionY, double positionZ, double velocityX, double velocityY, double velocityZ); - Body(double positionX, double positionY, double positionZ, double weight); - Body(double positionX, double positionY, double positionZ, double velocityX, double velocityY, double velocityZ, double weight); - virtual ~Body(); -}; - -Body::Body(double positionX, double positionY, double positionZ) { - this->id = ULONG_MAX; - for (int i = 0; i < 3; i++) { - this->velocity[i] = 0.0; - this->acceleration[i] = 0.0; - } - this->position[0] = positionX; - this->position[1] = positionY; - this->position[2] = positionZ; - this->mass = 1.0; - this->refinement = 0; -} - -Body::Body(double positionX, double positionY, double positionZ, double velocityX, double velocityY, double velocityZ) { - this->id = ULONG_MAX; - for (int i = 0; i < 3; i++) { - this->acceleration[i] = 0.0; - } - this->position[0] = positionX; - this->position[1] = positionY; - this->position[2] = positionZ; - this->velocity[0] = velocityX; - this->velocity[1] = velocityY; - this->velocity[2] = velocityZ; - this->mass = 1.0; - this->refinement = 0; -} - -Body::Body(double positionX, double positionY, double positionZ, double weight) { - this->id = ULONG_MAX; - for (int i = 0; i < 3; i++) { - this->velocity[i] = 0.0; - this->acceleration[i] = 0.0; - } - this->position[0] = positionX; - this->position[1] = positionY; - this->position[2] = positionZ; - this->mass = weight; - this->refinement = 0; -} - -Body::Body(double positionX, double positionY, double positionZ, double velocityX, double velocityY, double velocityZ, double weight) { - this->id = ULONG_MAX; - for (int i = 0; i < 3; i++) { - this->acceleration[i] = 0.0; - } - this->position[0] = positionX; - this->position[1] = positionY; - this->position[2] = positionZ; - this->velocity[0] = velocityX; - this->velocity[1] = velocityY; - this->velocity[2] = velocityZ; - this->mass = weight; - this->refinement = 0; -} - -Body::~Body() { - -} - +using namespace nbody; int main(int argc, char* argv[]) { const int tag = 13; @@ -105,21 +21,30 @@ int main(int argc, char* argv[]) { fprintf(stderr,"Requires at least two processes.\n"); } - /* create a type for struct car */ - const int nitems = 3; - int blocklengths[3] = {1, 1, 3}; - MPI_Datatype types[3] = {MPI_INT, MPI_INT, MPI_DOUBLE}; - MPI_Datatype mpi_car_type; - MPI_Aint offsets[3]; - - offsets[0] = offsetof(Car, shifts); - offsets[1] = offsetof(Car, topSpeed); - offsets[2] = offsetof(Car, pos); - - MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_car_type); - MPI_Type_commit(&mpi_car_type); - - + int bodyBlocklengths[6] = {1, 3, 3, 3, 1, 1}; + MPI_Datatype bodyDatatypes[6] = {MPI_UNSIGNED_LONG, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_INT}; + MPI_Aint bodyOffsets[6]; + MPI_Datatype bodyType; + bodyOffsets[0] = offsetof(Body, id); + bodyOffsets[1] = offsetof(Body, position); + bodyOffsets[2] = offsetof(Body, velocity); + bodyOffsets[3] = offsetof(Body, acceleration); + bodyOffsets[4] = offsetof(Body, mass); + bodyOffsets[5] = offsetof(Body, refinement); + MPI_Type_create_struct(6, bodyBlocklengths, bodyOffsets, bodyDatatypes, &bodyType); + MPI_Type_commit(&bodyType); + + /* + int boxBlocklengths[2] = {3, 3}; + MPI_Datatype boxDatatypes[2] = {MPI_DOUBLE, MPI_DOUBLE}; + MPI_Aint boxOffsets[2]; + MPI_Datatype boxType; + bodyOffsets[0] = offsetof(Body, id); + bodyOffsets[1] = offsetof(Body, position); + MPI_Type_create_struct(2, boxBlocklengths, boxOffsets, boxDatatypes, &boxType); + MPI_Type_commit(&boxType); + */ +/* const int bitems = 6; int bblocklengths[6] = {1, 3, 3, 3, 1, 1}; MPI_Datatype btypes[6] = {MPI_UNSIGNED_LONG, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_INT}; @@ -135,32 +60,24 @@ int main(int argc, char* argv[]) { MPI_Type_create_struct(bitems, bblocklengths, boffsets, btypes, &mpi_body_type); MPI_Type_commit(&mpi_body_type); - + */ MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (rank == 0) { - vector send; - send.push_back(Body()); - send.push_back(Body()); - - send[0].id = 1; - send[1].id = 2; - send[0].mass = 3.0; - send[1].mass = 4.0; - send[0].refinement = 5; - send[1].refinement = 6; - for (int i = 0; i < 3; i++) { - send[0].position[i] = 50 + i * 10 + 0; - send[1].position[i] = 50 + i * 10 + 1; - send[0].velocity[i] = 50 + i * 10 + 2; - send[1].velocity[i] = 50 + i * 10 + 3; - send[0].acceleration[i] = 50 + i * 10 + 4; - send[1].acceleration[i] = 50 + i * 10 + 5; + vector send = dubinskiParse(argv[1]); + + + for (int i = 0; i < send.size(); i++) { + printBody(0, send[i]); } + + const int dest = 1; - MPI_Send(&send[0], 2, mpi_body_type, dest, tag, MPI_COMM_WORLD); + int bodySize = send.size(); + MPI_Send(&bodySize, 1, MPI_INT, dest, tag, MPI_COMM_WORLD); + MPI_Send(&send[0], bodySize, bodyType, dest, tag, MPI_COMM_WORLD); /* vector send; @@ -189,28 +106,16 @@ int main(int argc, char* argv[]) { const int src=0; vector recv; + int bodySize; - recv.reserve(2); - MPI_Recv(&recv[0], 2, mpi_body_type, src, tag, MPI_COMM_WORLD, &status); - - cout << recv[0].id << endl; - cout << recv[1].id << endl; - cout << recv[0].mass << endl; - cout << recv[1].mass << endl; - cout << recv[0].refinement << endl; - cout << recv[1].refinement << endl; - - for (int i = 0; i < 3; i++) { - cout << recv[0].position[i] << endl; - cout << recv[1].position[i] << endl; - cout << recv[0].velocity[i] << endl; - cout << recv[1].velocity[i] << endl; - cout << recv[0].acceleration[i] << endl; - cout << recv[1].acceleration[i] << endl; + MPI_Recv(&bodySize, 1, MPI_INT, src, tag, MPI_COMM_WORLD, &status); + recv.reserve(bodySize); + MPI_Recv(&recv[0], bodySize, bodyType, src, tag, MPI_COMM_WORLD, &status); + for (int i = 0; i < bodySize; i++) { + printBody(1, recv[i]); } - /* MPI_Status status; const int src=0; @@ -226,9 +131,7 @@ int main(int argc, char* argv[]) { */ } - - MPI_Type_free(&mpi_car_type); - MPI_Type_free(&mpi_body_type); + MPI_Type_free(&bodyType); MPI_Finalize(); return 0; } diff --git a/bh_tree_mpi/simulation/MpiSimulation.cpp b/bh_tree_mpi/simulation/MpiSimulation.cpp index 814b647..7b963ef 100644 --- a/bh_tree_mpi/simulation/MpiSimulation.cpp +++ b/bh_tree_mpi/simulation/MpiSimulation.cpp @@ -101,7 +101,8 @@ namespace nbody { bb = nodes.front().getBB(); extendForBodies(bb, this->bodies); nodes.front().setBB(bb); - printBB(0, bb); + //printBB(0, bb); + while (nodes.size() < (unsigned int) this->parallelSize) { int mostBodiesIndex = 0; @@ -125,15 +126,27 @@ namespace nbody { } this->bodies = nodes[0].getBodies(); + for (unsigned int i = 0; i < nodes.size(); i++) { + for (unsigned int j = 0; j < nodes[i].getBodies().size(); i++) { + printBody(i, nodes[i].getBodies()[j]); + } + + } + + + /* for (unsigned int i = 1; i < nodes.size(); i++) { this->comms.push_back(MpiBodyComm(&this->bodyType)); this->comms.back().sendUnblocking(i, nodes[i].getBodies()); } + */ } else { + /* this->comms.push_back(MpiBodyComm(&this->bodyType)); this->comms[0].recvBlocking(0, this->bodies); + */ } - //Body::print(this->parallelRank, this->bodies); + //print(this->parallelRank, this->bodies); } void MpiSimulation::distributeDomains(vector localBodies) { @@ -169,7 +182,6 @@ namespace nbody { printBB(this->parallelRank, this->domains[i]); vector refinements = this->tree->copyRefinements(this->domains[i]); - //cout << "ref " << refinements.size() << endl; vector::iterator it = this->comms.begin(); diff --git a/bh_tree_mpi/simulation/Simulation.cpp b/bh_tree_mpi/simulation/Simulation.cpp index 8b1aaa0..1709ca8 100644 --- a/bh_tree_mpi/simulation/Simulation.cpp +++ b/bh_tree_mpi/simulation/Simulation.cpp @@ -16,7 +16,7 @@ namespace nbody { bool Simulation::readInputData(string filename) { if (this->parallelRank == 0) { - this->bodies = Tree::dubinskiParse(filename); + this->bodies = dubinskiParse(filename); if (this->bodies.empty()) { return false; } -- GitLab