introduce Block classes + prepare operation code for blocking (in future vectorization)

This commit is contained in:
Christian Zimmermann 2017-08-26 22:53:02 +02:00
parent 8a75177b39
commit 0d2a5e22e2
4 changed files with 267 additions and 16 deletions

127
src/block.cc Normal file
View file

@ -0,0 +1,127 @@
// -*- C++ -*-
#include "block.h"
namespace MultiArrayHelper
{
/*****************
* BlockBase *
*****************/
template <typename T>
BlockBase<T>::BlockBase(size_t size) : mSize(size) {}
template <typename T>
size_t BlockBase<T>::size() const
{
return mSize;
}
template <typename T>
template <class OpFunction>
BlockResult<T> BlockBase<T>::operate(const BlockBase& in)
{
assert(mSize == in.size());
OpFunction f;
BlockResult<T> res(mSize);
for(size_t i = 0; i != mSize; ++i){
res[i] = f(this->operator[](i), in[i]);
}
return res;
}
/*************
* Block *
*************/
template <typename T>
Block<T>::Block(const std::vector<T>& data,
size_t begPos, size_t size) :
BlockBase<T>(size),
mBegPtr(data.data() + begPos) {}
template <typename T>
BlockType Block<T>::type() const
{
return BlockType::BLOCK;
}
template <typename T>
const T& Block<T>::operator[](size_t i) const
{
return *(mBegPtr + i);
}
/******************
* BlockValue *
******************/
template <typename T>
BlockValue<T>::BlockValue(const T& val, size_t size) :
BlockBase<T>(size),
mVal(val) {}
template <typename T>
BlockType BlockValue<T>::type() const
{
return BlockType::VALUE;
}
template <typename T>
const T& BlockValue<T>::operator[](size_t i) const
{
return mVal;
}
/******************
* SplitBlock *
******************/
template <typename T>
SplitBlock<T>::SplitBlock(std::vector<T*>&& begPtrVec) :
BlockBase<T>(begPtrVec.size()),
mBegPtr(begPtrVec) {}
template <typename T>
BlockType SplitBlock<T>::type() const
{
return BlockType::SPLIT;
}
template <typename T>
const T& SplitBlock<T>::operator[](size_t i) const
{
return *(mBegPtrVec[i]);
}
/*******************
* BlockResult *
*******************/
template <typename T>
BlockResult<T>::BlockResult(size_t size) :
BlockBase<T>(size),
mRes(size) {}
template <typename T>
BlockType BlockResult<T>::type() const
{
return BlockType::RESULT;
}
template <typename T>
const T& BlockResult<T>::operator[](size_t i) const
{
return mRes[i];
}
template <typename T>
T& BlockResult<T>::operator[](size_t i)
{
return mRes[i];
}
} // end namespace MultiArrayHelper

111
src/block.h Normal file
View file

@ -0,0 +1,111 @@
// -*- C++ -*-
#ifndef __block_h__
#define __block_h__
#include <cstdlib>
#include <vector>
#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 <typename T>
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 <class OpFunction>
BlockResult<T> operate(const BlockBase& in);
protected:
size_t mSize;
};
template <typename T>
class Block : public BlockBase<T>
{
public:
Block() = default;
Block(const std::vector<T>& 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 <typename T>
class BlockValue : public BlockBase<T>
{
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 <typename T>
class SplitBlock : public BlockBase<T>
{
public:
SplitBlock() = default;
SplitBlock(std::vector<T*>&& 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<T*> mBegPtrVec;
};
template <typename T>
class BlockResult : public BlockBase<T>
{
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<T> mRes;
};
} // end namespace MultiArrayHelper
#include "block.cc"
#endif

View file

@ -4,7 +4,10 @@
namespace MultiArrayTools
{
namespace
{
using namespace MultiArrayHelper;
}
/*********************************
* MultiArrayOperationBase *
@ -73,13 +76,13 @@ namespace MultiArrayTools
}
template <typename T, class... Ranges>
T& OperationMaster<T,Ranges...>::get()
BlockBase<T>& OperationMaster<T,Ranges...>::get(const IndexBase& ind)
{
return mArrayRef.data()[ mIndex->pos() ];
}
template <typename T, class... Ranges>
const T& OperationMaster<T,Ranges...>::get() const
const BlockBase<T>& OperationMaster<T,Ranges...>::get(const IndexBase& ind) const
{
return mArrayRef.data()[ mIndex->pos() ];
}
@ -99,7 +102,7 @@ namespace MultiArrayTools
}
template <typename T, class... Ranges>
const T& ConstOperationRoot<T,Ranges...>::get() const
const BlockBase<T>& ConstOperationRoot<T,Ranges...>::get(const IndexBase& ind) const
{
return mArrayRef[ (*mIndex)() ];
}
@ -125,13 +128,13 @@ namespace MultiArrayTools
}
template <typename T, class... Ranges>
const T& OperationRoot<T,Ranges...>::get() const
const BlockBase<T>& OperationRoot<T,Ranges...>::get(const IndexBase& ind) const
{
return mArrayRef[ (*mIndex)() ];
}
template <typename T, class... Ranges>
T& OperationRoot<T,Ranges...>::get()
BlockBase<T>& OperationRoot<T,Ranges...>::get(const IndexBase& ind)
{
return mArrayRef[ (*mIndex)() ];
}
@ -146,7 +149,7 @@ namespace MultiArrayTools
mOps(ops...) {}
template <typename T, class OpFunction, class... Ops>
const T& Operation<T,OpFunction,Ops...>::get() const
const BlockBase<T>& Operation<T,OpFunction,Ops...>::get(const IndexBase& ind) const
{
mRes = PackNum<sizeof...(Ops)-1>::template unpackArgs<T,OpFunction>(mOps);
return mRes;

View file

@ -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<IndexBase>& blockIndex) = 0;
//virtual size_t argNum() const = 0;
virtual const T& get() const = 0;
virtual const BlockBase<T>& get(const IndexBase& ind) const = 0;
protected:
mutable std::shared_ptr<BlockBase> mBlockPtr;
};
template <typename T>
@ -45,7 +55,7 @@ namespace MultiArrayTools
public:
MutableOperationBase() = default;
virtual T& get() = 0;
virtual BlockBase<T>& get(const IndexBase& ind) = 0;
};
template <class OperationClass>
@ -87,8 +97,8 @@ namespace MultiArrayTools
OperationMaster(MutableMultiArrayBase<T,Ranges...>& ma, const OperationBase<T>& second,
std::shared_ptr<typename CRange::IndexType>& index);
virtual T& get() override;
virtual const T& get() const override;
virtual BlockBase<T>& get(const IndexBase& ind) override;
virtual const BlockBase<T>& get(const IndexBase& ind) const override;
protected:
@ -113,7 +123,7 @@ namespace MultiArrayTools
ConstOperationRoot(const MultiArrayBase<T,Ranges...>& ma,
const std::shared_ptr<typename Ranges::IndexType>&... indices);
virtual const T& get() const override;
virtual const BlockBase<T>& get(const IndexBase& ind) const override;
protected:
@ -137,8 +147,8 @@ namespace MultiArrayTools
OperationMaster<T,Ranges...> operator=(const OperationBase<T>& in);
virtual const T& get() const override;
virtual T& get() override;
virtual const BlockBase<T>& get(const IndexBase& ind) const override;
virtual BlockBase<T>& 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<T>& get(const IndexBase& ind) const override;
protected:
std::tuple<Ops...> mOps;
mutable T mRes;
mutable BlockResult<T> mRes;
};
}