diff --git a/CMakeLists.txt b/CMakeLists.txt index bacd260..5626d0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ else() message(WARNING "compiler ${CMAKE_CXX_COMPILER_ID} officially not supported") endif() -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++14 -Wpedantic -Ofast -march=native -faligned-new -funroll-loops -fopenmp") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++17 -Wpedantic -Ofast -march=native -faligned-new -funroll-loops -fopenmp") #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++14 -g -Wpedantic -Ofast -march=native -faligned-new -funroll-loops -fopenmp -ftemplate-backtrace-limit=0") #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++14 -g -Wpedantic -O0 -march=native -faligned-new -funroll-loops -fopenmp") diff --git a/src/include/allocator.h b/src/include/allocator.h index 5d66dac..56ca670 100644 --- a/src/include/allocator.h +++ b/src/include/allocator.h @@ -1,6 +1,6 @@ -#ifndef __ma_allocator__ -#define __ma_allocator__ +#ifndef __cxz_allocator__ +#define __cxz_allocator__ #include #include @@ -12,7 +12,7 @@ #define MIB_SIZE 1024*1024 // 1MiB #define WARN_SIZE MIB_SIZE*100 // 100 MiB -namespace MultiArrayHelper +namespace CNORXZInternal { template @@ -65,12 +65,12 @@ namespace MultiArrayHelper } -} // namespace MultiArrayHelper +} // namespace CNORXZInternal -namespace MultiArrayTools +namespace CNORXZ { template - using vector = std::vector>; + using vector = std::vector>; template inline std::vector toStdVec(const vector& v) @@ -84,6 +84,6 @@ namespace MultiArrayTools return vector(v.begin(), v.end()); } -} // namespace MultiArrayTools +} // namespace CNORXZ #endif diff --git a/src/include/arith.cc.h b/src/include/arith.cc.h new file mode 100644 index 0000000..6eec6f4 --- /dev/null +++ b/src/include/arith.cc.h @@ -0,0 +1,35 @@ + +#include "arith.h" + +namespace CNORXZ +{ + + template + template + auto StaticFunctionBase::mk(const Ops&... ops) + { + return Operation(ops...); + } + + template + template + inline auto StaticFunctionBase::xapply(const Tuple& tp, As... as) + { + if constexpr(N > 0){ + return xapply(tp, std::get(tp), as...); + } + else { + return F::apply(std::get<0>(tp), as...); + } + } + + template + template + inline auto StaticFunctionBase::apply(const std::tuple& arg) + { + return xapply(arg); + //return ArgPack::template mk >(arg); + } + + +} diff --git a/src/include/arith.h b/src/include/arith.h index db34d75..74b1230 100644 --- a/src/include/arith.h +++ b/src/include/arith.h @@ -1,79 +1,48 @@ -#ifndef __arith_h__ -#define __arith_h__ +#ifndef __cxz_arith_h__ +#define __cxz_arith_h__ #include -namespace MultiArrayTools +namespace CNORXZ { - template - struct ArgPack - { - template - static inline auto mk(const Tuple& tp, As... as) - -> decltype(ArgPack::template mk(tp)),As...>(tp, std::get(tp), as...)) - { - return ArgPack::template mk(tp)),As...>(tp, std::get(tp), as...); - } - - template - static inline auto mkd(const F& ff, const Tuple& tp, As... as) - -> decltype(ArgPack::template mkd(tp)),As...>(ff, tp, std::get(tp), as...)) - { - return ArgPack::template mkd(tp)),As...>(ff, tp, std::get(tp), as...); - } - }; - - template <> - struct ArgPack<0> - { - template - static inline auto mk(const Tuple& tp, As... as) - -> decltype(F::apply(std::get<0>(tp), as...)) - { - return F::apply(std::get<0>(tp), as...); - } - - template - static inline auto mkd(const F& ff, const Tuple& tp, As... as) - -> decltype(ff(std::get<0>(tp), as...)) - { - return ff(std::get<0>(tp), as...); - } - }; - - template + //template + template struct StaticFunctionBase { static constexpr bool FISSTATIC = true; - typedef T value_type; - - template - static auto mk(const Ops&... ops) - -> Operation - { - return Operation(ops...); - } + typedef F function; + //typedef typename F::value_type value_type; - static inline T apply(const std::tuple& arg) - { - return ArgPack::template mk >(arg); - } + template + static auto mk(const Ops&... ops); + + template + static inline auto xapply(const Tuple& tp, As... as); + + template + static inline auto apply(const std::tuple& arg); }; // OPERATIONS (STATIC) template - struct identity : public StaticFunctionBase, T> + struct identity : public StaticFunctionBase> { //static constexpr bool FISSTATIC = true; - using StaticFunctionBase, T>::apply; - + using StaticFunctionBase>::apply; + typedef T value_type; + static inline T apply(T a) { return a; } + + static inline T selfApply(T& a1, const T& a2) + { + return a1 = a2; + } }; template @@ -89,12 +58,13 @@ namespace MultiArrayTools using dividesv = decltype(std::declval()/std::declval()); template - struct plusx : public StaticFunctionBase, plusx, T, U> + struct plusx : public StaticFunctionBase> { static constexpr bool FISSTATIC = true; - using StaticFunctionBase, plusx, T, U>::apply; - - static inline plusv apply(T a1, U a2) + using StaticFunctionBase>::apply; + typedef plusv value_type; + + static inline value_type apply(T a1, U a2) { return a1 + a2; } @@ -106,36 +76,39 @@ namespace MultiArrayTools }; template - struct minusx : public StaticFunctionBase, minusx, T, U> + struct minusx : public StaticFunctionBase> { static constexpr bool FISSTATIC = true; - using StaticFunctionBase, minusx, T, U>::apply; + using StaticFunctionBase>::apply; + typedef minusv value_type; - static inline plusv apply(T a1, U a2) + static inline value_type apply(T a1, U a2) { return a1 - a2; } }; template - struct multipliesx : public StaticFunctionBase, multipliesx, T, U> + struct multipliesx : public StaticFunctionBase> { static constexpr bool FISSTATIC = true; - using StaticFunctionBase, multipliesx, T, U>::apply; + using StaticFunctionBase>::apply; + typedef multipliesv value_type; - static inline multipliesv apply(T a1, U a2) + static inline value_type apply(T a1, U a2) { return a1 * a2; } }; template - struct dividesx : public StaticFunctionBase, dividesx, T, U> + struct dividesx : public StaticFunctionBase> { static constexpr bool FISSTATIC = true; - using StaticFunctionBase, dividesx, T, U>::apply; + using StaticFunctionBase>::apply; + typedef dividesv value_type; - static inline dividesv apply(T a1, U a2) + static inline value_type apply(T a1, U a2) { return a1 / a2; } @@ -143,11 +116,12 @@ namespace MultiArrayTools }; template - struct negate : public StaticFunctionBase, T> + struct negate : public StaticFunctionBase> { static constexpr bool FISSTATIC = true; - using StaticFunctionBase, T>::apply; - + using StaticFunctionBase>::apply; + typedef T value_type; + static inline T apply(T a) { return -a; @@ -167,60 +141,6 @@ namespace MultiArrayTools template using divides = dividesx; - /* - template - struct plus : public StaticFunctionBase, T, T> - { - static constexpr bool FISSTATIC = true; - using StaticFunctionBase, T, T>::apply; - - static inline T apply(T a1, T a2) - { - return a1 + a2; - } - - static inline T& selfApply(T& a1, const T& a2) - { - return a1 += a2; - } - }; - - template - struct minus : public StaticFunctionBase, T, T> - { - static constexpr bool FISSTATIC = true; - using StaticFunctionBase, T, T>::apply; - - static inline T apply(T a1, T a2) - { - return a1 - a2; - } - }; - - template - struct multiplies : public StaticFunctionBase, T, T> - { - static constexpr bool FISSTATIC = true; - using StaticFunctionBase, T, T>::apply; - - static inline T apply(T a1, T a2) - { - return a1 * a2; - } - }; - - template - struct divides : public StaticFunctionBase, T, T> - { - static constexpr bool FISSTATIC = true; - using StaticFunctionBase, T, T>::apply; - - static inline T apply(T a1, T a2) - { - return a1 / a2; - } - }; - */ // OPERATIONS (STATIC) template class function @@ -241,16 +161,28 @@ namespace MultiArrayTools return mF(args...); } + template + static inline auto xapply(const std::function& ff, const Tuple& tp, As... as) + { + if constexpr(N > 0){ + return xapply(ff, tp, std::get(tp), as...); + } + else { + return ff(std::get<0>(tp), as...); + } + } + inline R operator()(const std::tuple& args) { - return ArgPack::template mkd,std::tuple>>(mF, args); + return xapply(mF, args); } }; #include #define regFunc1(fff) template \ - struct x_##fff : public StaticFunctionBase, T> {\ + struct x_##fff : public StaticFunctionBase> {\ static constexpr bool FISSTATIC = true;\ + typedef T value_type; \ static inline T apply(T a){\ return fff(a); } }; @@ -265,35 +197,17 @@ namespace MultiArrayTools template static inline T apply(T a) { - return a * x_ipow::apply(a); + if constexpr(N > 0){ + return a * x_ipow::apply(a); + } + else { + return a; + } } }; - template <> - struct x_ipow<0> - { - static constexpr bool FISSTATIC = true; +} // end namespace CNORXZInternal + +#include "arith.cc.h" - template - static inline T apply(T a) - { - return a; - } - }; - - /* - template - struct dynamic_function - { - static constexpr bool FISSTATIC = false; - - template - inline T apply(Us... args) - { - return f(args...); - } - }; - */ -} // end namespace MultiArrayHelper - #endif diff --git a/src/include/base_def.h b/src/include/base_def.h index 9628956..d5d87da 100644 --- a/src/include/base_def.h +++ b/src/include/base_def.h @@ -1,7 +1,7 @@ // -*- C++ -*- -#ifndef __base_def_h__ -#define __base_def_h__ +#ifndef __cxz_base_def_h__ +#define __cxz_base_def_h__ #include diff --git a/src/include/basic_types.h b/src/include/basic_types.h new file mode 100644 index 0000000..0c806be --- /dev/null +++ b/src/include/basic_types.h @@ -0,0 +1,18 @@ +#ifndef __cxz_basic_types_h__ +#define __cxz_basic_types_h__ + +#include + +namespace CNORXZ +{ + + // just to fix this forever: + typedef int32_t Int; + typedef uint64_t SizeT; + typedef double Double; + typedef Double Real; + typedef std::string String; + +} + +#endif diff --git a/src/include/cnorxz.cc.h b/src/include/cnorxz.cc.h new file mode 100644 index 0000000..a431fd2 --- /dev/null +++ b/src/include/cnorxz.cc.h @@ -0,0 +1,15 @@ + +#include "ranges/ranges_header.cc.h" + +#include "container_index.cc.h" +#include "cxz_operation.cc.h" +#include "functional_array.cc.h" +#include "helper_tools.cc.h" +#include "map_range.cc.h" +#include "cxz_array_base.cc.h" +#include "cxz_array.cc.h" +#include "slice.cc.h" +#include "dynamic_operation.cc.h" +//#include "high_level_operation.cc.h" +//#include "expressions.cc.h" + diff --git a/src/include/cnorxz.h b/src/include/cnorxz.h new file mode 100644 index 0000000..6a655a5 --- /dev/null +++ b/src/include/cnorxz.h @@ -0,0 +1,20 @@ +// -*- C++ -*- + +#ifndef __cxz_cnorxz_h__ +#define __cxz_cnorxz_h__ + +#include +#include "container_index.h" +#include "cxz_operation.h" +#include "cxz_array_base.h" +#include "cxz_array.h" +#include "functional_array.h" +#include "helper_tools.h" +#include "operation_def.h" +#include "map_range.h" +#include "dynamic_operation.h" +//#include "high_level_operation.h" + +#include "cnorxz.cc.h" + +#endif diff --git a/src/include/container_index.cc.h b/src/include/container_index.cc.h new file mode 100644 index 0000000..20e79be --- /dev/null +++ b/src/include/container_index.cc.h @@ -0,0 +1,440 @@ + +#include "container_index.h" + +namespace CNORXZ +{ + + template + ConstContainerIndex::ConstContainerIndex(const ConstContainerIndex& in, bool copy) : + IB(in), + mNonTrivialBlocks(in.mNonTrivialBlocks), + mExternControl(false), + mBlockSizes(in.mBlockSizes), + mData(in.mData), + mObjPtrNum(in.mObjPtrNum), + mCPos(in.mCPos) + { + sfor_pn<0,sizeof...(Indices)> + ( [&](auto i) + { + typedef typename std::remove_reference(mIPack))>::type + SubType; + std::get(mIPack) = std::make_shared( in.template get() ) ; + return true; + }); + } + + template + ConstContainerIndex& ConstContainerIndex::copy(const ConstContainerIndex& in) + { + IB::operator=(in); + mNonTrivialBlocks = in.mNonTrivialBlocks; + mExternControl = false; + mBlockSizes = in.mBlockSizes; + mData = in.mData; + mObjPtrNum = in.mObjPtrNum; + mCPos = in.mCPos; + sfor_pn<0,sizeof...(Indices)> + ( [&](auto i) + { + typedef typename std::remove_reference(mIPack))>::type + SubType; + std::get(mIPack) = std::make_shared( in.template get() ) ; + return true; + }); + return *this; + } + + template + template + ConstContainerIndex::ConstContainerIndex(const std::shared_ptr& range, + std::intptr_t objPtrNum) : + IndexInterface,std::tuple >(range, 0), + mObjPtrNum(objPtrNum) + { + std::get(mBlockSizes) = 1; + sfor_mn + ( [&](auto i) { + auto r = range->template getPtr(); + std::get(mIPack) = r->beginPtr(); + *std::get(mIPack) = 0; + + std::get(mBlockSizes) = sfor_p + ( [&](auto j) { return std::get(mIPack)->max(); } , + [&](auto a, auto b) { return a * b; }); + return 0; + }); + + IB::mPos = sfor_m + ( [&](auto i) { return std::get(mIPack); }, + [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); + mCPos = RangeHelper::makePos(mIPack, mBlockSizes); + } + + template + template + ConstContainerIndex::ConstContainerIndex(const std::shared_ptr& range, + std::intptr_t objPtrNum, + const std::array& blockSizes) : + IndexInterface,std::tuple >(range, 0), + mObjPtrNum(objPtrNum) + { + sfor_mn + ( [&](auto i) { + auto r = range->template getPtr(); + std::get(mIPack) = r->beginPtr(); + *std::get(mIPack) = 0; + return 0; + }); + IB::mPos = sfor_m + ( [&](auto i) { return std::get(mIPack); }, + [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); + mCPos = RangeHelper::makePos(mIPack, mBlockSizes); + mNonTrivialBlocks = true; + } + + template + template + ConstContainerIndex& + ConstContainerIndex::operator=(const ConstContainerIndex& in) + { + mIPack = in.mIPack; + return (*this)(); + } + + + template + ConstContainerIndex& ConstContainerIndex::sync() + { + if(mExternControl){ + IB::mPos = sfor_m + ( [&](auto i) { return std::get(mIPack); }, + [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); + mCPos = RangeHelper::makePos(mIPack, mBlockSizes); + } + return *this; + } + + template + template + auto& ConstContainerIndex::get() const + { + return *std::get( mIPack ); + } + + template + template + auto ConstContainerIndex::getPtr() const + { + return std::get( mIPack ); + } + + template + ConstContainerIndex& ConstContainerIndex::operator()(const std::shared_ptr&... inds) + { + return (*this)(std::make_tuple(inds...)); + } + + template + ConstContainerIndex& ConstContainerIndex::operator()(const std::tuple...>& inds) + { + sfor_pn<0,sizeof...(Indices)> + ( [&](auto i) { std::get(mIPack) = std::get(inds); return 0; } ); + mExternControl = true; + return sync(); + } + + template + ConstContainerIndex& ConstContainerIndex::operator()() + { + return sync(); + } + + template + IndexType ConstContainerIndex::type() const { return IndexType::CONT; } + + template + ConstContainerIndex& ConstContainerIndex::operator++() + { + if(mExternControl){ + IB::mPos = sfor_m + ( [&](auto i) { return std::get(mIPack); }, + [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); + } + sfor_m + ( [&](auto i) { + auto& si = *std::get( mIPack ); + if(si.last() and i != 0) { si = 0; return true; } + else { ++si; return false; } + return false; + } ); + mCPos = RangeHelper::makePos(mIPack, mBlockSizes); + ++IB::mPos; + return *this; + } + + template + ConstContainerIndex& ConstContainerIndex::operator--() + { + if(mExternControl){ + IB::mPos = sfor_m + ( [&](auto i) { return std::get(mIPack); }, + [&](auto a, auto b) {return a->pos() + b*a->max();}, 0 ); + } + sfor_m + ( [&](auto i) { + auto& si = *std::get( mIPack ); + if(si.first() and i != 0) { si = si.max()-1; return true; } + else { --si; return false; } + return false; + } ); + mCPos = RangeHelper::makePos(mIPack, mBlockSizes); + --IB::mPos; + return *this; + + } + + template + ConstContainerIndex& ConstContainerIndex::operator=(size_t pos) + { + IB::mPos = pos; + RangeHelper::setIndexPack(mIPack, pos); + mCPos = RangeHelper::makePos(mIPack, mBlockSizes); + return *this; + } + + template + int ConstContainerIndex::pp(std::intptr_t idxPtrNum) + { + const int tmp = RangeHelper::ppx(mIPack, mBlockSizes, idxPtrNum); + IB::mPos += tmp; + return tmp; + } + + template + int ConstContainerIndex::mm(std::intptr_t idxPtrNum) + { + const int tmp = RangeHelper::mmx(mIPack, mBlockSizes, idxPtrNum); + IB::mPos -= tmp; + return tmp; + } + + template + std::string ConstContainerIndex::stringMeta() const + { + return std::dynamic_pointer_cast( IB::mRangePtr )->stringMeta(IB::mPos); + } + + template + typename ConstContainerIndex::MetaType ConstContainerIndex::meta() const + { + MetaType metaTuple; + sfor_pn<0,sizeof...(Indices)> + ( [&](auto i) { std::get(metaTuple) = std::get(mIPack)->meta(); return 0; } ); + return metaTuple; + } + + template + ConstContainerIndex& ConstContainerIndex::at(const MetaType& metaPos) + { + sfor_pn<0,sizeof...(Indices)> + ( [&](auto i) { std::get(mIPack)->at( std::get(metaPos) ); return 0; } ); + IB::mPos = RangeHelper::makePos(mIPack, mBlockSizes); + return *this; + } + + template + size_t ConstContainerIndex::dim() const + { + return sizeof...(Indices); + } + + template + bool ConstContainerIndex::first() const + { + return IB::pos() == 0; + } + + template + bool ConstContainerIndex::last() const + { + return IB::pos() == IB::mMax - 1; + } + + template + bool ConstContainerIndex::sliceMode() const + { + return mNonTrivialBlocks; + } + + template + std::shared_ptr::RangeType> + ConstContainerIndex::range() + { + return std::dynamic_pointer_cast( IB::mRangePtr ); + } + + template + size_t ConstContainerIndex::getStepSize(size_t n) + { + if(n >= sizeof...(Indices)){ + assert(0); + // throw !! + } + return mBlockSizes[n+1]; + } + + template + template + auto ConstContainerIndex::ifor(size_t step, Exprs exs) const + { + return RangeHelper::mkFor<0>(step, mIPack, mBlockSizes, exs); + } + + template + template + auto ConstContainerIndex::iforh(size_t step, Exprs exs) const + { + return RangeHelper::mkForh<0>(step, mIPack, mBlockSizes, exs); + } + + template + template + auto ConstContainerIndex::pifor(size_t step, Exprs exs) const + { + return RangeHelper::mkPFor<0>(step, mIPack, mBlockSizes, exs); + } + + template + std::intptr_t ConstContainerIndex::container() const + { + return mObjPtrNum; + } + + template + ConstContainerIndex& ConstContainerIndex:: + format(const std::array& blocks) + { + mBlockSizes = blocks; + mNonTrivialBlocks = true; + return *this; + } + + template + ConstContainerIndex& ConstContainerIndex::setData(const T* data) + { + mData = data; + return *this; + } + + template + const T& ConstContainerIndex::operator*() const + { + return mData[mCPos]; + } + + template + const T* ConstContainerIndex::operator->() const + { + return &mData[mCPos]; + } + + template + ConstContainerIndex ConstContainerIndex::operator++(int) + { + auto tmp = *this; + ++(*this); + return tmp; + } + + template + ConstContainerIndex ConstContainerIndex::operator--(int) + { + auto tmp = *this; + --(*this); + return tmp; + } + + template + ConstContainerIndex& ConstContainerIndex::operator+=(int diff) + { + if(diff < 0){ + for(int i = 0; i != diff; ++i){ + (*this)--; + } + } + else { + for(int i = 0; i != diff; ++i){ + (*this)++; + } + } + return *this; + } + + template + ConstContainerIndex& ConstContainerIndex::operator-=(int diff) + { + if(diff < 0){ + for(int i = 0; i != diff; ++i){ + (*this)++; + } + } + else { + for(int i = 0; i != diff; ++i){ + (*this)--; + } + } + return *this; + } + + template + ConstContainerIndex ConstContainerIndex::operator+(int num) const + { + auto tmp = *this; + return tmp += num; + } + + template + ConstContainerIndex ConstContainerIndex::operator-(int num) const + { + auto tmp = *this; + return tmp -= num; + } + + template + int ConstContainerIndex::operator-(const ConstContainerIndex& it) const + { + return static_cast( IB::mPos ) - static_cast( it.pos() ); + } + + template + const T& ConstContainerIndex::operator[](int num) const + { + return mData[IB::mPos + num]; + } + + template + bool ConstContainerIndex::operator<(const ConstContainerIndex& it) const + { + return IB::mPos < it.pos(); + } + + template + bool ConstContainerIndex::operator>(const ConstContainerIndex& it) const + { + return IB::mPos > it.pos(); + } + + template + bool ConstContainerIndex::operator<=(const ConstContainerIndex& it) const + { + return IB::mPos <= it.pos(); + } + + template + bool ConstContainerIndex::operator>=(const ConstContainerIndex& it) const + { + return IB::mPos >= it.pos(); + } + +} // namespace CNORXZ diff --git a/src/include/container_index.h b/src/include/container_index.h new file mode 100644 index 0000000..c47d1d6 --- /dev/null +++ b/src/include/container_index.h @@ -0,0 +1,307 @@ +// -*- C++ -*- + +#ifndef __cxz_container_index_h__ +#define __cxz_container_index_h__ + +#include +#include +#include + +#include "ranges/range_base.h" +#include "ranges/index_base.h" +#include "mbase_def.h" +#include "statics/static_for.h" +#include "ranges/range_helper.h" + +namespace CNORXZ +{ + + template + class ConstContainerIndex : public IndexInterface, + std::tuple >, + public std::iterator + { + public: + + typedef IndexInterface, + std::tuple > IB; + typedef std::tuple MetaType; + typedef std::tuple...> IndexPack; + typedef ContainerRange RangeType; + + static constexpr IndexType sType() { return IndexType::CONT; } + static constexpr size_t sDim() { return sizeof...(Indices); } + static constexpr size_t totalDim() { return (... * Indices::totalDim()); } + + static constexpr SpaceType STYPE = SpaceType::ANY; + static constexpr bool PARALLEL = std::tuple_element<0,std::tuple>::type::PARALLEL; + + template + using CIX = ConstContainerIndex; + + template + friend class CIX; + + private: + + ConstContainerIndex() = default; + + bool mNonTrivialBlocks = false; + bool mExternControl = false; + IndexPack mIPack; + std::array mBlockSizes; + const T* mData = nullptr; + std::intptr_t mObjPtrNum; + + protected: + size_t mCPos; + + public: + + ConstContainerIndex(const ConstContainerIndex& in) = default; + ConstContainerIndex& operator=(const ConstContainerIndex& in) = default; + + ConstContainerIndex(const ConstContainerIndex& in, bool copy); + + ConstContainerIndex& copy(const ConstContainerIndex& in); + + template + ConstContainerIndex& operator=(const ConstContainerIndex& in); + + template + ConstContainerIndex(const std::shared_ptr& range, + std::intptr_t objPtrNum); + + template + ConstContainerIndex(const std::shared_ptr& range, + std::intptr_t objPtrNum, + const std::array& blockSizes); + + + template + size_t getBlockSize() const { return std::get(mBlockSizes); } + + const IndexPack& pack() const { return mIPack; } + + ConstContainerIndex& sync(); // recalculate 'IB::mPos' when externalControl == true + ConstContainerIndex& operator()(const std::shared_ptr&... inds); // control via external indices + ConstContainerIndex& operator()(const std::tuple...>& inds); + ConstContainerIndex& operator()(); // -> sync; just to shorten the code + + // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== + + IndexType type() const; + + ConstContainerIndex& operator++(); + ConstContainerIndex& operator--(); + + ConstContainerIndex& operator=(size_t pos); + + int pp(std::intptr_t idxPtrNum); + int mm(std::intptr_t idxPtrNum); + + std::string stringMeta() const; + MetaType meta() const; + ConstContainerIndex& at(const MetaType& metaPos); + + size_t dim() const; + bool first() const; + bool last() const; + bool sliceMode() const; + + std::shared_ptr range(); + + template + auto& get() const; + + template + auto getPtr() const; + + size_t getStepSize(size_t n); + + template + auto ifor(size_t step, Exprs exs) const; + + template + auto iforh(size_t step, Exprs exs) const; + + template + auto pifor(size_t step, Exprs exs) const; + + std::intptr_t container() const; + ConstContainerIndex& format(const std::array& blocks); + + // Iterator Stuff + + ConstContainerIndex& setData(const T* data); + + const T& operator*() const; + const T* operator->() const; + //T& operator*(); + //T* operator->(); + + ConstContainerIndex operator++(int); + ConstContainerIndex operator--(int); + ConstContainerIndex& operator+=(int diff); + ConstContainerIndex& operator-=(int diff); + ConstContainerIndex operator+(int num) const; + ConstContainerIndex operator-(int num) const; + + int operator-(const ConstContainerIndex& it) const; + const T& operator[](int num) const; + + bool operator<(const ConstContainerIndex& it) const; + bool operator>(const ConstContainerIndex& it) const; + bool operator<=(const ConstContainerIndex& it) const; + bool operator>=(const ConstContainerIndex& it) const; + + }; + + template + class ContainerIndex : public ConstContainerIndex + { + public: + + typedef ConstContainerIndex CCI; + typedef CCI IB; + typedef typename CCI::MetaType MetaType; + typedef typename CCI::IndexPack IndexPack; + typedef typename CCI::RangeType RangeType; + + static constexpr IndexType sType() { return CCI::sType(); } + static constexpr size_t sDim() { return CCI::sDim(); } + static constexpr size_t totalDim() { return CCI::totalDim(); } + + static constexpr SpaceType STYPE = CCI::STYPE; + static constexpr bool PARALLEL = CCI::PARALLEL; + + template + using CIX = ContainerIndex; + + template + friend class CIX; + + private: + + ContainerIndex() = default; + + T* mMData = nullptr; + + public: + + ContainerIndex(const ContainerIndex& in) = default; + ContainerIndex& operator=(const ContainerIndex& in) = default; + + ContainerIndex(const ContainerIndex& in, bool copy) : CCI(in,copy) + { mMData = in.mMData; } + + ContainerIndex(const ConstContainerIndex& in, T* data) : CCI(in) + { mMData = data; } + + ContainerIndex(const ConstContainerIndex& in, T* data, bool copy) : + CCI(in,copy) + { mMData = data; } + + ContainerIndex& copy(const ContainerIndex& in) + { CCI::copy(in); mMData = in.mMData; } + + template + ContainerIndex& operator=(const ContainerIndex& in) + { CCI::operator=(in); return *this; } + + template + ContainerIndex(const std::shared_ptr& range, + std::intptr_t objPtrNum) : CCI(range, objPtrNum) {} + + template + ContainerIndex(const std::shared_ptr& range, + std::intptr_t objPtrNum, + const std::array& blockSizes) + : CCI(range, objPtrNum, blockSizes) {} + + + template + size_t getBlockSize() const { return CCI::template getBlockSize(); } + + const IndexPack& pack() const { CCI::pack(); return *this; } + + ContainerIndex& sync() { return CCI::sync(); return *this; } + ContainerIndex& operator()(const std::shared_ptr&... inds) + { CCI::operator()(inds...); return *this; } + ContainerIndex& operator()(const std::tuple...>& inds) + { CCI::operator()(inds); return *this; } + ContainerIndex& operator()() { CCI::operator()(); return *this; } + + // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== + + IndexType type() const { return CCI::type(); } + + ContainerIndex& operator++() { CCI::operator++(); return *this; } + ContainerIndex& operator--() { CCI::operator--(); return *this; } + + ContainerIndex& operator=(size_t pos) { CCI::operator=(pos); return *this; } + + int pp(std::intptr_t idxPtrNum) { return CCI::pp(idxPtrNum); } + int mm(std::intptr_t idxPtrNum) { return CCI::mm(idxPtrNum); } + + std::string stringMeta() const { return CCI::stringMeta; } + MetaType meta() const { return CCI::meta(); } + ContainerIndex& at(const MetaType& metaPos) { CCI::at(metaPos); return *this; } + + size_t dim() const { return CCI::dim(); } + bool first() const { return CCI::first(); } + bool last() const { return CCI::last(); } + bool sliceMode() const { return CCI::sliceMode(); } + + std::shared_ptr range() { return CCI::range(); } + + template + auto& get() const { return CCI::template get(); } + + template + auto getPtr() const { return CCI::template getPtr(); } + + size_t getStepSize(size_t n) { return getStepSize(n); } + + template + auto ifor(size_t step, Exprs exs) const { return CCI::ifor(step, exs); } + + template + auto iforh(size_t step, Exprs exs) const { return CCI::iforh(step, exs); } + + template + auto pifor(size_t step, Exprs exs) const { return CCI::pifor(step, exs); } + + std::intptr_t container() const { return CCI::container(); } + ContainerIndex& format(const std::array& blocks) + { CCI::format(blocks); return *this; } + + // Iterator Stuff + + ContainerIndex& setData(T* data) { CCI::setData(data); mMData = data; return *this; } + + const T& operator*() const { return CCI::operator*(); } + const T* operator->() const { return CCI::operator->(); } + T& operator*() { return mMData[CCI::mCPos]; } + T* operator->() { return &mMData[CCI::mCPos]; } + + ContainerIndex operator++(int) { auto tmp = *this; ++(*this); return tmp; } + ContainerIndex operator--(int) { auto tmp = *this; --(*this); return tmp; } + ContainerIndex& operator+=(int diff) { CCI::operator+=(diff); return *this; } + ContainerIndex& operator-=(int diff) { CCI::operator-=(diff); return *this; } + ContainerIndex operator+(int num) const { CCI::operator+(num); return *this; } + ContainerIndex operator-(int num) const { CCI::operator-(num); return *this; } + + int operator-(const ContainerIndex& it) const { return CCI::operator-(it); } + const T& operator[](int num) const { return CCI::operator[](num); } + + bool operator<(const ContainerIndex& it) const { return CCI::operator<(it); } + bool operator>(const ContainerIndex& it) const { return CCI::operator>(it); } + bool operator<=(const ContainerIndex& it) const { return CCI::operator<=(it); } + bool operator>=(const ContainerIndex& it) const { return CCI::operator>=(it); } + + }; + +} // end namespace CNORXZ + +#endif diff --git a/src/include/conversions.h b/src/include/conversions.h index 5cb06c7..2b1dc78 100644 --- a/src/include/conversions.h +++ b/src/include/conversions.h @@ -1,11 +1,11 @@ -#ifndef __ma_conversions_h__ -#define __ma_conversions_h__ +#ifndef __cxz_conversions_h__ +#define __cxz_conversions_h__ -#include "multi_array.h" +#include "cxz_array.h" #include "slice.h" -namespace MultiArrayTools +namespace CNORXZ { namespace ConversionSizes @@ -94,7 +94,7 @@ namespace MultiArrayTools } template - auto tcast(MultiArray& ma) + auto tcast(Array& ma) -> decltype(rangeTpToSlice ( rtcast( ma.range()->space() ), reinterpret_cast( ma.data() ) )) @@ -107,7 +107,7 @@ namespace MultiArrayTools } template - auto tcast(const MultiArray& ma) + auto tcast(const Array& ma) -> decltype(rangeTpToSlice ( rtcast( ma.range()->space() ), reinterpret_cast( ma.data() ) )) diff --git a/src/include/cxz_array.cc.h b/src/include/cxz_array.cc.h new file mode 100644 index 0000000..797a991 --- /dev/null +++ b/src/include/cxz_array.cc.h @@ -0,0 +1,269 @@ + +#include "cxz_array.h" +#include "statics/static_for.h" + +namespace CNORXZ +{ + template + Scalar scalar(const T& in) + { + NullRF nrf; + return Scalar( std::dynamic_pointer_cast( nrf.create() ), vector( { in } ) ); + } + + /******************* + * Array * + *******************/ + + template + Array::Array(const typename CRange::Space& space) : + MutableArrayBase(space), + mCont(MAB::mRange->size()) + { + MAB::mInit = true; + } + + template + Array::Array(const typename CRange::Space& space, + const vector& vec) : + MutableArrayBase(space), + mCont(vec) + { + MAB::mInit = true; + if(mCont.size() > MAB::mRange->size()){ + mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); + } + } + + + template + Array::Array(const std::shared_ptr&... ranges) : + MutableArrayBase(ranges...), + mCont(MAB::mRange->size()) + { + MAB::mInit = true; + } + + template + Array::Array(const std::shared_ptr&... ranges, const T& val) : + MutableArrayBase(ranges...), + mCont(MAB::mRange->size(), val) + { + MAB::mInit = true; + } + + template + Array::Array(const std::shared_ptr&... ranges, const vector& vec) : + MutableArrayBase(ranges...), + mCont(vec) + { + MAB::mInit = true; + if(mCont.size() > MAB::mRange->size()){ + mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); + } + } + + template + Array::Array(const std::shared_ptr&... ranges, vector&& vec) : + MutableArrayBase(ranges...), + mCont(std::forward>(vec)) + { + MAB::mInit = true; + if(mCont.size() > MAB::mRange->size()){ + mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end()); + } + } + + template + template + Array::Array(const std::shared_ptr&... ranges, Array&& in) : + MutableArrayBase(ranges...), + mCont( std::move( in.mCont ) ) + { + // maybe some checks here in the future... + assert(mCont.size() == MAB::mRange->size()); + MAB::mInit = true; + in.mInit = false; + } + + template + Array::Array(Array&& ama, SIZET... sizes) : + MutableArrayBase + ( ama.range()->template get<0>().template scast(sizes...)->space() ), + mCont( std::move( ama.mCont ) ) + { + MAB::mInit = true; + ama.mInit = false; + } + + template + T& Array::operator[](const IndexType& i) + { + return mCont[ i.pos() ]; + } + + template + const T& Array::operator[](const IndexType& i) const + { + return mCont[ i.pos() ]; + } + + template + T& Array::at(const typename IndexType::MetaType& meta) + { + return mCont[ MAB::cbegin().at(meta).pos() ]; + } + + template + const T& Array::at(const typename IndexType::MetaType& meta) const + { + return mCont[ MAB::cbegin().at(meta).pos() ]; + } + + template + bool Array::isConst() const + { + return false; + } + + template + bool Array::isSlice() const + { + return false; + } + + template + template + Array Array::format(const std::shared_ptr&... nrs) + { + //MAB::mInit = false; + return Array( nrs... , mCont ); + } + + template + template + Array Array::format(const std::tuple...>& nrs) + { + //MAB::mInit = false; + return Array( nrs , mCont ); + } + + template + template + Slice Array::slformat(const std::shared_ptr&... nrs) + { + return Slice( nrs..., mCont.data() ); + } + + template + template + ConstSlice Array::slformat(const std::shared_ptr&... nrs) const + { + return ConstSlice( nrs..., mCont.data() ); + } + + template + const T* Array::data() const + { + return mCont.data(); + } + + template + T* Array::data() + { + return mCont.data(); + } + + template + std::shared_ptr > Array::anonymous(bool slice) const + { + AnonymousRangeFactory arf(MAB::mRange->space()); + if(slice){ + return std::make_shared > + ( std::dynamic_pointer_cast( arf.create() ), + data() ); + } + else { + return std::make_shared > + ( std::dynamic_pointer_cast( arf.create() ), + mCont ); + } + } + + template + Array& Array::operator=(const T& in) + { + for(auto& x: mCont){ + x = in; + } + return *this; + } + + template + Array& Array::operator+=(const Array& in) + { + if(not MAB::mInit){ // not initialized by default constructor !! + (*this) = in; + } + else { + sfor_p<0,sizeof...(SRanges),0> + ( [&](auto i) { return std::get(MAB::mRange->space()).get() == std::get(in.mRange->space()).get(); }, + [&](auto a, auto b) { return a and b; }); + for(size_t i = 0; i != mCont.size(); ++i){ + mCont[i] += in.mCont[i]; + } + } + return *this; + } + + template + Array& Array::operator-=(const Array& in) + { + if(not MAB::mInit){ // not initialized by default constructor !! + (*this) = in; + } + else { + sfor_p<0,sizeof...(SRanges),0> + ( [&](auto i) { return std::get(MAB::mRange->space()).get() == std::get(in.mRange->space()).get(); }, + [&](auto a, auto b) { return a and b; }); + for(size_t i = 0; i != mCont.size(); ++i){ + mCont[i] -= in.mCont[i]; + } + } + return *this; + } + + template + Array& Array::operator*=(const T& in) + { + for(auto& x: mCont){ + x *= in; + } + return *this; + } + + template + Array& Array::operator/=(const T& in) + { + for(auto& x: mCont){ + x /= in; + } + return *this; + } + + template + Array::operator T() const + { + //static_assert( sizeof...(SRanges) == 1, "try to cast non-scalar type into scalar" ); + // TODO: check that SIZE is statically = 1 !!! + return mCont[0]; + } + + template + auto Array::cat() const + -> decltype(ArrayCatter::cat(*this)) + { + return ArrayCatter::cat(*this); + } +} + diff --git a/src/include/cxz_array.h b/src/include/cxz_array.h new file mode 100644 index 0000000..7b5471f --- /dev/null +++ b/src/include/cxz_array.h @@ -0,0 +1,151 @@ +// -*- C++ -*- + +#ifndef __cxz_array_h__ +#define __cxz_array_h__ + +#include + +#include "cxz_array_base.h" +#include "ranges/anonymous_range.h" + +namespace CNORXZ +{ + template + struct ArrayCatter; + + + template + struct ArrayCatter + { + template + static auto cat(const Array& ma) + -> Array + { + return ma; + } + }; + + template + class Array : public MutableArrayBase + { + public: + + typedef ContainerRange CRange; + typedef ArrayBase MAB; + typedef ConstContainerIndex IndexType; + + using ArrayBase::operator[]; + using MutableArrayBase::operator[]; + + DEFAULT_MEMBERS(Array); + Array(const std::shared_ptr&... ranges); + Array(const std::shared_ptr&... ranges, const T& val); + Array(const std::shared_ptr&... ranges, const vector& vec); + Array(const std::shared_ptr&... ranges, vector&& vec); + + template + Array(const std::shared_ptr&... ranges, Array&& in); // same effect as format + + Array(const typename CRange::Space& space); + Array(const typename CRange::Space& space, const vector& vec); + Array(Array&& ama, SIZET... sizes); + + // Only if ALL ranges have default extensions: + //Array(const vector& vec); + //Array(vector&& vec); + + // template + // Array(const Array,Range3> in); + + // implement contstructor using FunctionalArray as Input !!! + + //template + //Array& operator=(const Array,Range3> in); + + virtual T& operator[](const IndexType& i) final; + virtual const T& operator[](const IndexType& i) const final; + virtual T& at(const typename IndexType::MetaType& meta) override; + virtual const T& at(const typename IndexType::MetaType& meta) const override; + + virtual bool isConst() const override; + virtual bool isSlice() const override; + + template + Array format(const std::shared_ptr&... nrs); // reformat array using 'nr' which in + // total must have the same size as mRange + + template + Array format(const std::tuple...>& nrs); + + template + Slice slformat(const std::shared_ptr&... nrs); + + template + ConstSlice slformat(const std::shared_ptr&... nrs) const; + + virtual const T* data() const override; + virtual T* data() override; + virtual vector& vdata() { return mCont; } + virtual const vector& vdata() const { return mCont; } + vector&& vmove() { MAB::mInit = false; return std::move(mCont); } + + virtual std::shared_ptr > anonymous(bool slice = false) const override; + //virtual std::shared_ptr > anonymousMove() override; + + auto cat() const + -> decltype(ArrayCatter::cat(*this)); + + operator T() const; + + Array& operator=(const T& in); + + Array& operator+=(const Array& in); + Array& operator-=(const Array& in); + Array& operator*=(const T& in); + Array& operator/=(const T& in); + + template + friend class Array; + + private: + + vector mCont; + }; + + template + using Scalar = Array; + + template + Scalar scalar(const T& in); + + template + struct ArrayCatter > + { + template + static auto cat(const Array,Ranges...>& ma) + -> Array + { + auto sma = *ma.begin(); + const size_t smas = sma.size(); + const size_t mas = ma.size(); + auto cr = ma.range()->cat(sma.range()); + vector ov; + ov.reserve(mas * smas); + + for(auto& x: ma){ + assert(x.size() == smas); + ov.insert(ov.end(), x.vdata().begin(), x.vdata().end()); + } + return Array(cr->space(), std::move(ov)); + } + }; + + +} + +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ + + +#endif diff --git a/src/include/multi_array_base.cc.h b/src/include/cxz_array_base.cc.h similarity index 55% rename from src/include/multi_array_base.cc.h rename to src/include/cxz_array_base.cc.h index ff7a2cb..2f5552b 100644 --- a/src/include/multi_array_base.cc.h +++ b/src/include/cxz_array_base.cc.h @@ -1,15 +1,15 @@ -#include "multi_array_base.h" +#include "cxz_array_base.h" -namespace MultiArrayTools +namespace CNORXZ { /********************** - * MultiArrayBase * + * ArrayBase * **********************/ template - MultiArrayBase::MultiArrayBase(const MultiArrayBase& in) : + ArrayBase::ArrayBase(const ArrayBase& in) : mInit(in.mInit), mRange(in.mRange) { @@ -20,7 +20,7 @@ namespace MultiArrayTools template - MultiArrayBase::MultiArrayBase(MultiArrayBase&& in) : + ArrayBase::ArrayBase(ArrayBase&& in) : mInit(in.mInit), mRange(in.mRange) { @@ -30,7 +30,7 @@ namespace MultiArrayTools } template - MultiArrayBase& MultiArrayBase::operator=(const MultiArrayBase& in) + ArrayBase& ArrayBase::operator=(const ArrayBase& in) { mInit = in.mInit; mRange = in.mRange; @@ -41,7 +41,7 @@ namespace MultiArrayTools } template - MultiArrayBase& MultiArrayBase::operator=(MultiArrayBase&& in) + ArrayBase& ArrayBase::operator=(ArrayBase&& in) { mInit = in.mInit; mRange = in.mRange; @@ -52,24 +52,24 @@ namespace MultiArrayTools } template - MultiArrayBase::MultiArrayBase(const std::shared_ptr&... ranges) + ArrayBase::ArrayBase(const std::shared_ptr&... ranges) { - ContainerRangeFactory crf(ranges...); - mRange = std::dynamic_pointer_cast >( crf.create() ); + ContainerRangeFactory crf(ranges...); + mRange = std::dynamic_pointer_cast >( crf.create() ); mProtoI = std::make_shared( mRange, reinterpret_cast(this) ); } template - MultiArrayBase::MultiArrayBase(const typename CRange::Space& space) + ArrayBase::ArrayBase(const typename CRange::Space& space) { - ContainerRangeFactory crf(space); - mRange = std::dynamic_pointer_cast >( crf.create() ); + ContainerRangeFactory crf(space); + mRange = std::dynamic_pointer_cast >( crf.create() ); mProtoI = std::make_shared( mRange, reinterpret_cast(this) ); } template template - const T& MultiArrayBase::operator[](const ContainerIndex& i) + const T& ArrayBase::operator[](const ConstContainerIndex& i) { IndexType ii(*mProtoI); ii = i; @@ -77,7 +77,7 @@ namespace MultiArrayTools } template - const T& MultiArrayBase::operator[](const std::tuple...>& is) const + const T& ArrayBase::operator[](const std::tuple...>& is) const { IndexType ii(*mProtoI); ii(is); @@ -85,70 +85,62 @@ namespace MultiArrayTools } template - size_t MultiArrayBase::size() const + size_t ArrayBase::size() const { return mRange->size(); } template - typename MultiArrayBase::IndexType MultiArrayBase::begin() const + typename ArrayBase::CIndexType ArrayBase::begin() const { - IndexType i(*mProtoI,true); + return cbegin(); + } + + template + typename ArrayBase::CIndexType ArrayBase::end() const + { + return end(); + } + + template + typename ArrayBase::CIndexType ArrayBase::cbegin() const + { + CIndexType i(*mProtoI,true); i = 0; return i.setData(data()); } template - typename MultiArrayBase::IndexType MultiArrayBase::end() const + typename ArrayBase::CIndexType ArrayBase::cend() const { - IndexType i(*mProtoI,true); + CIndexType i(*mProtoI,true); i = i.max(); - //i = mRange->size(); - return i.setData(data()); - } - - template - typename MultiArrayBase::IndexType - MultiArrayBase::beginIndex() const - { - IndexType i(*mProtoI,true); - i = 0; return i.setData(data()); } template - typename MultiArrayBase::IndexType - MultiArrayBase::endIndex() const - { - IndexType i(*mProtoI,true); - i = i.max(); - //i = mRange->size(); - return i.setData(data()); - } - - template - const std::shared_ptr::CRange>& - MultiArrayBase::range() const + const std::shared_ptr::CRange>& + ArrayBase::range() const { return mRange; } template - bool MultiArrayBase::isConst() const + bool ArrayBase::isConst() const { return true; } template ConstOperationRoot - MultiArrayBase::operator()(const std::shared_ptr&... inds) const + ArrayBase::operator()(const std::shared_ptr&... inds) const { return ConstOperationRoot(*this, inds...); } template ConstOperationRoot - MultiArrayBase::op(const std::shared_ptr& ind) const + ArrayBase::op(const std::shared_ptr& ind) const { return ConstOperationRoot(data(), *ind); } @@ -156,7 +148,7 @@ namespace MultiArrayTools template template ConstOperationRoot - MultiArrayBase::m(const std::shared_ptr&... inds) const + ArrayBase::m(const std::shared_ptr&... inds) const { static_assert(sizeof...(SRanges) == sizeof...(MappedRanges), "number of mapped ranges must be equal to number of original ranges"); @@ -164,14 +156,14 @@ namespace MultiArrayTools } template - bool MultiArrayBase::isInit() const + bool ArrayBase::isInit() const { return mInit; } template template - auto MultiArrayBase::getRangePtr() const + auto ArrayBase::getRangePtr() const -> decltype(mRange->template getPtr()) { return mRange->template getPtr(); @@ -179,35 +171,20 @@ namespace MultiArrayTools /****************************** - * MutableMultiArrayBase * + * MutableArrayBase * ******************************/ template - MutableMultiArrayBase::MutableMultiArrayBase(const std::shared_ptr&... ranges) : - MultiArrayBase(ranges...) {} + MutableArrayBase::MutableArrayBase(const std::shared_ptr&... ranges) : + ArrayBase(ranges...) {} template - MutableMultiArrayBase::MutableMultiArrayBase(const typename CRange::Space& space) : - MultiArrayBase(space) {} - /* - template - typename MutableMultiArrayBase::IndexType MutableMultiArrayBase::begin() - { - auto i = mRange->begin(); - return i.setData(data()); - } - - template - typename MutableMultiArrayBase::IndexType MutableMultiArrayBase::end() - { - auto i = mRange->end(); - return i.setData(data()); - } - */ + MutableArrayBase::MutableArrayBase(const typename CRange::Space& space) : + ArrayBase(space) {} template template - T& MutableMultiArrayBase::operator[](const ContainerIndex& i) + T& MutableArrayBase::operator[](const ConstContainerIndex& i) { IndexType ii(*MAB::mProtoI); ii = i; @@ -215,44 +192,60 @@ namespace MultiArrayTools } template - T& MutableMultiArrayBase::operator[](const std::tuple...>& is) + T& MutableArrayBase::operator[](const std::tuple...>& is) { - IndexType ii(*MAB::mProtoI); + IndexType ii(*MAB::mProtoI,this->data()); ii(is); return (*this)[ii]; } + template + typename MutableArrayBase::IndexType MutableArrayBase::begin() + { + IndexType i(*MAB::mProtoI,this->data(),true); + i = 0; + return i.setData(data()); + } template - bool MutableMultiArrayBase::isConst() const + typename MutableArrayBase::IndexType MutableArrayBase::end() + { + IndexType i(*MAB::mProtoI,this->data(),true); + i = i.max(); + return i.setData(data()); + } + + + template + bool MutableArrayBase::isConst() const { return false; } template OperationRoot - MutableMultiArrayBase::operator()(const std::shared_ptr&... inds) + MutableArrayBase::operator()(const std::shared_ptr&... inds) { return OperationRoot(*this, inds...); } template OperationRoot - MutableMultiArrayBase::op(const std::shared_ptr& ind) + MutableArrayBase::op(const std::shared_ptr& ind) { return OperationRoot(data(), *ind); } template ConstOperationRoot - MutableMultiArrayBase::operator()(const std::shared_ptr&... inds) const + MutableArrayBase::operator()(const std::shared_ptr&... inds) const { return ConstOperationRoot(*this, inds...); } template ConstOperationRoot - MutableMultiArrayBase::op(const std::shared_ptr& ind) const + MutableArrayBase::op(const std::shared_ptr& ind) const { return ConstOperationRoot(data(), *ind); } @@ -260,7 +253,7 @@ namespace MultiArrayTools template template OperationRoot - MutableMultiArrayBase::m(const std::shared_ptr&... inds) + MutableArrayBase::m(const std::shared_ptr&... inds) { static_assert(sizeof...(SRanges) == sizeof...(MappedRanges), "number of mapped ranges must be equal to number of original ranges"); @@ -270,12 +263,12 @@ namespace MultiArrayTools template template ConstOperationRoot - MutableMultiArrayBase::m(const std::shared_ptr&... inds) const + MutableArrayBase::m(const std::shared_ptr&... inds) const { static_assert(sizeof...(SRanges) == sizeof...(MappedRanges), "number of mapped ranges must be equal to number of original ranges"); return ConstOperationRoot(*this, inds...); } -} // end namespace MultiArrayTools +} // end namespace CNORXZ diff --git a/src/include/multi_array_base.h b/src/include/cxz_array_base.h similarity index 64% rename from src/include/multi_array_base.h rename to src/include/cxz_array_base.h index b04ed8d..4e91609 100644 --- a/src/include/multi_array_base.h +++ b/src/include/cxz_array_base.h @@ -1,6 +1,6 @@ -#ifndef __multi_array_base_h__ -#define __multi_array_base_h__ +#ifndef __cxz_array_base_h__ +#define __cxz_array_base_h__ #include #include @@ -13,7 +13,7 @@ #include "ranges/rheader.h" -namespace MultiArrayTools +namespace CNORXZ { template @@ -51,38 +51,39 @@ namespace MultiArrayTools // Explicitely specify subranges in template argument !!! template - class MultiArrayBase + class ArrayBase { public: typedef T value_type; - typedef ContainerRange CRange; + typedef ContainerRange CRange; + typedef ConstContainerIndex CIndexType; typedef ContainerIndex IndexType; protected: bool mInit = false; std::shared_ptr mRange; - std::shared_ptr mProtoI; + std::shared_ptr mProtoI; public: - //DEFAULT_MEMBERS(MultiArrayBase); - MultiArrayBase(const std::shared_ptr&... ranges); - MultiArrayBase(const typename CRange::Space& space); + //DEFAULT_MEMBERS(ArrayBase); + ArrayBase(const std::shared_ptr&... ranges); + ArrayBase(const typename CRange::Space& space); - MultiArrayBase() = default; - MultiArrayBase(const MultiArrayBase& in); - MultiArrayBase(MultiArrayBase&& in); - MultiArrayBase& operator=(const MultiArrayBase& in); - MultiArrayBase& operator=(MultiArrayBase&& in); + ArrayBase() = default; + ArrayBase(const ArrayBase& in); + ArrayBase(ArrayBase&& in); + ArrayBase& operator=(const ArrayBase& in); + ArrayBase& operator=(ArrayBase&& in); - virtual ~MultiArrayBase() = default; + virtual ~ArrayBase() = default; template - const T& operator[](const ContainerIndex& i); + const T& operator[](const ConstContainerIndex& i); const T& operator[](const std::tuple...>& is) const; - virtual const T& operator[](const IndexType& i) const = 0; + virtual const T& operator[](const CIndexType& i) const = 0; virtual const T& at(const typename CRange::IndexType::MetaType& meta) const = 0; virtual const T* data() const = 0; @@ -90,20 +91,19 @@ namespace MultiArrayTools virtual size_t size() const; virtual bool isSlice() const = 0; - virtual IndexType begin() const; - virtual IndexType end() const; + virtual CIndexType begin() const; + virtual CIndexType end() const; + virtual CIndexType cbegin() const; + virtual CIndexType cend() const; - virtual IndexType beginIndex() const; - virtual IndexType endIndex() const; - virtual const std::shared_ptr& range() const; virtual bool isConst() const; - virtual std::shared_ptr > anonymous(bool slice = false) const = 0; + virtual std::shared_ptr > anonymous(bool slice = false) const = 0; virtual ConstOperationRoot - op(const std::shared_ptr& ind) const; + op(const std::shared_ptr& ind) const; virtual ConstOperationRoot operator()(const std::shared_ptr&... inds) const; @@ -121,48 +121,49 @@ namespace MultiArrayTools }; template - class MutableMultiArrayBase : public MultiArrayBase + class MutableArrayBase : public ArrayBase { public: - typedef ContainerRange CRange; - typedef MultiArrayBase MAB; + typedef ContainerRange CRange; + typedef ArrayBase MAB; typedef ContainerIndex IndexType; + typedef ConstContainerIndex CIndexType; - using MultiArrayBase::operator[]; - using MultiArrayBase::at; - using MultiArrayBase::data; - using MultiArrayBase::begin; - using MultiArrayBase::end; + using ArrayBase::operator[]; + using ArrayBase::at; + using ArrayBase::data; + using ArrayBase::begin; + using ArrayBase::end; + using ArrayBase::cbegin; + using ArrayBase::cend; - DEFAULT_MEMBERS(MutableMultiArrayBase); - MutableMultiArrayBase(const std::shared_ptr&... ranges); - MutableMultiArrayBase(const typename CRange::Space& space); + DEFAULT_MEMBERS(MutableArrayBase); + MutableArrayBase(const std::shared_ptr&... ranges); + MutableArrayBase(const typename CRange::Space& space); template - T& operator[](const ContainerIndex& i); + T& operator[](const ConstContainerIndex& i); T& operator[](const std::tuple...>& is); - virtual T& operator[](const IndexType& i) = 0; + virtual T& operator[](const CIndexType& i) = 0; virtual T& at(const typename CRange::IndexType::MetaType& meta) = 0; virtual T* data() = 0; - //virtual IndexType begin(); - //virtual IndexType end(); + virtual IndexType begin(); + virtual IndexType end(); virtual bool isConst() const override; - //virtual std::shared_ptr > anonymousMove() = 0; - virtual ConstOperationRoot - op(const std::shared_ptr& ind) const override; + op(const std::shared_ptr& ind) const override; virtual ConstOperationRoot operator()(const std::shared_ptr&... inds) const override; virtual OperationRoot - op(const std::shared_ptr& ind); + op(const std::shared_ptr& ind); virtual OperationRoot operator()(const std::shared_ptr&... inds); @@ -177,7 +178,7 @@ namespace MultiArrayTools }; -} // end namespace MultiArrayTools +} // end namespace CNORXZ /* ========================= * * --- TEMPLATE CODE --- * diff --git a/src/include/ma_assert.h b/src/include/cxz_assert.h similarity index 100% rename from src/include/ma_assert.h rename to src/include/cxz_assert.h diff --git a/src/include/multi_array_operation.cc.h b/src/include/cxz_operation.cc.h similarity index 62% rename from src/include/multi_array_operation.cc.h rename to src/include/cxz_operation.cc.h index 183bb70..7d50b1b 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/cxz_operation.cc.h @@ -1,15 +1,18 @@ -#include "multi_array_operation.h" +#include "cxz_operation.h" +#include "xfor/exttype.h" +#include "ranges/range_helper.h" + /* ========================= * * --- TEMPLATE CODE --- * * ========================= */ -namespace MultiArrayTools +namespace CNORXZ { namespace { - using namespace MultiArrayHelper; + using namespace CNORXZInternal; } /*************************** @@ -48,7 +51,6 @@ namespace MultiArrayTools template template auto OperationBase::c(const std::shared_ptr& ind) const - -> Contraction { return Contraction(THIS(), ind); } @@ -56,28 +58,17 @@ namespace MultiArrayTools template template auto OperationBase::sl(const std::shared_ptr&... inds) const - -> ConstSlice { ConstSlice out(inds->range()...); out.define(inds...) = THIS(); return out; } - template - template - auto OperationBase::slc(const std::shared_ptr&... inds) const - -> SliceContraction - { - return SliceContraction - (THIS(), inds...); - } - template template auto OperationBase::p(const std::shared_ptr&... inds) const - -> ConstOperationRoot { - auto ma = std::make_shared> + auto ma = std::make_shared> (inds->range()... , static_cast(0)); (*ma)(inds...) = THIS(); return ConstOperationRoot(ma, inds...); @@ -86,9 +77,8 @@ namespace MultiArrayTools template template auto OperationBase::to(const std::shared_ptr&... inds) const - -> MultiArray { - MultiArray out(inds->range()...); + Array out(inds->range()...); out(inds...) = THIS(); return out; } @@ -96,9 +86,8 @@ namespace MultiArrayTools template template auto OperationBase::addto(const std::shared_ptr&... inds) const - -> MultiArray { - MultiArray out(inds->range()..., + Array out(inds->range()..., static_cast(0)); out(inds...) += THIS(); return out; @@ -107,9 +96,8 @@ namespace MultiArrayTools template template auto OperationBase::pto(const std::shared_ptr&... inds) const - -> MultiArray { - MultiArray out(inds->range()...); + Array out(inds->range()...); out(inds...).par() = THIS(); return out; } @@ -117,9 +105,8 @@ namespace MultiArrayTools template template auto OperationBase::paddto(const std::shared_ptr&... inds) const - -> MultiArray { - MultiArray out(inds->range()..., + Array out(inds->range()..., static_cast(0)); out(inds...).par() += THIS(); return out; @@ -129,93 +116,112 @@ namespace MultiArrayTools template auto OperationBase::a(const std::shared_ptr>& ll, const Args&... args) const - -> Operation,OperationClass, Args...> { return Operation,OperationClass, Args...>(ll, THIS(), args...); } template auto OperationBase::ptr() const - -> OperationPointer { return OperationPointer(THIS()); } - + /* + template + template + auto OperationBase::ho(const std::shared_ptr& inds...) const + { + typedef XX SubOp; + return HyperOperation() + } + */ + + /************************ + * AccessTemplate * + ************************/ + + template + auto AccessTemplate::get(size_t pos) + { + return THIS().get(pos); + } + + template + auto AccessTemplate::get(size_t pos) const + { + return THIS().get(pos); + } + + template + auto AccessTemplate::oget(size_t pos) const + { + return THIS().oget(pos); + } + + template + template + void AccessTemplate::exec(size_t pos, const Op& op, ExtType e) const + { + return THIS().template exec(pos,op,e); + } + /************************ * AssignmentExpr * ************************/ - template - struct OpIndexResolve - {}; - - template <> - struct OpIndexResolve + template + inline size_t opIndexResolve(size_t start, ExtType last) { - - template - static inline size_t get(size_t start, ExtType last) - { + if constexpr(OIA == OpIndexAff::EXTERN){ return last.val(); } - - }; - - template <> - struct OpIndexResolve - { - template - static inline size_t get(size_t start, ExtType last) - { + if constexpr(OIA == OpIndexAff::TARGET){ return start; } - }; + return 0; + } - - template - AssignmentExpr2::AssignmentExpr2(T* dataPtr, const Target& tar, const OpClass& sec) : - mTar(tar), mSec(sec), mDataPtr(dataPtr) {} - - template - inline void AssignmentExpr2::operator()(size_t start) + template + AssignmentExpr::AssignmentExpr(const AccessTemplate& dataAcc, const Target& tar, const OpClass& sec) : + mTar(tar), mSec(sec), mDataAcc(static_cast(dataAcc)) {} + + template + inline void AssignmentExpr::operator()(size_t start) { ExtType last = rootSteps(); last.zero(); - mDataPtr[OpIndexResolve::get(start,last)] = mSec.get(last.next()); - //mDataPtr[last.val()] = mSec.get(last.next()); + // TODO: ask MA container for data (ptr)!!! + mDataAcc.template exec(opIndexResolve(start,last),mSec,last.next()); } - template - inline void AssignmentExpr2::operator()(size_t start, ExtType last) + template + inline void AssignmentExpr::operator()(size_t start, ExtType last) { - //CHECK; - mDataPtr[OpIndexResolve::get(start,last)] = mSec.get(last.next()); + mDataAcc.template exec(opIndexResolve(start,last),mSec,last.next()); } - template - typename AssignmentExpr2::ExtType - AssignmentExpr2::rootSteps(std::intptr_t iPtrNum) const + template + typename AssignmentExpr::ExtType + AssignmentExpr::rootSteps(std::intptr_t iPtrNum) const { return mTar.rootSteps(iPtrNum).extend( mSec.rootSteps(iPtrNum) ); } - template - inline void AssignmentExpr2::operator()(size_t mlast, DExt last) + template + inline void AssignmentExpr::operator()(size_t mlast, DExt last) { (*this)(mlast, std::dynamic_pointer_cast>(last)->ext()); } - template - inline DExt AssignmentExpr2::dRootSteps(std::intptr_t iPtrNum) const + template + inline DExt AssignmentExpr::dRootSteps(std::intptr_t iPtrNum) const { return std::make_shared>(rootSteps(iPtrNum)); } - template - inline DExt AssignmentExpr2::dExtension() const + template + inline DExt AssignmentExpr::dExtension() const { - CHECK; return nullptr; //???!!! } @@ -225,31 +231,58 @@ namespace MultiArrayTools static_assert(SIZE == sizeof...(Ops), "size missmatch"); } + template + inline size_t MOpGetX( ExtType last, const std::tuple& etp) + { + if constexpr(N > 0){ + std::get(etp).get(last); + return MOpGetX(last.next(),etp); + } + else { + std::get(etp).get(last); + return 0; + } + } + + template + inline void MOpSetX( ExtType last, std::tuple& etp) + { + if constexpr(N > 0){ + std::get(etp).set(last); + MOpSetX(last.next(),etp); + } + else { + std::get(etp).set(last); + } + } + template inline size_t MOp::get(ExtType last) const { - return RootSumN::get(last,mOps); + return MOpGetX(last,mOps.mOps); } - + template inline MOp& MOp::set(ExtType last) { - RootSumN::set(last,mOps); + MOpSetX(last,mOps.mOps); return *this; } template template auto MOp::loop(Expr exp) const - -> decltype(PackNum::mkLoop( mOps, exp)) { - return PackNum::mkLoop( mOps, exp); + return sfor_m + ( [&](auto i){ return std::get(mOps.mOps); }, + [&](auto f, auto next) { return f.loop(next); }, + exp ); } template auto MOp::rootSteps(std::intptr_t iPtrNum) const -> ExtType { - return RootSumN::rootSteps(mOps,iPtrNum); + return mOps.rootSteps(iPtrNum); } template @@ -304,59 +337,13 @@ namespace MultiArrayTools return nullptr; //???!!! } - template - AddExpr::AddExpr(T* dataPtr, const Target& tar, const OpClass& sec) : - mTar(tar), mSec(sec), mDataPtr(dataPtr) {} - - template - inline void AddExpr::operator()(size_t start, ExtType last) - { - mDataPtr[OpIndexResolve::get(start,last)] += mSec.get(last.next()); - //mDataPtr[start] += mSec.get(last.next()); - //mDataPtr[start] += mSec.template get(last); - } - - template - inline void AddExpr::operator()(size_t start) - { - ExtType last = rootSteps(); - last.zero(); - mDataPtr[OpIndexResolve::get(start,last)] += mSec.get(last.next()); - } - - template - typename AddExpr::ExtType AddExpr::rootSteps(std::intptr_t iPtrNum) const - { - return mTar.rootSteps(iPtrNum).extend( mSec.rootSteps(iPtrNum) ); - //return mSec.rootSteps(iPtrNum); - } - - template - inline void AddExpr::operator()(size_t mlast, DExt last) - { - (*this)(mlast, std::dynamic_pointer_cast>(last)->ext()); - } - - template - inline DExt AddExpr::dRootSteps(std::intptr_t iPtrNum) const - { - return std::make_shared>(rootSteps(iPtrNum)); - } - - template - inline DExt AddExpr::dExtension() const - { - CHECK; - return nullptr; //???!!! - } - /**************************** * ConstOperationRoot * ****************************/ template ConstOperationRoot:: - ConstOperationRoot(const MultiArrayBase& ma, + ConstOperationRoot(const ArrayBase& ma, const std::shared_ptr&... indices) : mDataPtr(ma.data()), mOrigDataPtr(ma.data()), @@ -368,7 +355,7 @@ namespace MultiArrayTools template ConstOperationRoot:: - ConstOperationRoot(std::shared_ptr > maptr, + ConstOperationRoot(std::shared_ptr > maptr, const std::shared_ptr&... indices) : mDataPtr(maptr->data()), mOrigDataPtr(maptr->data()), @@ -377,7 +364,6 @@ namespace MultiArrayTools { mIndex(indices...); mDataPtr = mOrigDataPtr + mIndex.pos(); - //mOff = mIndex.pos(); } template @@ -388,22 +374,26 @@ namespace MultiArrayTools mIndex( ind ) { mDataPtr = mOrigDataPtr + mIndex.pos(); - //mOff = mIndex.pos(); } template template inline const T& ConstOperationRoot::get(ET pos) const { - return mDataPtr[pos.val()/*+mOff*/]; + return mDataPtr[pos.val()]; + } + + template + template + inline const V& ConstOperationRoot::vget(ET pos) const + { + return *(reinterpret_cast(mDataPtr+pos.val())); } template template inline ConstOperationRoot& ConstOperationRoot::set(ET pos) { - //mIndex = pos.val(); - //mDataPtr = mOrigDataPtr + mIndex.pos(); mDataPtr = mOrigDataPtr + pos.val(); return *this; } @@ -411,16 +401,13 @@ namespace MultiArrayTools template const T* ConstOperationRoot::data() const { - //auto i = mIndex; - //return mOrigDataPtr/* + i().pos()*/; return mDataPtr; } template MExt ConstOperationRoot::rootSteps(std::intptr_t iPtrNum) const { - return MExt(getStepSize( mIndex, iPtrNum )); - //return MExt(getStepSize( getRootIndices( mIndex->info() ), iPtrNum )); + return MExt(RangeHelper::getStepSize( mIndex, iPtrNum )); } @@ -445,6 +432,14 @@ namespace MultiArrayTools return static_cast( mOp.get(pos) ); } + template + template + inline V StaticCast::vget(ET pos) const + { + assert(0); // !!! + return V(); + } + template template inline StaticCast& StaticCast::set(ET pos) @@ -483,25 +478,29 @@ namespace MultiArrayTools inline typename MetaOperationRoot::value_type MetaOperationRoot::get(ET pos) const { - //VCHECK(pos.val()); - //VCHECK(mDataPtr); - //VCHECK(mDataPtr[pos.val()]) return (mWorkIndex = pos.val()).meta(); } + template + template + inline V + MetaOperationRoot::vget(ET pos) const + { + assert(0); // !!! + return V(); + } + template template inline MetaOperationRoot& MetaOperationRoot::set(ET pos) { - //(*mIndex) = pos.val(); return *this; } template MExt MetaOperationRoot::rootSteps(std::intptr_t iPtrNum) const { - return MExt(getStepSize( *mIndex, iPtrNum )); - //return MExt(getStepSize( getRootIndices( mIndex->info() ), iPtrNum )); + return MExt(RangeHelper::getStepSize( *mIndex, iPtrNum )); } @@ -518,25 +517,29 @@ namespace MultiArrayTools template OperationRoot:: - OperationRoot(MutableMultiArrayBase& ma, + OperationRoot(MutableArrayBase& ma, const std::shared_ptr&... indices) : mDataPtr(ma.data()), mOrigDataPtr(ma.data()), + mDataAcc( ma.data(), ma.data() ), mIndex( ma.begin() ) { mIndex(indices...); + mDataAcc.set( mIndex.pos() ); mDataPtr = mOrigDataPtr + mIndex.pos(); } template OperationRoot:: - OperationRoot(MutableMultiArrayBase& ma, + OperationRoot(MutableArrayBase& ma, const std::tuple...>& indices) : mDataPtr(ma.data()), mOrigDataPtr(ma.data()), + mDataAcc( ma.data(), ma.data() ), mIndex( ma.begin() ) { mIndex(indices); + mDataAcc.set( mIndex.pos() ); mDataPtr = mOrigDataPtr + mIndex.pos(); } @@ -545,72 +548,104 @@ namespace MultiArrayTools OperationRoot(T* data, const IndexType& ind) : mDataPtr(data), mOrigDataPtr(data), + mDataAcc( data, data ), mIndex( ind ) { + mDataAcc.set( mIndex.pos() ); mDataPtr = mOrigDataPtr + mIndex.pos(); } + template + template + auto OperationRoot::asx(const OpClass& in) const + { + static_assert(Access::VSIZE > 0, "Access::VSIZE failed"); + return mIndex.ifor(1,this->template asxExpr(in)).template vec(); + } + + template + template + auto OperationRoot::asxExpr(const OpClass& in) const + { + static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); + return in.loop(AssignmentExpr,OperationRoot,OpClass> + (mDataAcc,*this,in)); + } + + template + template + auto OperationRoot::asx(const OpClass& in, const std::shared_ptr& i) const + { + static_assert(Access::VSIZE > 0, "Access::VSIZE failed"); + return i->ifor(1,this->template asxExpr(in)).template vec(); + } + template template auto OperationRoot::assign(const OpClass& in) const - -> decltype(mIndex.ifor(1,in.loop(AssignmentExpr2,OpClass,OpIndexAff::TARGET> - (mOrigDataPtr,*this,in)))) - { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return mIndex.ifor(1,in.loop(AssignmentExpr2,OpClass,OpIndexAff::TARGET> - (mOrigDataPtr,*this,in))); + return this->template asx,PointerAccess>(in); } template template auto OperationRoot::assignExpr(const OpClass& in) const - -> decltype(in.loop(AssignmentExpr2,OpClass> - (mOrigDataPtr,*this,in))) { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return in.loop(AssignmentExpr2,OpClass> - (mOrigDataPtr,*this,in)); + return this->template asxExpr,PointerAccess>(in); } template template auto OperationRoot::assign(const OpClass& in, const std::shared_ptr& i) const - -> decltype(i->ifor(1,in.loop(AssignmentExpr2,OpClass> - (mOrigDataPtr,*this,in)))) { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return i->ifor(1,in.loop(AssignmentExpr2,OpClass> - (mOrigDataPtr,*this,in))); + return this->template asx,PointerAccess>(in,i); } template template auto OperationRoot::plus(const OpClass& in) const - -> decltype(mIndex.ifor(1,in.loop(AddExpr,OpClass,OpIndexAff::TARGET> - (mOrigDataPtr,*this,in)))) { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return mIndex.ifor(1,in.loop(AddExpr,OpClass,OpIndexAff::TARGET> - (mOrigDataPtr,*this,in))); + return this->template asx,PointerAccess>(in); } template template auto OperationRoot::plus(const OpClass& in, const std::shared_ptr& i) const - -> decltype(i->ifor(1,in.loop(AddExpr,OpClass> - (mOrigDataPtr,*this,in)))) { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return i->ifor(1,in.loop(AddExpr,OpClass> - (mOrigDataPtr,*this,in))); + return this->template asx,PointerAccess>(in,i); + } + + template + inline void vexec(TarOp& th, const OpClass& in) + { + // VECTORIZATION!!! + // PARALLEL!!! + if constexpr(VABLE){ + CHECK; + typedef typename TarOp::value_type T; + auto x = th.template asx>(in); + //if(x.rootSteps(x.vI()) == 1){ + if(0){ + CHECK; + x(); + } + else { + th.template asx>(in)(); + } + } + else { + typedef typename TarOp::value_type T; + //IAccess> tmp; + th.template asx>(in)(); + } } template template OperationRoot& OperationRoot::operator=(const OpClass& in) { - assign(in)(); + //asx>(in)(); + vexec>(*this,in); return *this; } @@ -618,7 +653,8 @@ namespace MultiArrayTools template OperationRoot& OperationRoot::operator+=(const OpClass& in) { - plus(in)(); + //asx>(in)(); + vexec>(*this,in); return *this; } @@ -627,26 +663,32 @@ namespace MultiArrayTools { return operator= >(in); } - + /* template ParallelOperationRoot OperationRoot::par() { return ParallelOperationRoot(mOrigDataPtr, mIndex); } - + */ template template inline T& OperationRoot::get(ET pos) const { - return mDataPtr[pos.val()]; + return *mDataAcc.get(pos.val()); + } + + template + template + inline V& OperationRoot::vget(ET pos) const + { + return *(reinterpret_cast(mDataAcc.get(pos.val()))); } template template inline OperationRoot& OperationRoot::set(ET pos) { - //mIndex = pos.val(); - //mDataPtr = mOrigDataPtr + mIndex.pos(); + mDataAcc.set(pos.val()); mDataPtr = mOrigDataPtr + pos.val(); return *this; } @@ -654,7 +696,7 @@ namespace MultiArrayTools template MExt OperationRoot::rootSteps(std::intptr_t iPtrNum) const { - return MExt(getStepSize( mIndex, iPtrNum )); + return MExt(RangeHelper::getStepSize( mIndex, iPtrNum )); } template @@ -667,9 +709,7 @@ namespace MultiArrayTools template T* OperationRoot::data() const { - //auto i = mIndex; - //return mOrigDataPtr/* + i().pos()*/; - return mDataPtr; + return mDataAcc.get(0); } template @@ -684,14 +724,15 @@ namespace MultiArrayTools /******************************* * ParallelOperationRoot * - *******************************/ + ****************************** template ParallelOperationRoot:: - ParallelOperationRoot(MutableMultiArrayBase& ma, + ParallelOperationRoot(MutableArrayBase& ma, const std::shared_ptr&... indices) : mDataPtr(ma.data()), mOrigDataPtr(ma.data()), + mDataAcc( ma.data(), ma.data() ), mIndex( ma.begin() ) { mIndex(indices...); @@ -703,59 +744,79 @@ namespace MultiArrayTools ParallelOperationRoot(T* data, const IndexType& ind) : mDataPtr(data), mOrigDataPtr(data), + mDataAcc( data, data ), mIndex( ind ) { mDataPtr = mOrigDataPtr + mIndex.pos(); } template - template - auto ParallelOperationRoot::assign(const OpClass& in) - -> decltype(mIndex.pifor(1,in.loop(AssignmentExpr2,OpClass,OpIndexAff::TARGET> - (mOrigDataPtr,*this,in)))) - + template + auto ParallelOperationRoot::asx(const OpClass& in) const { - return mIndex.pifor(1,in.loop(AssignmentExpr2,OpClass,OpIndexAff::TARGET> - (mOrigDataPtr,*this,in))); + static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); + return mIndex.pifor(1,in.loop(AssignmentExpr,ParallelOperationRoot,OpClass,OpIndexAff::TARGET> + (mDataAcc,*this,in))).template vec(); } + template + template + auto ParallelOperationRoot::asxExpr(const OpClass& in) const + { + static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); + return in.loop(AssignmentExpr,ParallelOperationRoot,OpClass> + (mDataAcc,*this,in)); + } + + template + template + auto ParallelOperationRoot::asx(const OpClass& in, const std::shared_ptr& i) const + { + static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); + return i->pifor(1,in.loop(AssignmentExpr,ParallelOperationRoot,OpClass> + (mDataAcc,*this,in))).template vec(); + } + + template + template + auto ParallelOperationRoot::assign(const OpClass& in) const + { + return this->template asx>(in); + } + + template + template + auto ParallelOperationRoot::assignExpr(const OpClass& in) const + { + return this->template asxExpr>(in); + } + template template auto ParallelOperationRoot::assign(const OpClass& in, const std::shared_ptr& i) const - -> decltype(i->pifor(1,in.loop(AssignmentExpr2,OpClass> - (mOrigDataPtr,*this,in)))) { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return i->pifor(1,in.loop(AssignmentExpr2,OpClass> - (mOrigDataPtr,*this,in))); + return this->template asx>(in,i); } template template - auto ParallelOperationRoot::plus(const OpClass& in) - -> decltype(mIndex.pifor(1,in.loop(AddExpr,OpClass,OpIndexAff::TARGET> - (mOrigDataPtr,*this,in)))) + auto ParallelOperationRoot::plus(const OpClass& in) const { - return mIndex.pifor(1,in.loop(AddExpr,OpClass,OpIndexAff::TARGET> - (mOrigDataPtr,*this,in))); + return this->template asx>(in); } template template auto ParallelOperationRoot::plus(const OpClass& in, const std::shared_ptr& i) const - -> decltype(i->pifor(1,in.loop(AddExpr,OpClass> - (mOrigDataPtr,*this,in)))) { - static_assert( OpClass::SIZE == decltype(in.rootSteps())::SIZE, "Ext Size mismatch" ); - return i->pifor(1,in.loop(AddExpr,OpClass> - (mOrigDataPtr,*this,in))); + return this->template asx>(in,i); } template template ParallelOperationRoot& ParallelOperationRoot::operator=(const OpClass& in) { - assign(in)(); + vexec(*this,in); return *this; } @@ -763,7 +824,7 @@ namespace MultiArrayTools template ParallelOperationRoot& ParallelOperationRoot::operator+=(const OpClass& in) { - plus(in)(); + vexec(*this,in); return *this; } @@ -778,7 +839,14 @@ namespace MultiArrayTools template inline T& ParallelOperationRoot::get(ET pos) const { - return mDataPtr[pos.val()/*+mOff*/]; + return mDataPtr[pos.val()+mOff]; + } + + template + template + inline V& ParallelOperationRoot::vget(ET pos) const + { + return *(reinterpret_cast(mDataPtr+pos.val())); } template @@ -793,8 +861,7 @@ namespace MultiArrayTools template MExt ParallelOperationRoot::rootSteps(std::intptr_t iPtrNum) const { - return MExt(getStepSize( mIndex, iPtrNum )); - //return MExt(getStepSize( mIndex.info(), iPtrNum )); + return MExt(RangeHelper::getStepSize( mIndex, iPtrNum )); } template @@ -810,7 +877,7 @@ namespace MultiArrayTools auto i = mIndex; return mOrigDataPtr + i().pos(); } - + */ /************************ * OperationValue * @@ -826,6 +893,13 @@ namespace MultiArrayTools return mVal; } + template + template + inline V OperationValue::vget(ET pos) const + { + return static_cast(mVal); // implement???!!! + } + template template inline OperationValue& OperationValue::set(ET pos) @@ -898,40 +972,95 @@ namespace MultiArrayTools mOps(ops...), mF(ff) { - static_assert( not FISSTATIC, "using instance of function supposed to be static" ); + static_assert( not FISSTATIC, "using instance of static function" ); + } + + template + inline auto + mkOpExpr(std::shared_ptr f, const ETuple& pos, const OpTuple& ops, Args... args) + { + if constexpr(I == std::tuple_size{}){ + if constexpr(OpFunction::FISSTATIC){ + return OpFunction::apply(args...); + } + else { + (*f)(args...); + } + } + else { + typedef typename std::remove_reference(ops))>::type NextOpType; + return mkOpExpr + ( f, getX(pos), ops, args..., std::get(ops).get(pos)); + } + } + + template + inline auto + mkVOpExpr(std::shared_ptr f, const ETuple& pos, const OpTuple& ops, Args... args) + { + if constexpr(I == std::tuple_size{}){ + if constexpr(OpFunction::FISSTATIC){ + return VFunc::apply(args...); + } + else { + auto vf = mkVFuncPtr(f); + (*vf)(args...); + } + } + else { + typedef typename std::remove_reference(ops))>::type NextOpType; + return mkVOpExpr( f, getX(pos), ops, args..., + std::get(ops).template vget(pos)); + } } template template inline auto Operation::get(ET pos) const { - typedef std::tuple OpTuple; - return PackNum:: - template mkOpExpr(mF, pos, mOps); + return mkOpExpr<0>(mF, pos, mOps.mOps); } + template + template + inline auto Operation::vget(ET pos) const + { + return mkVOpExpr<0,V>(mF, pos, mOps.mOps); + } + + template + static inline void setOpPos(OpTuple& ot, const ETuple& et) + { + if constexpr(I != std::tuple_size{}){ + typedef typename std::remove_reference(ot))>::type NextOpType; + std::get( ot ).set( et ); + setOpPos(ot, getX(et)); + } + } + template template inline Operation& Operation::set(ET pos) { - typedef std::tuple OpTuple; - PackNum::template setOpPos(mOps,pos); + setOpPos<0>(mOps.mOps,pos); return *this; } template auto Operation::rootSteps(std::intptr_t iPtrNum) const - -> decltype(PackNum::mkSteps(iPtrNum, mOps)) + -> ExtType { - return PackNum::mkSteps(iPtrNum, mOps); + return mOps.rootSteps(iPtrNum); } template template auto Operation::loop(Expr exp) const - -> decltype(PackNum::mkLoop( mOps, exp )) { - return PackNum::mkLoop( mOps, exp ); + return sfor_m + ( [&](auto i){ return std::get(mOps.mOps); }, + [&](auto f, auto next) { return f.loop(next); }, + exp ); } @@ -953,6 +1082,14 @@ namespace MultiArrayTools return mOp.template get(pos); } + template + template + inline auto Contraction::vget(ET pos) const + -> decltype(mOp.template vget(pos)) + { + return mOp.template vget(pos); + } + template template inline Contraction& Contraction::set(ET pos) @@ -976,50 +1113,4 @@ namespace MultiArrayTools return mInd->iforh(0,mOp.loop(exp)); } - /************************** - * SliceContraction * - **************************/ - - template - SliceContraction::SliceContraction(const Op& op, - std::shared_ptr... ind) : - mOp(op), - mCont(std::make_shared >(ind->range()...)), - mTarOp(*mCont,ind...) - { } - - // forward loop !!!! - template - template - inline const MultiArray& - SliceContraction::get(ET pos) const - { - *mCont = 0; - mOp.set(pos); - mTarOp = mOp; - return *mCont; - } - - template - template - inline SliceContraction& SliceContraction::set(ET pos) - { - mOp.set(pos); - return *this; - } - - template - auto SliceContraction::rootSteps(std::intptr_t iPtrNum) const - -> decltype(mOp.rootSteps(iPtrNum)) - { - return mOp.rootSteps(iPtrNum); - } - - template - template - auto SliceContraction::loop(Expr exp) const -> decltype(mOp.loop(exp)) - { - return mOp.loop(exp); - } - } diff --git a/src/include/multi_array_operation.h b/src/include/cxz_operation.h similarity index 54% rename from src/include/multi_array_operation.h rename to src/include/cxz_operation.h index 01887cd..474f529 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/cxz_operation.h @@ -1,30 +1,32 @@ // -*- C++ -*- -#ifndef __multi_array_operation_h__ -#define __multi_array_operation_h__ +#ifndef __cxz_operation_h__ +#define __cxz_operation_h__ #include #include #include #include #include +#include #include "base_def.h" #include "mbase_def.h" #include "ranges/rheader.h" -#include "pack_num.h" #include "arith.h" - #include "xfor/xfor.h" +#include "type_operations.h" -namespace MultiArrayTools +#include "statics/static_for.h" + +namespace CNORXZ { namespace { - using namespace MultiArrayHelper; + using namespace CNORXZInternal; } template @@ -48,43 +50,30 @@ namespace MultiArrayTools auto operator/(const OperationBase& in) const; template - auto c(const std::shared_ptr& ind) const - -> Contraction; + auto c(const std::shared_ptr& ind) const; template - auto sl(const std::shared_ptr&... inds) const - -> ConstSlice; + auto sl(const std::shared_ptr&... inds) const; template - auto slc(const std::shared_ptr&... inds) const - -> SliceContraction; + auto p(const std::shared_ptr&... inds) const; template - auto p(const std::shared_ptr&... inds) const - -> ConstOperationRoot; - - template - auto to(const std::shared_ptr&... inds) const - -> MultiArray; + auto to(const std::shared_ptr&... inds) const; template - auto addto(const std::shared_ptr&... inds) const - -> MultiArray; + auto addto(const std::shared_ptr&... inds) const; template - auto pto(const std::shared_ptr&... inds) const - -> MultiArray; + auto pto(const std::shared_ptr&... inds) const; template - auto paddto(const std::shared_ptr&... inds) const - -> MultiArray; + auto paddto(const std::shared_ptr&... inds) const; template // Args = Operation Classes - auto a(const std::shared_ptr>& ll, const Args&... args) const - -> Operation,OperationClass, Args...>; + auto a(const std::shared_ptr>& ll, const Args&... args) const; - auto ptr() const - -> OperationPointer; + auto ptr() const; private: friend OperationClass; @@ -101,97 +90,6 @@ namespace MultiArrayTools friend OperationClass; }; - template - size_t sumRootNum() - { - return typename Op::rootNum(); - } - - template - size_t sumRootNum() - { - return typename Op1::rootNum() + sumRootNum(); - } - - template - struct RootSumN - { - template - struct rs - { - static constexpr size_t SIZE = Op1::SIZE + RootSumN::template rs::SIZE; - }; - - template - static inline auto rootSteps(const std::tuple& etp, std::intptr_t i) - { - return RootSumN::rootSteps(etp,i).extend( std::get(etp).rootSteps(i) ); - } - - template - static inline void exec( size_t start, ExtType last, std::tuple& etp) - { - std::get(etp)(start,last); - RootSumN::exec(start,last.next(),etp); - } - - template - static inline size_t get( ExtType last, const std::tuple& etp) - { - std::get(etp).get(last); - return RootSumN::get(last.next(),etp); - } - - template - static inline void set( ExtType last, std::tuple& etp) - { - std::get(etp).set(last); - RootSumN::set(last.next(),etp); - } - }; - - template <> - struct RootSumN<0> - { - template - struct rs - { - static constexpr size_t SIZE = Op1::SIZE; - }; - - template - static inline auto rootSteps(const std::tuple& etp, std::intptr_t i) - { - return std::get<0>(etp).rootSteps(i); - } - - template - static inline void exec( size_t start, ExtType last, std::tuple& etp) - { - std::get(etp)(start,last); - } - - template - static inline size_t get( ExtType last, const std::tuple& etp) - { - std::get(etp).get(last); - return 0; - } - - template - static inline void set( ExtType last, std::tuple& etp) - { - std::get(etp).set(last); - } - }; - - - template - struct RootSum - { - static constexpr size_t SIZE = RootSumN::template rs::SIZE; - }; - template struct SelfIdentity @@ -206,32 +104,241 @@ namespace MultiArrayTools EXTERN = 0, TARGET = 1 }; + + template + struct VType + { + typedef T type; + static constexpr size_t MULT = sizeof(type)/sizeof(T); + }; + + template <> + struct VType + { + typedef v256 type; + static constexpr size_t MULT = sizeof(type)/sizeof(double); + }; + + template