From 26f8e73d871524c10c587ccad265923890c085bf Mon Sep 17 00:00:00 2001 From: Paul Heinzlreiter <paul.heinzlreiter@risc-software.at> Date: Thu, 25 Aug 2016 15:34:18 +0200 Subject: [PATCH] * moved files to one diorectory --- .../{datastructures => }/BarnesHutTree.cpp | 0 .../{datastructures => }/BarnesHutTree.hpp | 0 bh_tree_mpi/{datastructures => }/Body.cpp | 0 bh_tree_mpi/{datastructures => }/Body.hpp | 0 bh_tree_mpi/{datastructures => }/Box.cpp | 0 bh_tree_mpi/{datastructures => }/Box.hpp | 0 .../{parallelization => }/MpiBodyComm.cpp | 0 .../{parallelization => }/MpiBodyComm.hpp | 0 .../{parallelization => }/MpiSimulation.cpp | 0 .../{parallelization => }/MpiSimulation.hpp | 0 bh_tree_mpi/{datastructures => }/Node.cpp | 0 bh_tree_mpi/{datastructures => }/Node.hpp | 0 bh_tree_mpi/PthreadSimulation.cpp | 43 +++++++++++ bh_tree_mpi/PthreadSimulation.hpp | 26 +++++++ bh_tree_mpi/ReadWriteLock.cpp | 77 +++++++++++++++++++ bh_tree_mpi/ReadWriteLock.hpp | 23 ++++++ .../{parallelization => }/Simulation.cpp | 0 .../{parallelization => }/Simulation.hpp | 0 bh_tree_mpi/{datastructures => }/Tree.cpp | 0 bh_tree_mpi/{datastructures => }/Tree.hpp | 0 bh_tree_mpi/mpimain.cpp | 20 +++++ bh_tree_mpi/pthreadmain.cpp | 11 +++ 22 files changed, 200 insertions(+) rename bh_tree_mpi/{datastructures => }/BarnesHutTree.cpp (100%) rename bh_tree_mpi/{datastructures => }/BarnesHutTree.hpp (100%) rename bh_tree_mpi/{datastructures => }/Body.cpp (100%) rename bh_tree_mpi/{datastructures => }/Body.hpp (100%) rename bh_tree_mpi/{datastructures => }/Box.cpp (100%) rename bh_tree_mpi/{datastructures => }/Box.hpp (100%) rename bh_tree_mpi/{parallelization => }/MpiBodyComm.cpp (100%) rename bh_tree_mpi/{parallelization => }/MpiBodyComm.hpp (100%) rename bh_tree_mpi/{parallelization => }/MpiSimulation.cpp (100%) rename bh_tree_mpi/{parallelization => }/MpiSimulation.hpp (100%) rename bh_tree_mpi/{datastructures => }/Node.cpp (100%) rename bh_tree_mpi/{datastructures => }/Node.hpp (100%) create mode 100644 bh_tree_mpi/PthreadSimulation.cpp create mode 100644 bh_tree_mpi/PthreadSimulation.hpp create mode 100644 bh_tree_mpi/ReadWriteLock.cpp create mode 100644 bh_tree_mpi/ReadWriteLock.hpp rename bh_tree_mpi/{parallelization => }/Simulation.cpp (100%) rename bh_tree_mpi/{parallelization => }/Simulation.hpp (100%) rename bh_tree_mpi/{datastructures => }/Tree.cpp (100%) rename bh_tree_mpi/{datastructures => }/Tree.hpp (100%) create mode 100644 bh_tree_mpi/mpimain.cpp create mode 100644 bh_tree_mpi/pthreadmain.cpp diff --git a/bh_tree_mpi/datastructures/BarnesHutTree.cpp b/bh_tree_mpi/BarnesHutTree.cpp similarity index 100% rename from bh_tree_mpi/datastructures/BarnesHutTree.cpp rename to bh_tree_mpi/BarnesHutTree.cpp diff --git a/bh_tree_mpi/datastructures/BarnesHutTree.hpp b/bh_tree_mpi/BarnesHutTree.hpp similarity index 100% rename from bh_tree_mpi/datastructures/BarnesHutTree.hpp rename to bh_tree_mpi/BarnesHutTree.hpp diff --git a/bh_tree_mpi/datastructures/Body.cpp b/bh_tree_mpi/Body.cpp similarity index 100% rename from bh_tree_mpi/datastructures/Body.cpp rename to bh_tree_mpi/Body.cpp diff --git a/bh_tree_mpi/datastructures/Body.hpp b/bh_tree_mpi/Body.hpp similarity index 100% rename from bh_tree_mpi/datastructures/Body.hpp rename to bh_tree_mpi/Body.hpp diff --git a/bh_tree_mpi/datastructures/Box.cpp b/bh_tree_mpi/Box.cpp similarity index 100% rename from bh_tree_mpi/datastructures/Box.cpp rename to bh_tree_mpi/Box.cpp diff --git a/bh_tree_mpi/datastructures/Box.hpp b/bh_tree_mpi/Box.hpp similarity index 100% rename from bh_tree_mpi/datastructures/Box.hpp rename to bh_tree_mpi/Box.hpp diff --git a/bh_tree_mpi/parallelization/MpiBodyComm.cpp b/bh_tree_mpi/MpiBodyComm.cpp similarity index 100% rename from bh_tree_mpi/parallelization/MpiBodyComm.cpp rename to bh_tree_mpi/MpiBodyComm.cpp diff --git a/bh_tree_mpi/parallelization/MpiBodyComm.hpp b/bh_tree_mpi/MpiBodyComm.hpp similarity index 100% rename from bh_tree_mpi/parallelization/MpiBodyComm.hpp rename to bh_tree_mpi/MpiBodyComm.hpp diff --git a/bh_tree_mpi/parallelization/MpiSimulation.cpp b/bh_tree_mpi/MpiSimulation.cpp similarity index 100% rename from bh_tree_mpi/parallelization/MpiSimulation.cpp rename to bh_tree_mpi/MpiSimulation.cpp diff --git a/bh_tree_mpi/parallelization/MpiSimulation.hpp b/bh_tree_mpi/MpiSimulation.hpp similarity index 100% rename from bh_tree_mpi/parallelization/MpiSimulation.hpp rename to bh_tree_mpi/MpiSimulation.hpp diff --git a/bh_tree_mpi/datastructures/Node.cpp b/bh_tree_mpi/Node.cpp similarity index 100% rename from bh_tree_mpi/datastructures/Node.cpp rename to bh_tree_mpi/Node.cpp diff --git a/bh_tree_mpi/datastructures/Node.hpp b/bh_tree_mpi/Node.hpp similarity index 100% rename from bh_tree_mpi/datastructures/Node.hpp rename to bh_tree_mpi/Node.hpp diff --git a/bh_tree_mpi/PthreadSimulation.cpp b/bh_tree_mpi/PthreadSimulation.cpp new file mode 100644 index 0000000..d7221a9 --- /dev/null +++ b/bh_tree_mpi/PthreadSimulation.cpp @@ -0,0 +1,43 @@ +#include "PthreadSimulation.hpp" + +namespace nbody { + PthreadSimulation::PthreadSimulation() { + this->correctState = -1; + } + + PthreadSimulation::~PthreadSimulation() { + } + + void PthreadSimulation::cleanup() { + + } + + int PthreadSimulation::getNumberOfProcesses() { + return this->threads.size(); + } + + int PthreadSimulation::getProcessId() { + vector<pthread_t>::iterator it = this->threads.begin(); + int id = 0; + + while (*it != pthread_self() && it != this->threads.end()) { + id++; + it++; + } + if (it != this->threads.end()) { + return id; + } else { + return -1; + } + } + + bool PthreadSimulation::readInputData(string filename) { + this->bodies = Tree::dubinskiParse(filename); + return !this->bodies.empty(); + } + + void PthreadSimulation::runStep() { + + } + +} diff --git a/bh_tree_mpi/PthreadSimulation.hpp b/bh_tree_mpi/PthreadSimulation.hpp new file mode 100644 index 0000000..eab2126 --- /dev/null +++ b/bh_tree_mpi/PthreadSimulation.hpp @@ -0,0 +1,26 @@ +#ifndef PTHREAD_SIMULATION_HPP +#define PTHREAD_SIMULATION_HPP + +#include "../datastructures/BarnesHutTree.hpp" +#include <vector> +#include <pthread.h> + +namespace nbody { + class PthreadSimulation { + protected: + int correctState; + vector<Body> bodies; + BarnesHutTree tree; + vector<pthread_t> threads; + public: + PthreadSimulation(); + virtual ~PthreadSimulation(); + virtual void cleanup() = 0; + virtual int getNumberOfProcesses() = 0; + virtual int getProcessId() = 0; + virtual bool readInputData(string filename) = 0; + virtual void runStep() = 0; + }; +} + +#endif diff --git a/bh_tree_mpi/ReadWriteLock.cpp b/bh_tree_mpi/ReadWriteLock.cpp new file mode 100644 index 0000000..e7ee7fa --- /dev/null +++ b/bh_tree_mpi/ReadWriteLock.cpp @@ -0,0 +1,77 @@ +#include "ReadWriteLock.hpp" + +namespace nbody { + ReadWriteLock::ReadWriteLock() { + pthread_mutex_init(&this->mutex, NULL); + pthread_rwlock_init(&this->rwlock, NULL); + this->owner = NULL; + } + + ReadWriteLock::~ReadWriteLock() { + pthread_mutex_destroy(&this->mutex); + pthread_rwlock_destroy(&this->rwlock); + } + + bool ReadWriteLock::readLock(pthread_t* myThread) { + bool result; + + pthread_mutex_lock(&this->mutex); + result = (pthread_rwlock_tryrdlock(&this->rwlock) == 0); + if (result) { + this->owner = myThread; + } + pthread_mutex_unlock(&this->mutex); + return result; + } + + bool ReadWriteLock::writeLock(pthread_t* myThread) { + bool result; + + pthread_mutex_lock(&this->mutex); + result = (pthread_rwlock_trywrlock(&this->rwlock) == 0); + if (result) { + this->owner = myThread; + } + pthread_mutex_unlock(&this->mutex); + return result; + } + + bool ReadWriteLock::upgradeToWriteLock(pthread_t* myThread) { + bool result = false; + + pthread_mutex_lock(&this->mutex); + if (this->owner == myThread) { + pthread_rwlock_unlock(&this->rwlock); + pthread_rwlock_wrlock(&this->rwlock); + result = true; + } + pthread_mutex_unlock(&this->mutex); + return result; + } + + bool ReadWriteLock::downgradeToReadLock(pthread_t* myThread) { + bool result = false; + + pthread_mutex_lock(&this->mutex); + if (this->owner == myThread) { + pthread_rwlock_unlock(&this->rwlock); + pthread_rwlock_rdlock(&this->rwlock); + result = true; + } + pthread_mutex_unlock(&this->mutex); + return result; + } + + bool ReadWriteLock::unLock(pthread_t* myThread) { + bool result = false; + + pthread_mutex_lock(&this->mutex); + if (this->owner == myThread) { + pthread_rwlock_unlock(&this->rwlock); + this->owner = NULL; + result = true; + } + pthread_mutex_unlock(&this->mutex); + return result; + } +} diff --git a/bh_tree_mpi/ReadWriteLock.hpp b/bh_tree_mpi/ReadWriteLock.hpp new file mode 100644 index 0000000..956fc4e --- /dev/null +++ b/bh_tree_mpi/ReadWriteLock.hpp @@ -0,0 +1,23 @@ +#ifndef READ_WRITE_LOCK +#define READ_WRITE_LOCK + +#include <pthread.h> + +namespace nbody { + class ReadWriteLock { + protected: + pthread_mutex_t mutex; + pthread_rwlock_t rwlock; + pthread_t* owner; + public: + ReadWriteLock(); + virtual ~ReadWriteLock(); + virtual bool readLock(pthread_t* myThread); + virtual bool writeLock(pthread_t* myThread); + virtual bool upgradeToWriteLock(pthread_t* myThread); + virtual bool downgradeToReadLock(pthread_t* myThread); + virtual bool unLock(pthread_t* myThread); + }; +} + +#endif diff --git a/bh_tree_mpi/parallelization/Simulation.cpp b/bh_tree_mpi/Simulation.cpp similarity index 100% rename from bh_tree_mpi/parallelization/Simulation.cpp rename to bh_tree_mpi/Simulation.cpp diff --git a/bh_tree_mpi/parallelization/Simulation.hpp b/bh_tree_mpi/Simulation.hpp similarity index 100% rename from bh_tree_mpi/parallelization/Simulation.hpp rename to bh_tree_mpi/Simulation.hpp diff --git a/bh_tree_mpi/datastructures/Tree.cpp b/bh_tree_mpi/Tree.cpp similarity index 100% rename from bh_tree_mpi/datastructures/Tree.cpp rename to bh_tree_mpi/Tree.cpp diff --git a/bh_tree_mpi/datastructures/Tree.hpp b/bh_tree_mpi/Tree.hpp similarity index 100% rename from bh_tree_mpi/datastructures/Tree.hpp rename to bh_tree_mpi/Tree.hpp diff --git a/bh_tree_mpi/mpimain.cpp b/bh_tree_mpi/mpimain.cpp new file mode 100644 index 0000000..ec6ecfd --- /dev/null +++ b/bh_tree_mpi/mpimain.cpp @@ -0,0 +1,20 @@ +#include "datastructures/BarnesHutTree.hpp" +#include "parallelization/MpiSimulation.hpp" +#include <iostream> +#include <mpi.h> + +using namespace nbody; +using namespace std; + +int main(int argc, char* argv[]) { + MPI_Init(&argc, &argv); + MpiSimulation simulation(argc, argv); + + simulation.distributeBodies(); + for (int i = 0; i < 6; i++) { + simulation.runStep(); + } + simulation.cleanup(); + MPI_Finalize(); + return 0; +} diff --git a/bh_tree_mpi/pthreadmain.cpp b/bh_tree_mpi/pthreadmain.cpp new file mode 100644 index 0000000..0599446 --- /dev/null +++ b/bh_tree_mpi/pthreadmain.cpp @@ -0,0 +1,11 @@ +#include "datastructures/BarnesHutTree.hpp" +#include <iostream> +#include <pthread.h> + +using namespace nbody; +using namespace std; + +int main(int argc, char* argv[]) { + + return 0; +} -- GitLab