From 3e1d268fa33869a4b18ccfe52e99ab271fe342cc Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Wed, 17 Oct 2018 20:25:32 +0100 Subject: [PATCH 01/33] Hadrons: DiskVector optimisation --- Hadrons/DiskVector.hpp | 65 ++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/Hadrons/DiskVector.hpp b/Hadrons/DiskVector.hpp index 94c3e597..eb29755f 100644 --- a/Hadrons/DiskVector.hpp +++ b/Hadrons/DiskVector.hpp @@ -83,6 +83,7 @@ public: public: DiskVectorBase(const std::string dirname, const unsigned int size = 0, const unsigned int cacheSize = 1, const bool clean = true); + DiskVectorBase(DiskVectorBase &&v) = default; virtual ~DiskVectorBase(void); const T & operator[](const unsigned int i) const; RwAccessHelper operator[](const unsigned int i); @@ -97,14 +98,16 @@ private: void cacheInsert(const unsigned int i, const T &obj) const; void clean(void); private: - std::string dirname_; - unsigned int size_, cacheSize_; - double access_{0.}, hit_{0.}; - bool clean_; + std::string dirname_; + unsigned int size_, cacheSize_; + double access_{0.}, hit_{0.}; + bool clean_; // using pointers to allow modifications when class is const // semantic: const means data unmodified, but cache modification allowed - std::unique_ptr> cachePtr_; - std::unique_ptr> loadsPtr_; + std::unique_ptr> cachePtr_; + std::unique_ptr> indexPtr_; + std::unique_ptr> freePtr_; + std::unique_ptr> loadsPtr_; }; /****************************************************************************** @@ -201,7 +204,9 @@ DiskVectorBase::DiskVectorBase(const std::string dirname, const unsigned int cacheSize, const bool clean) : dirname_(dirname), size_(size), cacheSize_(cacheSize), clean_(clean) -, cachePtr_(new std::map()) +, cachePtr_(new std::vector(size)) +, indexPtr_(new std::map()) +, freePtr_(new std::stack) , loadsPtr_(new std::deque()) { struct stat s; @@ -211,6 +216,10 @@ DiskVectorBase::DiskVectorBase(const std::string dirname, HADRONS_ERROR(Io, "directory '" + dirname + "' already exists") } mkdir(dirname); + for (unsigned int i = 0; i < cacheSize_; ++i) + { + freePtr_->push(i); + } } template @@ -225,8 +234,10 @@ DiskVectorBase::~DiskVectorBase(void) template const T & DiskVectorBase::operator[](const unsigned int i) const { - auto &cache = *cachePtr_; - auto &loads = *loadsPtr_; + auto &cache = *cachePtr_; + auto &index = *indexPtr_; + auto &freeInd = *freePtr_; + auto &loads = *loadsPtr_; DV_DEBUG_MSG(this, "accessing " << i << " (RO)"); @@ -235,7 +246,7 @@ const T & DiskVectorBase::operator[](const unsigned int i) const HADRONS_ERROR(Size, "index out of range"); } const_cast(access_)++; - if (cache.find(i) == cache.end()) + if (index.find(i) == index.end()) { // cache miss DV_DEBUG_MSG(this, "cache miss"); @@ -262,7 +273,7 @@ const T & DiskVectorBase::operator[](const unsigned int i) const DV_DEBUG_MSG(this, "in cache: " << msg); #endif - return cache.at(i); + return cache[index.at(i)]; } template @@ -300,13 +311,15 @@ std::string DiskVectorBase::filename(const unsigned int i) const template void DiskVectorBase::evict(void) const { - auto &cache = *cachePtr_; - auto &loads = *loadsPtr_; + auto &index = *indexPtr_; + auto &freeInd = *freePtr_; + auto &loads = *loadsPtr_; - if (cache.size() >= cacheSize_) + if (index.size() >= cacheSize_) { DV_DEBUG_MSG(this, "evicting " << loads.front()); - cache.erase(loads.front()); + freeInd.push(index.at(loads.front())); + index.erase(loads.front()); loads.pop_front(); } } @@ -314,29 +327,39 @@ void DiskVectorBase::evict(void) const template void DiskVectorBase::fetch(const unsigned int i) const { - auto &cache = *cachePtr_; - auto &loads = *loadsPtr_; + auto &cache = *cachePtr_; + auto &index = *indexPtr_; + auto &freeInd = *freePtr_; + auto &loads = *loadsPtr_; + struct stat s; DV_DEBUG_MSG(this, "loading " << i << " from disk"); evict(); + if(stat(filename(i).c_str(), &s) != 0) { HADRONS_ERROR(Io, "disk vector element " + std::to_string(i) + " uninitialised"); } - load(cache[i], filename(i)); + index[i] = freeInd.top(); + freeInd.pop(); + load(cache[index.at(i)], filename(i)); loads.push_back(i); } template void DiskVectorBase::cacheInsert(const unsigned int i, const T &obj) const { - auto &cache = *cachePtr_; - auto &loads = *loadsPtr_; + auto &cache = *cachePtr_; + auto &index = *indexPtr_; + auto &freeInd = *freePtr_; + auto &loads = *loadsPtr_; evict(); - cache[i] = obj; + index[i] = freeInd.top(); + freeInd.pop(); + cache[index.at(i)] = obj; loads.push_back(i); #ifdef DV_DEBUG From f05b25dae4566fe7f5ed8c887e39bf759c753ade Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Wed, 17 Oct 2018 20:26:26 +0100 Subject: [PATCH 02/33] Hadrons: A2AMatrix load --- Hadrons/A2AMatrix.hpp | 122 +++++++++++++++++- .../Modules/MContraction/A2AAslashField.hpp | 8 +- .../Modules/MContraction/A2AMesonField.hpp | 8 +- 3 files changed, 122 insertions(+), 16 deletions(-) diff --git a/Hadrons/A2AMatrix.hpp b/Hadrons/A2AMatrix.hpp index 95133f30..480d327f 100644 --- a/Hadrons/A2AMatrix.hpp +++ b/Hadrons/A2AMatrix.hpp @@ -37,6 +37,10 @@ See the full license in the file "LICENSE" in the top level distribution directo #define HADRONS_A2AM_NAME "a2aMatrix" #endif +#ifndef HADRONS_A2AM_IO_TYPE +#define HADRONS_A2AM_IO_TYPE ComplexF +#endif + #define HADRONS_A2AM_PARALLEL_IO BEGIN_HADRONS_NAMESPACE @@ -51,6 +55,9 @@ BEGIN_HADRONS_NAMESPACE template using A2AMatrixSet = Eigen::TensorMap>; +template +using A2AMatrix = Eigen::Matrix; + /****************************************************************************** * Abstract class for A2A kernels * ******************************************************************************/ @@ -76,10 +83,15 @@ public: // constructors A2AMatrixIo(void) = default; A2AMatrixIo(std::string filename, std::string dataname, - const unsigned int nt, const unsigned int ni, - const unsigned int nj); + const unsigned int nt, const unsigned int ni = 0, + const unsigned int nj = 0); // destructor ~A2AMatrixIo(void) = default; + // access + unsigned int getNi(void) const; + unsigned int getNj(void) const; + unsigned int getNt(void) const; + size_t getSize(void) const; // file allocation template void initFile(const MetadataType &d, const unsigned int chunkSize); @@ -88,9 +100,11 @@ public: const unsigned int blockSizei, const unsigned int blockSizej); void saveBlock(const A2AMatrixSet &m, const unsigned int ext, const unsigned int str, const unsigned int i, const unsigned int j); + template