From 1ab0c21667a22b4a4fb2760c94ab2a230f25cedc Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Fri, 30 Dec 2022 13:32:27 +0100 Subject: [PATCH] first array operation test --- CMakeLists.txt | 2 - src/include/array/array_base.cc.h | 34 +++----- src/include/array/array_base.h | 14 +--- src/include/cnorxz.cc.h | 1 + src/include/cnorxz.h | 2 + src/include/operation/op_types.cc.h | 19 ++++- src/include/operation/op_types.h | 5 ++ src/include/ranges/index_utils.cc.h | 117 ---------------------------- src/include/ranges/index_utils.h | 18 ----- src/tests/marray_unit_test.cc | 44 +++++++---- 10 files changed, 67 insertions(+), 189 deletions(-) delete mode 100644 src/include/ranges/index_utils.cc.h delete mode 100644 src/include/ranges/index_utils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e58182f..4f77b8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,8 +11,6 @@ else() endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++17 -Wpedantic -O2 -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") enable_testing() diff --git a/src/include/array/array_base.cc.h b/src/include/array/array_base.cc.h index de82ef6..43e0ad2 100644 --- a/src/include/array/array_base.cc.h +++ b/src/include/array/array_base.cc.h @@ -71,19 +71,11 @@ namespace CNORXZ } template - template - COpRoot CArrayBase::operator()(const IndexPtr& i) const + template + COpRoot CArrayBase::operator()(const Sptr& i) const { - auto fi = this->cbegin().format( std::static_pointer_cast(i) ); - return COpRoot(*this, fi); - } - - template - template - COpRoot CArrayBase::operator()(const Sptr>& i) const - { - auto fi = this->cbegin().format( i ); - return COpRoot>(*this, fi); + CXZ_WARNING("FORMAT / BLOCKSIZES!!!"); + return coproot(*this, i); } /***************** @@ -107,7 +99,7 @@ namespace CNORXZ template T& ArrayBase::at(const IndexInterface& i) { - CXZ_ASSERT(i.les() < this->size(), "index out of range"); + CXZ_ASSERT(i.lex() < this->size(), "index out of range"); // check further compatibility of index/range format!!! auto ai = this->begin() + i.lex(); return *ai; @@ -136,19 +128,11 @@ namespace CNORXZ } template - template - OpRoot ArrayBase::operator()(const IndexPtr& i) + template + OpRoot ArrayBase::operator()(const Sptr& i) { - auto fi = this->cbegin().format( std::static_pointer_cast(i) ); - return OpRoot(*this, fi); - } - - template - template - OpRoot ArrayBase::operator()(const Sptr>& i) - { - auto fi = this->cbegin().format( i ); - return OpRoot>(*this, fi); + CXZ_WARNING("FORMAT / BLOCKSIZES!!!"); + return oproot(*this, i); } } diff --git a/src/include/array/array_base.h b/src/include/array/array_base.h index 741a744..65d5ad0 100644 --- a/src/include/array/array_base.h +++ b/src/include/array/array_base.h @@ -49,11 +49,8 @@ namespace CNORXZ virtual bool isView() const = 0; - template - COpRoot operator()(const IndexPtr& i) const; - - template - COpRoot operator()(const Sptr>& i) const; + template + COpRoot operator()(const Sptr& i) const; }; @@ -91,11 +88,8 @@ namespace CNORXZ virtual iterator begin(); virtual iterator end(); - template - OpRoot operator()(const IndexPtr& i); - - template - OpRoot operator()(const Sptr>& i); + template + OpRoot operator()(const Sptr& i); }; diff --git a/src/include/cnorxz.cc.h b/src/include/cnorxz.cc.h index 6a2de0e..46e5a5c 100644 --- a/src/include/cnorxz.cc.h +++ b/src/include/cnorxz.cc.h @@ -3,3 +3,4 @@ #include "memory/memory.cc.h" #include "ranges/ranges.cc.h" #include "array/array.cc.h" +#include "operation/operation.cc.h" diff --git a/src/include/cnorxz.h b/src/include/cnorxz.h index 3a99d10..3177ab2 100644 --- a/src/include/cnorxz.h +++ b/src/include/cnorxz.h @@ -6,6 +6,8 @@ #include "base/base.h" #include "memory/memory.h" #include "ranges/ranges.h" +#include "array/array.h" +#include "operation/operation.h" #include "cnorxz.cc.h" diff --git a/src/include/operation/op_types.cc.h b/src/include/operation/op_types.cc.h index d641f8d..f09cd97 100644 --- a/src/include/operation/op_types.cc.h +++ b/src/include/operation/op_types.cc.h @@ -111,7 +111,12 @@ namespace CNORXZ return mData; } - + template + constexpr decltype(auto) coproot(const CArrayBase& a, const Sptr& ind) + { + return COpRoot(a, ind); + } + /**************** * OpCont * ****************/ @@ -250,7 +255,7 @@ namespace CNORXZ template constexpr OpRoot& OpRoot::operator=(const Op& o) { - a(mIndex, [](auto& a, const auto& b) { a = b; }, o); + OI::a(mIndex, [](auto& a, const auto& b) { a = b; }, o); return *this; } @@ -258,14 +263,14 @@ namespace CNORXZ template constexpr OpRoot& OpRoot::operator+=(const Op& o) { - a(mIndex, [](auto& a, const auto& b) { a += b; }, o); + OI::a(mIndex, [](auto& a, const auto& b) { a += b; }, o); return *this; } template constexpr OpRoot& OpRoot::operator=(const OpRoot& o) { - a(mIndex, [](auto& a, const auto& b) { a = b; }, o); + OI::a(mIndex, [](auto& a, const auto& b) { a = b; }, o); return *this; } @@ -307,6 +312,12 @@ namespace CNORXZ return mData; } + template + constexpr decltype(auto) oproot(ArrayBase& a, const Sptr& ind) + { + return OpRoot(a, ind); + } + /******************* * Operation * *******************/ diff --git a/src/include/operation/op_types.h b/src/include/operation/op_types.h index bc6607c..0624425 100644 --- a/src/include/operation/op_types.h +++ b/src/include/operation/op_types.h @@ -91,6 +91,9 @@ namespace CNORXZ Sptr mIndex; }; + template + constexpr decltype(auto) coproot(const CArrayBase& a, const Sptr& ind); + template class OpCont : public OpInterface> { @@ -169,6 +172,8 @@ namespace CNORXZ }; + template + constexpr decltype(auto) oproot(ArrayBase& a, const Sptr& ind); template class Operation : public OpInterface> diff --git a/src/include/ranges/index_utils.cc.h b/src/include/ranges/index_utils.cc.h deleted file mode 100644 index c5c1967..0000000 --- a/src/include/ranges/index_utils.cc.h +++ /dev/null @@ -1,117 +0,0 @@ - -#ifndef __cxz_index_utils_cc_h__ -#define __cxz_index_utils_cc_h__ - -#include "index_utils.h" - -namespace CNORZX -{ - template - constexpr decltype(auto) getIndexDepth(const Index& ind) - { - static_assert(is_index::value, "got non-index type"); - if constexpr(has_sub::value){ - if constexpr(has_static_sub::value){ - constexpr SizeT D = index_dim::value; - return iter<0,D> - ( [&](auto i) { return getIndexDepth(*std::get(ind.pack())); }, - [](auto... e) { - if constexpr( is_integral_constant::value and ... ){ - return std::integral_constant {}; - } - else { - return std::max({e...}); - } - } ); - } - else { - SizeT o = 0; - if(ind.dim() == 1){ - auto p = ind.pack(); - if(p.size() == 0){ - o = 1; - } - else { - o = getIndexDepth(*p[0]); - } - } - else { - for(SizeT i = 0; i != ind.dim(); ++i){ - const SizeT e = getIndexDepth(*ind.pack()[i]); - if(e > o){ - o = e; - } - } - } - return o; - } - } - else { - return std::integral_constant {}; - } - } - - template - constexpr decltype(auto) getDimension(const Index& ind, IntT depth) - { - static_assert(is_index::value, "got non-index type"); - if constexpr(std::is_same,IntT>::value) { - return std::integral_constant {} - } - else { - auto iminus = [](auto i) { - if constexpr( is_integral_constant::value ) { - return std::integral_constant {} - } - else { - return i-1; - } - }; - if constexpr(has_sub::value){ - if constexpr(has_static_sub::value and is_integral_constant::value){ - constexpr SizeT D = index_dim::value; - return iter<0,D> - ( [&](auto i) { return getDimension(*std::get(ind.pack()), - iminus(depth)); }, - [](auto... e) { - if constexpr( is_integral_constant::value and ... ){ - return std::integral_constant {}; - } - else { - return (e + ...); - } - } ); - } - else { - SizeT o = 0; - if(depth == 0u){ - o = 1; - } - else if(ind.dim() == 1){ - auto p = ind.pack(); - if(p.size() == 0){ - o = 1; - } - else { - o = getDimension(*p[0], iminus(depth)); - } - } - else { - for(SizeT i = 0; i != ind.dim(); ++i){ - const SizeT e = getDimension(*ind.pack()[i], iminus(depth)); - if(e > o){ - o = e; - } - } - } - return o; - } - } - else { - return std::integral_constant {}; - } - } - } -} - -#endif diff --git a/src/include/ranges/index_utils.h b/src/include/ranges/index_utils.h deleted file mode 100644 index 4c2ab55..0000000 --- a/src/include/ranges/index_utils.h +++ /dev/null @@ -1,18 +0,0 @@ - -#ifndef __cxz_index_utils_h__ -#define __cxz_index_utils_h__ - -#include "base/base.h" - -namespace CNORZX -{ - template - constexpr decltype(auto) getIndexDepth(const Index& ind); - - template - constexpr decltype(auto) getDimension(const Index& ind, IntT depth); - - -} - -#endif diff --git a/src/tests/marray_unit_test.cc b/src/tests/marray_unit_test.cc index ab85ecf..d0b5f50 100644 --- a/src/tests/marray_unit_test.cc +++ b/src/tests/marray_unit_test.cc @@ -21,10 +21,12 @@ namespace { mSize = 7; mCR1 = CRangeFactory(mSize).create(); + mA = MArray(mCR1, Numbers::get(0,mSize)); } SizeT mSize; RangePtr mCR1; + MArray mA; }; class MA_2D_Test : public ::testing::Test @@ -37,46 +39,62 @@ namespace mStrMeta = { "another", "test", "string", "vector", "for", "this", "Test" }; mCR1 = CRangeFactory(mSize).create(); mUR1 = URangeFactory(mStrMeta).create(); + const SizeT ssize = mStrMeta.size(); + const SizeT size = mSize * ssize; + mA = MArray(mCR1*mUR1, Numbers::get(0,size)); + mA2 = MArray(mUR1*mCR1, Numbers::get(0,size)); } SizeT mSize; Vector mStrMeta; RangePtr mCR1; RangePtr mUR1; + MArray mA; + MArray mA2; }; - + TEST_F(MA_1D_Test, Basics) { - const MArray a(mCR1, Numbers::get(0,mSize)); auto crx = std::dynamic_pointer_cast(mCR1); - EXPECT_EQ(a.size(), mSize); - EXPECT_FALSE(a.isView()); + EXPECT_EQ(mA.size(), mSize); + EXPECT_FALSE(mA.isView()); auto ei = crx->end(); EXPECT_EQ(ei.lex(), mSize); for(auto i = crx->begin(); i != ei; ++i){ EXPECT_EQ(i.lex(), i.pos()); - EXPECT_EQ(a[i], a.data()[i.lex()]); - EXPECT_EQ(a.at(i), a.data()[i.lex()]); + EXPECT_EQ(mA[i], mA.data()[i.lex()]); + EXPECT_EQ(mA.at(i), mA.data()[i.lex()]); } - EXPECT_THROW({a.at(ei);}, std::runtime_error); + EXPECT_THROW({mA.at(ei);}, std::runtime_error); } TEST_F(MA_2D_Test, Basics) { const SizeT ssize = mStrMeta.size(); const SizeT size = mSize * ssize; - const MArray a(mCR1*mUR1, Numbers::get(0,size)); - EXPECT_EQ(a.range()->dim(), 2u); - EXPECT_EQ(a.size(), size); - EXPECT_EQ(a.range()->sub(0), mCR1); - EXPECT_EQ(a.range()->sub(1), mUR1); + EXPECT_EQ(mA.range()->dim(), 2u); + EXPECT_EQ(mA.size(), size); + EXPECT_EQ(mA.range()->sub(0), mCR1); + EXPECT_EQ(mA.range()->sub(1), mUR1); auto cr1x = std::dynamic_pointer_cast(mCR1); auto ur1x = std::dynamic_pointer_cast>(mUR1); for(auto ci = cr1x->begin(); ci != cr1x->end(); ++ci){ for(auto ui = ur1x->begin(); ui != ur1x->end(); ++ui){ const SizeT p = ci.lex()*ssize + ui.lex(); - EXPECT_EQ( a[ci*ui], a.data()[p] ); + EXPECT_EQ( mA[ci*ui], mA.data()[p] ); + } + } + } + + TEST_F(MA_2D_Test, Operation) + { + auto uip1 = std::make_shared>(mUR1); + auto cip1 = std::make_shared(mCR1); + mA2(uip1*cip1) = mA(cip1*uip1); + for(auto ui = mUR1->begin(); ui != mUR1->end(); ++ui){ + for(auto ci = mCR1->begin(); ci != mCR1->end(); ++ci){ + EXPECT_EQ(mA2[ui*ci], mA[ci*ui]); } } }