From 00ad09e4de21f24c32b6a09465ab010dca338d3b Mon Sep 17 00:00:00 2001 From: Paul Heinzlreiter Date: Mon, 7 Nov 2016 14:03:11 +0100 Subject: [PATCH] * added fixes according to tsteinr comments --- bhtree_mpi/CMakeLists.txt | 5 +++ .../src/datastructures/BarnesHutTree.cpp | 10 +++--- bhtree_mpi/src/datastructures/Body.cpp | 33 +++++-------------- bhtree_mpi/src/datastructures/Body.hpp | 17 +++++----- bhtree_mpi/src/datastructures/Box.cpp | 28 +++++----------- bhtree_mpi/src/datastructures/Box.hpp | 7 ++-- bhtree_mpi/src/datastructures/CMakeLists.txt | 7 ++++ bhtree_mpi/src/datastructures/Node.cpp | 8 ++--- bhtree_mpi/src/datastructures/Tree.cpp | 2 +- bhtree_mpi/src/datastructures/Tree.hpp | 2 +- bhtree_mpi/src/simulation/MpiSimulation.cpp | 25 ++++++-------- bhtree_mpi/src/simulation/MpiSimulation.hpp | 4 +-- 12 files changed, 65 insertions(+), 83 deletions(-) diff --git a/bhtree_mpi/CMakeLists.txt b/bhtree_mpi/CMakeLists.txt index 2b78ca2..235a164 100644 --- a/bhtree_mpi/CMakeLists.txt +++ b/bhtree_mpi/CMakeLists.txt @@ -26,6 +26,11 @@ if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") endif() message("** Enabling '${NAME}'") +include(CheckCXXCompilerFlag) +CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) +set(CXX11 ${COMPILER_SUPPORTS_CXX11}) +set(CXX11_FLAGS -std=c++11) +set(CMAKE_CXX_FLAGS "-std=c++11") find_package(MPI REQUIRED) diff --git a/bhtree_mpi/src/datastructures/BarnesHutTree.cpp b/bhtree_mpi/src/datastructures/BarnesHutTree.cpp index 8f6315e..8cc66d5 100644 --- a/bhtree_mpi/src/datastructures/BarnesHutTree.cpp +++ b/bhtree_mpi/src/datastructures/BarnesHutTree.cpp @@ -49,7 +49,7 @@ namespace nbody { current->representative.position[j] += child->representative.position[j] * child->representative.mass; } child = child->nextSibling; - } while (child != NULL); + } while (child != nullptr); } for (int j = 0; j < 3; j++) { if (current->representative.mass > FLT_EPSILON) { @@ -74,8 +74,8 @@ namespace nbody { child->parent = current; child->bb = *it; child->bodies = copyBodies(*it, current->bodies); - child->nextSibling = NULL; - child->prevSibling = NULL; + child->nextSibling = nullptr; + child->prevSibling = nullptr; after->insertBefore(child); if (it != subboxes.begin()) { child->prev->nextSibling = child; @@ -143,10 +143,10 @@ namespace nbody { while (!current->leaf) { Node* child = current->next; - while (child != NULL && !contained(child->getBB(), it->position)) { + while (child != nullptr && !contained(child->getBB(), it->position)) { child = child->nextSibling; } - if (child != NULL) { + if (child != nullptr) { current = child; } current = child; diff --git a/bhtree_mpi/src/datastructures/Body.cpp b/bhtree_mpi/src/datastructures/Body.cpp index de6779c..23d4853 100644 --- a/bhtree_mpi/src/datastructures/Body.cpp +++ b/bhtree_mpi/src/datastructures/Body.cpp @@ -8,14 +8,13 @@ #include #include #include +#include namespace nbody { using namespace std; void resetAcceleration(Body& body) { - for (int i = 0; i < 3; i++) { - body.acceleration[i] = 0.0; - } + fill(body.acceleration.begin(), body.acceleration.end(), 0.0); } //helper method for intergration @@ -73,23 +72,13 @@ namespace nbody { } bool validDouble(double val) { - char buf[128]; - - sprintf(buf, "%f", val); - for (int i = 0; i < strlen(buf); i++) { - if (!((buf[i] >= '0' && buf[i] <= '9') || buf[i] == '.' || buf[i] == '-')) { - return false; - } - } - return true; + return !::isnan(val) && isfinite(val); } - bool valid(Body body) { - for (int i = 0; i < 3; i++) { - if (!validDouble(body.position[i])) return false; - if (!validDouble(body.velocity[i])) return false; - if (!validDouble(body.acceleration[i])) return false; - } + bool validBody(Body body) { + if (!all_of(body.position.begin(), body.position.end(), validDouble)) return false; + if (!all_of(body.velocity.begin(), body.velocity.end(), validDouble)) return false; + if (!all_of(body.acceleration.begin(), body.acceleration.end(), validDouble)) return false; if (!validDouble(body.mass)) return false; return body.mass >= 0.0; } @@ -131,7 +120,6 @@ namespace nbody { b.id = id++; result.push_back(b); } - infile.close(); return result; } @@ -143,11 +131,6 @@ namespace nbody { } bool valid(vector bodies) { - for (vector::iterator it = bodies.begin(); it != bodies.end(); it++) { - if (!valid(*it)) { - return false; - } - } - return true; + return all_of(bodies.begin(), bodies.end(), validBody); } } diff --git a/bhtree_mpi/src/datastructures/Body.hpp b/bhtree_mpi/src/datastructures/Body.hpp index 9f2b0fd..e0855a1 100644 --- a/bhtree_mpi/src/datastructures/Body.hpp +++ b/bhtree_mpi/src/datastructures/Body.hpp @@ -3,22 +3,23 @@ #include #include +#include namespace nbody { using namespace std; static const double timestep = 1.0; - typedef struct Derivative_ { - double dx[3]; - double dv[3]; + typedef struct DerivativeStruct { + array dx; + array dv; } Derivative; - typedef struct Body_ { + typedef struct BodyStruct { unsigned long id; - double position[3]; - double velocity[3]; - double acceleration[3]; + array position; + array velocity; + array acceleration; double mass; int refinement; } Body; @@ -27,7 +28,7 @@ namespace nbody { Derivative evaluate(Body body, double dt, Derivative d); void integrate(Body& body); void accumulateForceOntoBody(Body& to, Body from); - bool valid(Body body); + bool validBody(Body body); void printBody(int parallelId, Body body); void printBodies(int parallelId, vector bodies); bool valid(vector bodies); diff --git a/bhtree_mpi/src/datastructures/Box.cpp b/bhtree_mpi/src/datastructures/Box.cpp index 326bddf..c069432 100644 --- a/bhtree_mpi/src/datastructures/Box.cpp +++ b/bhtree_mpi/src/datastructures/Box.cpp @@ -1,16 +1,15 @@ #include #include #include +#include #include "Box.hpp" namespace nbody { using namespace std; void initBox(Box& box) { - for (int i = 0; i < 3; i++) { - box.min[i] = FLT_MAX; - box.max[i] = -FLT_MAX; - } + fill(box.min.begin(), box.min.end(), FLT_MAX); + fill(box.max.begin(), box.max.end(), FLT_MIN); } //extend box to form cube @@ -39,17 +38,10 @@ namespace nbody { //extend for bodies void extendForBodies(Box& box, vector bodies) { - if (bodies.empty()) { - return; - } for (vector::iterator it = bodies.begin(); it != bodies.end(); it++) { for (int i = 0; i < 3; i++) { - if (it->position[i] < box.min[i]) { - box.min[i] = it->position[i]; - } - if (it->position[i] > box.max[i]) { - box.max[i] = it->position[i]; - } + box.min[i] = min(it->position[i], box.min[i]); + box.max[i] = max(it->position[i], box.max[i]); } } } @@ -120,17 +112,15 @@ namespace nbody { } double maxSidelength(Box box) { - double max = 0.0; + double maxVal = 0.0; if (!isValid(box)) { return -1.0; } for (int i = 0; i < 3; i++) { - if (box.max[i] - box.min[i] > max) { - max = box.max[i] - box.min[i]; - } + maxVal = max(box.max[i] - box.min[i], maxVal); } - return max; + return maxVal; } bool isCorrectBox(Box box) { @@ -284,7 +274,7 @@ namespace nbody { } //check for position in box - bool contained(Box box, double* position) { + bool contained(Box box, array position) { if (!isValid(box)) { return false; } diff --git a/bhtree_mpi/src/datastructures/Box.hpp b/bhtree_mpi/src/datastructures/Box.hpp index dc64fb2..930ef65 100644 --- a/bhtree_mpi/src/datastructures/Box.hpp +++ b/bhtree_mpi/src/datastructures/Box.hpp @@ -2,14 +2,15 @@ #define BOX_HPP #include +#include #include namespace nbody { using namespace std; typedef struct _Box { - double min[3]; - double max[3]; + array min; + array max; } Box; void initBox(Box& box); @@ -29,7 +30,7 @@ namespace nbody { double distanceToBox(Box box1, Box box2); vector octreeSplit(Box box); vector splitLongestSide(Box box); - bool contained(Box box, double* position); + bool contained(Box box, array position); void extend(Box& box, Box extender); void extend(Box& box, Body extender); } diff --git a/bhtree_mpi/src/datastructures/CMakeLists.txt b/bhtree_mpi/src/datastructures/CMakeLists.txt index 68204ec..fd50bd6 100644 --- a/bhtree_mpi/src/datastructures/CMakeLists.txt +++ b/bhtree_mpi/src/datastructures/CMakeLists.txt @@ -8,5 +8,12 @@ # ================================================================================================== cmake_minimum_required (VERSION 3.0 FATAL_ERROR) +include(CheckCXXCompilerFlag) +CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) +set(CXX11 ${COMPILER_SUPPORTS_CXX11}) +set(CXX11_FLAGS -std=c++11) +set(CMAKE_CXX_FLAGS "-std=c++11") file( GLOB datastructures_SOURCES *.hpp *.cpp ) add_library( datastructures ${datastructures_SOURCES} ) + + diff --git a/bhtree_mpi/src/datastructures/Node.cpp b/bhtree_mpi/src/datastructures/Node.cpp index b44a209..8b46cd2 100644 --- a/bhtree_mpi/src/datastructures/Node.cpp +++ b/bhtree_mpi/src/datastructures/Node.cpp @@ -9,14 +9,14 @@ namespace nbody { Node::Node(Tree* tree) { initBox(this->bb); - this->afterSubtree = NULL; + this->afterSubtree = nullptr; this->prev = this; this->next = this; this->leaf = true; this->tree = tree; - this->prevSibling = NULL; - this->nextSibling = NULL; - this->parent = NULL; + this->prevSibling = nullptr; + this->nextSibling = nullptr; + this->parent = nullptr; } Node::~Node() { diff --git a/bhtree_mpi/src/datastructures/Tree.cpp b/bhtree_mpi/src/datastructures/Tree.cpp index 616ae7f..66b8ab2 100644 --- a/bhtree_mpi/src/datastructures/Tree.cpp +++ b/bhtree_mpi/src/datastructures/Tree.cpp @@ -38,7 +38,7 @@ namespace nbody { this->nodes = new Node(this); } - unsigned long Tree::numberOfNodes() { + size_t Tree::numberOfNodes() { unsigned long nodes = 0; for (Node* node = this->nodes->next; node != this->nodes; node = node->next) { diff --git a/bhtree_mpi/src/datastructures/Tree.hpp b/bhtree_mpi/src/datastructures/Tree.hpp index c05cc5e..3a83cea 100644 --- a/bhtree_mpi/src/datastructures/Tree.hpp +++ b/bhtree_mpi/src/datastructures/Tree.hpp @@ -33,7 +33,7 @@ namespace nbody { virtual void build(vector bodies) = 0; virtual void build(vector bodies, Box domain) = 0; virtual int numberOfChildren() = 0; - virtual unsigned long numberOfNodes(); + virtual size_t numberOfNodes(); virtual bool isCorrect(); virtual void accumulateForceOnto(Body& body); virtual void computeForces(); diff --git a/bhtree_mpi/src/simulation/MpiSimulation.cpp b/bhtree_mpi/src/simulation/MpiSimulation.cpp index 9e96208..c0b76db 100644 --- a/bhtree_mpi/src/simulation/MpiSimulation.cpp +++ b/bhtree_mpi/src/simulation/MpiSimulation.cpp @@ -14,8 +14,7 @@ namespace nbody { using namespace std; MpiSimulation::MpiSimulation() { - this->domains = NULL; - this->tree = NULL; + this->tree = nullptr; this->bodyType = MPI_DATATYPE_NULL; this->boxType = MPI_DATATYPE_NULL; } @@ -26,8 +25,7 @@ namespace nbody { MpiSimulation::~MpiSimulation() { delete this->tree; - this->tree = NULL; - delete[] this->domains; + this->tree = nullptr; while (!this->sendStores.empty()) { delete[] this->sendStores.back().bodies; this->sendStores.pop_back(); @@ -59,7 +57,7 @@ namespace nbody { //get number of processes and own process id MPI_Comm_size(MPI_COMM_WORLD, &this->parallelSize); MPI_Comm_rank(MPI_COMM_WORLD, &this->parallelRank); - this->domains = new Box[this->parallelSize]; + this->domains.reserve(this->parallelSize); this->correctState = true; this->tree = new BarnesHutTree(this->parallelRank); @@ -75,8 +73,8 @@ namespace nbody { cerr << "Error occurred: terminating ..." << endl; MPI_Type_free(&this->bodyType); MPI_Type_free(&this->boxType); - MPI_Finalize(); - exit(-1); + MPI_Abort(MPI_COMM_WORLD, -1); + abort(); } } @@ -113,15 +111,12 @@ namespace nbody { int MpiSimulation::recv(vector& bodies, int source) { MPI_Status status; int count; - Body* lb; //do blocking recv; any source receive can be done with source == MPI_ANY_SOURCE MPI_Probe(source, 0, MPI_COMM_WORLD, &status); MPI_Get_count(&status, this->bodyType, &count); - lb = new Body[count]; - MPI_Recv(lb, count, this->bodyType, status.MPI_SOURCE, 0, MPI_COMM_WORLD, &status); - bodies = vector(lb, lb + count); - delete[] lb; + bodies.resize(count); + MPI_Recv(bodies.data(), count, this->bodyType, status.MPI_SOURCE, 0, MPI_COMM_WORLD, &status); //return source to determine message source for any source receives return status.MPI_SOURCE; } @@ -134,7 +129,7 @@ namespace nbody { Box bb; initBox(bb); - nodes.push_back(Node(NULL)); + nodes.push_back(Node(nullptr)); nodes.front().setBodies(this->bodies); extendForBodies(bb, this->bodies); nodes.front().setBB(bb); @@ -151,12 +146,12 @@ namespace nbody { } vector subdomains = splitLongestSide(nodes[mostBodiesIndex].getBB()); vector buf = nodes[mostBodiesIndex].getBodies(); - Node n(NULL); + Node n(nullptr); n.setBodies(extractBodies(subdomains[0], buf)); n.setBB(subdomains[0]); nodes.insert(nodes.begin() + mostBodiesIndex, n); - n = Node(NULL); + n = Node(nullptr); n.setBodies(extractBodies(subdomains[1], buf)); n.setBB(subdomains[1]); nodes.insert(nodes.begin() + mostBodiesIndex, n); diff --git a/bhtree_mpi/src/simulation/MpiSimulation.hpp b/bhtree_mpi/src/simulation/MpiSimulation.hpp index 4484c38..dbbdb6f 100644 --- a/bhtree_mpi/src/simulation/MpiSimulation.hpp +++ b/bhtree_mpi/src/simulation/MpiSimulation.hpp @@ -10,7 +10,7 @@ namespace nbody { using namespace std; - typedef struct _SendStore { + typedef struct SendStoreStruct { Body* bodies; MPI_Request request; int size; @@ -21,7 +21,7 @@ namespace nbody { protected: MPI_Datatype bodyType; MPI_Datatype boxType; - Box* domains; + vector domains; Box overallDomain; vector sendStores; -- GitLab