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 0000000000000000000000000000000000000000..d7221a96735d6f9ec584afa8f5a4c454b2542f65 --- /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 0000000000000000000000000000000000000000..eab2126c4bbd5557ef9bfc4c2eb0e44e6dc72fb6 --- /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 0000000000000000000000000000000000000000..e7ee7fa4e81a68ca6bae90da459cdf0c51c422b1 --- /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 0000000000000000000000000000000000000000..956fc4e2a552965142092ecbbc65154c57a1c272 --- /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 0000000000000000000000000000000000000000..ec6ecfd5571fe26e82fb4e0bc9fda10c92ae4e2b --- /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 0000000000000000000000000000000000000000..0599446860551967ca3dffbb81da5168dd58f11e --- /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; +}