From 0d2a5e22e25752d7f0484727af6151a609ff1bc5 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sat, 26 Aug 2017 22:53:02 +0200 Subject: [PATCH] introduce Block classes + prepare operation code for blocking (in future vectorization) --- src/block.cc | 127 +++++++++++++++++++++++++++++++++++ src/block.h | 111 ++++++++++++++++++++++++++++++ src/multi_array_operation.cc | 17 +++-- src/multi_array_operation.h | 28 +++++--- 4 files changed, 267 insertions(+), 16 deletions(-) create mode 100644 src/block.cc create mode 100644 src/block.h diff --git a/src/block.cc b/src/block.cc new file mode 100644 index 0000000..a06bbd0 --- /dev/null +++ b/src/block.cc @@ -0,0 +1,127 @@ +// -*- C++ -*- + +#include "block.h" + +namespace MultiArrayHelper +{ + + /***************** + * BlockBase * + *****************/ + + template + BlockBase::BlockBase(size_t size) : mSize(size) {} + + template + size_t BlockBase::size() const + { + return mSize; + } + + template + template + BlockResult BlockBase::operate(const BlockBase& in) + { + assert(mSize == in.size()); + OpFunction f; + BlockResult res(mSize); + for(size_t i = 0; i != mSize; ++i){ + res[i] = f(this->operator[](i), in[i]); + } + return res; + } + + /************* + * Block * + *************/ + + template + Block::Block(const std::vector& data, + size_t begPos, size_t size) : + BlockBase(size), + mBegPtr(data.data() + begPos) {} + + template + BlockType Block::type() const + { + return BlockType::BLOCK; + } + + template + const T& Block::operator[](size_t i) const + { + return *(mBegPtr + i); + } + + /****************** + * BlockValue * + ******************/ + + template + BlockValue::BlockValue(const T& val, size_t size) : + BlockBase(size), + mVal(val) {} + + template + BlockType BlockValue::type() const + { + return BlockType::VALUE; + } + + template + const T& BlockValue::operator[](size_t i) const + { + return mVal; + } + + /****************** + * SplitBlock * + ******************/ + + template + SplitBlock::SplitBlock(std::vector&& begPtrVec) : + BlockBase(begPtrVec.size()), + mBegPtr(begPtrVec) {} + + template + BlockType SplitBlock::type() const + { + return BlockType::SPLIT; + } + + template + const T& SplitBlock::operator[](size_t i) const + { + return *(mBegPtrVec[i]); + } + + /******************* + * BlockResult * + *******************/ + + template + BlockResult::BlockResult(size_t size) : + BlockBase(size), + mRes(size) {} + + template + BlockType BlockResult::type() const + { + return BlockType::RESULT; + } + + template + const T& BlockResult::operator[](size_t i) const + { + return mRes[i]; + } + + template + T& BlockResult::operator[](size_t i) + { + return mRes[i]; + } + + + +} // end namespace MultiArrayHelper diff --git a/src/block.h b/src/block.h new file mode 100644 index 0000000..fddf7e3 --- /dev/null +++ b/src/block.h @@ -0,0 +1,111 @@ +// -*- C++ -*- + +#ifndef __block_h__ +#define __block_h__ + +#include +#include + +#include "base_def.h" + +namespace MultiArrayHelper +{ + + enum class BlockType { + INDEF = 0, + BLOCK = 1, + VALUE = 2, + SPLIT = 3, + RESULT = 4 + }; + + // manage vectorization in the future !! + + template + class BlockBase + { + public: + BlockBase() = default; + BlockBase(size_t size); + + virtual BlockType type() const = 0; + + virtual size_t size() const; + virtual const T& operator[](size_t pos) const = 0; + + template + BlockResult operate(const BlockBase& in); + + protected: + size_t mSize; + }; + + template + class Block : public BlockBase + { + public: + Block() = default; + Block(const std::vector& data, size_t begPos, size_t size); + + virtual BlockType type() const override; + virtual const T& operator[](size_t pos) const override; + + protected: + T* mBegPtr; + }; + + template + class BlockValue : public BlockBase + { + public: + BlockValue() = default; + BlockValue(const T& val, size_t size); + + virtual BlockType type() const override; + virtual const T& operator[](size_t pos) const override; + + virtual BlockBase& set(size_t begPos) override; + + protected: + T mVal; + }; + + template + class SplitBlock : public BlockBase + { + public: + + SplitBlock() = default; + SplitBlock(std::vector&& begPtrVec); + + virtual BlockType type() const override; + virtual const T& operator[](size_t pos) const override; + + virtual BlockBase& set(size_t begPos) override; + + protected: + std::vector mBegPtrVec; + }; + + template + class BlockResult : public BlockBase + { + public: + BlockResult() = default; + BlockResult(size_t size); + + virtual BlockType type() const override; + virtual const T& operator[](size_t pos) const override; + virtual T& operator[](size_t i); + + virtual BlockBase& set(size_t begPos) override; + + protected: + std::vector mRes; + }; + +} // end namespace MultiArrayHelper + +#include "block.cc" + +#endif diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index 7dfbc53..7ca9321 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -4,7 +4,10 @@ namespace MultiArrayTools { - + namespace + { + using namespace MultiArrayHelper; + } /********************************* * MultiArrayOperationBase * @@ -73,13 +76,13 @@ namespace MultiArrayTools } template - T& OperationMaster::get() + BlockBase& OperationMaster::get(const IndexBase& ind) { return mArrayRef.data()[ mIndex->pos() ]; } template - const T& OperationMaster::get() const + const BlockBase& OperationMaster::get(const IndexBase& ind) const { return mArrayRef.data()[ mIndex->pos() ]; } @@ -99,7 +102,7 @@ namespace MultiArrayTools } template - const T& ConstOperationRoot::get() const + const BlockBase& ConstOperationRoot::get(const IndexBase& ind) const { return mArrayRef[ (*mIndex)() ]; } @@ -125,13 +128,13 @@ namespace MultiArrayTools } template - const T& OperationRoot::get() const + const BlockBase& OperationRoot::get(const IndexBase& ind) const { return mArrayRef[ (*mIndex)() ]; } template - T& OperationRoot::get() + BlockBase& OperationRoot::get(const IndexBase& ind) { return mArrayRef[ (*mIndex)() ]; } @@ -146,7 +149,7 @@ namespace MultiArrayTools mOps(ops...) {} template - const T& Operation::get() const + const BlockBase& Operation::get(const IndexBase& ind) const { mRes = PackNum::template unpackArgs(mOps); return mRes; diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index 1ddb0c6..01b1261 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -12,6 +12,11 @@ namespace MultiArrayTools { + namespace + { + using namespace MultiArrayHelper; + } + /* * OperationBase * MutableOperationBase @@ -35,8 +40,13 @@ namespace MultiArrayTools OperationBase() = default; virtual ~OperationBase() = default; + virtual OperationBase& block(const std::shared_ptr& blockIndex) = 0; + //virtual size_t argNum() const = 0; - virtual const T& get() const = 0; + virtual const BlockBase& get(const IndexBase& ind) const = 0; + + protected: + mutable std::shared_ptr mBlockPtr; }; template @@ -45,7 +55,7 @@ namespace MultiArrayTools public: MutableOperationBase() = default; - virtual T& get() = 0; + virtual BlockBase& get(const IndexBase& ind) = 0; }; template @@ -87,8 +97,8 @@ namespace MultiArrayTools OperationMaster(MutableMultiArrayBase& ma, const OperationBase& second, std::shared_ptr& index); - virtual T& get() override; - virtual const T& get() const override; + virtual BlockBase& get(const IndexBase& ind) override; + virtual const BlockBase& get(const IndexBase& ind) const override; protected: @@ -113,7 +123,7 @@ namespace MultiArrayTools ConstOperationRoot(const MultiArrayBase& ma, const std::shared_ptr&... indices); - virtual const T& get() const override; + virtual const BlockBase& get(const IndexBase& ind) const override; protected: @@ -137,8 +147,8 @@ namespace MultiArrayTools OperationMaster operator=(const OperationBase& in); - virtual const T& get() const override; - virtual T& get() override; + virtual const BlockBase& get(const IndexBase& ind) const override; + virtual BlockBase& get(const IndexBase& ind) override; protected: @@ -158,11 +168,11 @@ namespace MultiArrayTools Operation(const Ops&... ops); - virtual const T& get() const override; + virtual const BlockBase& get(const IndexBase& ind) const override; protected: std::tuple mOps; - mutable T mRes; + mutable BlockResult mRes; }; }