From 0d457750f38631f112fc4fa8fe2e8a5b2594fd96 Mon Sep 17 00:00:00 2001 From: Thomas Steinreiter Date: Mon, 21 Nov 2016 11:29:32 +0100 Subject: [PATCH] refactored initializing and ctors --- .../src/datastructures/BarnesHutTree.cpp | 4 +- bhtree_mpi/src/datastructures/Body.cpp | 38 +++++++++++-------- bhtree_mpi/src/datastructures/Body.hpp | 16 ++++++-- bhtree_mpi/src/datastructures/Box.cpp | 6 --- bhtree_mpi/src/datastructures/Box.hpp | 13 +++++-- bhtree_mpi/src/datastructures/Node.cpp | 11 +----- bhtree_mpi/src/datastructures/Node.hpp | 20 +++++----- bhtree_mpi/src/datastructures/Tree.cpp | 11 ++---- bhtree_mpi/src/datastructures/Tree.hpp | 6 +-- bhtree_mpi/src/simulation/MpiSimulation.cpp | 6 +-- bhtree_mpi/src/simulation/MpiSimulation.hpp | 4 +- bhtree_mpi/src/simulation/Simulation.cpp | 7 ---- bhtree_mpi/src/simulation/Simulation.hpp | 9 ++--- 13 files changed, 71 insertions(+), 80 deletions(-) diff --git a/bhtree_mpi/src/datastructures/BarnesHutTree.cpp b/bhtree_mpi/src/datastructures/BarnesHutTree.cpp index 1bb0bf4..9b33a66 100644 --- a/bhtree_mpi/src/datastructures/BarnesHutTree.cpp +++ b/bhtree_mpi/src/datastructures/BarnesHutTree.cpp @@ -5,9 +5,7 @@ #include namespace nbody { - BarnesHutTree::BarnesHutTree(std::size_t parallelId) : Tree(parallelId) { - } - + BarnesHutTree::BarnesHutTree(std::size_t parallelId) : Tree(parallelId) {} //determine octree subboxes std::vector BarnesHutTree::splitBB(const Node* node) { diff --git a/bhtree_mpi/src/datastructures/Body.cpp b/bhtree_mpi/src/datastructures/Body.cpp index 544ac5a..aeb6dca 100644 --- a/bhtree_mpi/src/datastructures/Body.cpp +++ b/bhtree_mpi/src/datastructures/Body.cpp @@ -7,6 +7,19 @@ #include namespace nbody { + Body::Body(const std::array& _position, + const std::array& _velocity, + const std::array& _acceleration, + double _mass, + bool _refinement, + std::size_t _id) : + id(_id), + position(_position), + velocity(_velocity), + acceleration(_acceleration), + mass(_mass), + refinement(_refinement) {} + void Body::resetAcceleration() { std::fill(std::begin(acceleration), std::end(acceleration), 0.0); } @@ -88,28 +101,23 @@ namespace nbody { std::vector result; std::string line; std::ifstream infile(filename.c_str(), std::ifstream::in); - double mass, px, py, pz, vx, vy, vz; - std::size_t id = 0; while (std::getline(infile, line)) { std::istringstream iss(line); - Body b; + double mass, px, py, pz, vx, vy, vz; + std::size_t id = 0; //not all input files have velocity, so initialize properly 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 = false; - b.mass = mass; - b.id = id++; + Body b{ + {{px, py, pz}}, + {{vx, vy, vz}}, + {{0.0, 0.0, 0.0}}, + mass, + false, + id++ + }; result.push_back(b); } return result; diff --git a/bhtree_mpi/src/datastructures/Body.hpp b/bhtree_mpi/src/datastructures/Body.hpp index 618602d..d1c9449 100644 --- a/bhtree_mpi/src/datastructures/Body.hpp +++ b/bhtree_mpi/src/datastructures/Body.hpp @@ -13,13 +13,21 @@ namespace nbody { std::array dv{}; }; - struct Body { //TODO(steinret): ctor - std::size_t id{}; + struct Body { + Body() = default; + Body(const std::array& _position, + const std::array& _velocity, + const std::array& _acceleration, + double _mass, + bool _refinement, + std::size_t _id); + + std::size_t id{0}; std::array position{}; std::array velocity{}; std::array acceleration{}; - double mass{}; - bool refinement{}; + double mass{0.0}; + bool refinement{false}; void resetAcceleration(); Derivative evaluate(double dt, const Derivative& d) const; diff --git a/bhtree_mpi/src/datastructures/Box.cpp b/bhtree_mpi/src/datastructures/Box.cpp index 5467f29..2181457 100644 --- a/bhtree_mpi/src/datastructures/Box.cpp +++ b/bhtree_mpi/src/datastructures/Box.cpp @@ -1,15 +1,9 @@ #include #include -#include #include #include "Box.hpp" namespace nbody { - Box::Box() { - std::fill(std::begin(min), std::end(min), std::numeric_limits::max()); - std::fill(std::begin(max), std::end(max), std::numeric_limits::min()); - } - //extend box to form cube void Box::extendToCube() { std::size_t longestSide = std::numeric_limits::max(); diff --git a/bhtree_mpi/src/datastructures/Box.hpp b/bhtree_mpi/src/datastructures/Box.hpp index 5f83574..e0c3dd3 100644 --- a/bhtree_mpi/src/datastructures/Box.hpp +++ b/bhtree_mpi/src/datastructures/Box.hpp @@ -3,14 +3,21 @@ #include #include +#include #include namespace nbody { struct Box { - std::array min{}; - std::array max{}; + using flt_lim = std::numeric_limits; + std::array min{ { + flt_lim::max(), + flt_lim::max(), + flt_lim::max() }}; + std::array max{{ + flt_lim::min(), + flt_lim::min(), + flt_lim::min() }}; - Box(); void extendToCube(); void extendForBodies(const std::vector& bodies); std::vector extractBodies(std::vector& bodies) const; diff --git a/bhtree_mpi/src/datastructures/Node.cpp b/bhtree_mpi/src/datastructures/Node.cpp index 2ea6253..661e05c 100644 --- a/bhtree_mpi/src/datastructures/Node.cpp +++ b/bhtree_mpi/src/datastructures/Node.cpp @@ -5,16 +5,7 @@ #include "Node.hpp" namespace nbody { - Node::Node(Tree* tree) { - this->afterSubtree = nullptr; - this->prev = this; - this->next = this; - this->leaf = true; - this->tree = tree; - this->prevSibling = nullptr; - this->nextSibling = nullptr; - this->parent = nullptr; - } + Node::Node(Tree* _tree):tree(_tree) {} Box Node::getBB() const { return this->bb; diff --git a/bhtree_mpi/src/datastructures/Node.hpp b/bhtree_mpi/src/datastructures/Node.hpp index fe5998a..f9193f0 100644 --- a/bhtree_mpi/src/datastructures/Node.hpp +++ b/bhtree_mpi/src/datastructures/Node.hpp @@ -16,17 +16,17 @@ namespace nbody { protected: Box bb; std::vector bodies; - Node* prev; - Node* next; - Node* nextSibling; - Node* prevSibling; - Node* parent; - Node* afterSubtree; - bool leaf; - Tree* tree; - Body representative{}; + Node* prev{ this }; + Node* next{ this }; + Node* nextSibling{ nullptr }; + Node* prevSibling{ nullptr }; + Node* parent{ nullptr }; + Node* afterSubtree{ nullptr }; + bool leaf{ true }; + Tree* tree{ nullptr }; + Body representative; public: - Node(Tree* tree); + Node(Tree* _tree); virtual ~Node() = default; virtual bool isSplitable() const; virtual void extendBBforBodies(); diff --git a/bhtree_mpi/src/datastructures/Tree.cpp b/bhtree_mpi/src/datastructures/Tree.cpp index 9c78ec9..e4e3426 100644 --- a/bhtree_mpi/src/datastructures/Tree.cpp +++ b/bhtree_mpi/src/datastructures/Tree.cpp @@ -8,13 +8,10 @@ #include "Simulation.hpp" namespace nbody { - Tree::Tree(std::size_t parallelId) { - //insert dummy root node - this->nodes = new Node(this); - this->maxLeafBodies = 16; - this->parallelId = parallelId; - this->simulation = nullptr; - } + Tree::Tree(std::size_t _parallelId): + nodes(new Node(this)), //insert dummy root node + maxLeafBodies(16), + parallelId(_parallelId) {} Tree::~Tree() { this->clean(); diff --git a/bhtree_mpi/src/datastructures/Tree.hpp b/bhtree_mpi/src/datastructures/Tree.hpp index 583a19b..a1fbd6b 100644 --- a/bhtree_mpi/src/datastructures/Tree.hpp +++ b/bhtree_mpi/src/datastructures/Tree.hpp @@ -20,9 +20,9 @@ namespace nbody { friend class ContinuousPthreadSimulation; protected: Node* nodes; - std::size_t maxLeafBodies; - std::size_t parallelId; - Simulation* simulation; + std::size_t maxLeafBodies{ 0 }; + std::size_t parallelId{ 0 }; + Simulation* simulation{ nullptr }; public: Tree(std::size_t parallelId); virtual ~Tree(); diff --git a/bhtree_mpi/src/simulation/MpiSimulation.cpp b/bhtree_mpi/src/simulation/MpiSimulation.cpp index 03aa38e..36f99b9 100644 --- a/bhtree_mpi/src/simulation/MpiSimulation.cpp +++ b/bhtree_mpi/src/simulation/MpiSimulation.cpp @@ -12,10 +12,6 @@ namespace nbody { MpiSimulation::MpiSimulation(const std::string& inputFile) { - this->tree = nullptr; - this->bodyType = MPI_DATATYPE_NULL; - this->boxType = MPI_DATATYPE_NULL; - //create MPI datatypes for bodies and domain boxes int bodyBlocklengths[6] = { 1, 3, 3, 3, 1, 1 }; MPI_Datatype bodyDatatypes[6] = { MPI_UINT64_T, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_CXX_BOOL }; @@ -94,7 +90,7 @@ namespace nbody { //do unblocking send auto store = std::make_unique(bodies); //TODO(steinret): ask ponweist if this could be done w/o unique_ptr - MPI_Isend(store->bodies.data(), bodySize, this->bodyType, target, 0, MPI_COMM_WORLD, &store->request); + MPI_Isend(store->bodies.data(), bodySize, this->bodyType, target, 0, MPI_COMM_WORLD, &store->request); // TODO(steinret): use BSend here sendStores.push_back(std::move(store)); } diff --git a/bhtree_mpi/src/simulation/MpiSimulation.hpp b/bhtree_mpi/src/simulation/MpiSimulation.hpp index cc85929..8e01dfe 100644 --- a/bhtree_mpi/src/simulation/MpiSimulation.hpp +++ b/bhtree_mpi/src/simulation/MpiSimulation.hpp @@ -19,8 +19,8 @@ namespace nbody { //MPI simulation class MpiSimulation : public Simulation { protected: - MPI_Datatype bodyType; - MPI_Datatype boxType; + MPI_Datatype bodyType{MPI_DATATYPE_NULL}; + MPI_Datatype boxType{MPI_DATATYPE_NULL}; std::vector domains; Box overallDomain; std::vector> sendStores; diff --git a/bhtree_mpi/src/simulation/Simulation.cpp b/bhtree_mpi/src/simulation/Simulation.cpp index 9000e08..02735c0 100644 --- a/bhtree_mpi/src/simulation/Simulation.cpp +++ b/bhtree_mpi/src/simulation/Simulation.cpp @@ -3,13 +3,6 @@ #include "Simulation.hpp" namespace nbody { - Simulation::Simulation() { - this->parallelRank = -1; - this->parallelSize = -1; - this->correctState = false; - this->tree = nullptr; - } - void Simulation::clearBodies() { this->bodies.clear(); } diff --git a/bhtree_mpi/src/simulation/Simulation.hpp b/bhtree_mpi/src/simulation/Simulation.hpp index 4120489..d92a2eb 100644 --- a/bhtree_mpi/src/simulation/Simulation.hpp +++ b/bhtree_mpi/src/simulation/Simulation.hpp @@ -9,13 +9,12 @@ namespace nbody { //simulation superclass class Simulation { protected: - int parallelSize; - int parallelRank; - bool correctState; + int parallelSize{ -1 }; + int parallelRank{ -1 }; + bool correctState{ false }; std::vector bodies; - BarnesHutTree* tree; + BarnesHutTree* tree{ nullptr }; public: - Simulation(); virtual void clearBodies(); virtual std::vector getBodies() const; virtual std::size_t getNumberOfProcesses() const = 0; -- GitLab