diff --git a/src/include/ranges/mrange.cc.h b/src/include/ranges/mrange.cc.h index 34785c1..f1fc5d6 100644 --- a/src/include/ranges/mrange.cc.h +++ b/src/include/ranges/mrange.cc.h @@ -412,9 +412,14 @@ namespace CNORXZ template decltype(auto) GMIndex::stepSize(const IndexId& id) const { + // TODO: also for YRange!!! + const auto own = [&]() { + if constexpr(I != 0){ return SPos<0> {}; } + else { return UPos(id == this->id() ? 1 : 0); } + }; return iter<0,NI> ( [&](auto i) { return mIPack[i]->stepSize(id) * format()[i]; }, - [](const auto&... ss) { return ( ss + ... ); }); + [](const auto&... ss) { return ( ss + ... ); }) + own(); } template diff --git a/src/include/xpr/pos_type.cc.h b/src/include/xpr/pos_type.cc.h index 98c6409..b201807 100644 --- a/src/include/xpr/pos_type.cc.h +++ b/src/include/xpr/pos_type.cc.h @@ -215,7 +215,6 @@ namespace CNORXZ template constexpr UPos FPos::operator()(const PosT1& a) const { - assert(0); return UPos(mExt * mMap[a.val()]); } diff --git a/src/opt/mpi/include/rrange.cc.h b/src/opt/mpi/include/rrange.cc.h index 1463ced..277a471 100644 --- a/src/opt/mpi/include/rrange.cc.h +++ b/src/opt/mpi/include/rrange.cc.h @@ -205,7 +205,12 @@ namespace CNORXZ template decltype(auto) RIndex::stepSize(const IndexId& id) const { - return getRankStepSize(id); + const auto own = [&]() { + if constexpr(I != 0){ return SPos<0> {}; } + else { return UPos(id == this->id() ? 1 : 0); } + }; + return mI->stepSize(id) + own(); + //return getRankStepSize(id); } template @@ -249,9 +254,14 @@ namespace CNORXZ MPI_Allgather(&lex, 1, MPI_UNSIGNED_LONG, lexs.data(), 1, MPI_UNSIGNED_LONG, MPI_COMM_WORLD); SizeT root = 0; - for(; root != lexs.size() and lexs[root] == mI->lmax().val(); ++root) {} + for(; root != lexs.size(); ++root) { + if(lexs[root] != mI->lmax().val()){ + break; + } + } if(root == lexs.size()){ // metaPos not in rrange *this = lmax().val(); + VCHECK(toString(metaPos)); assert(0); } else { diff --git a/src/opt/mpi/include/rrange.h b/src/opt/mpi/include/rrange.h index ed23e38..df92ef4 100644 --- a/src/opt/mpi/include/rrange.h +++ b/src/opt/mpi/include/rrange.h @@ -162,31 +162,6 @@ namespace CNORXZ Sptr mI; /**< Index on the local range of the THIS rank. */ Sptr mK; /**< Multi-index indicating the current rank. */ - mutable std::map> mRankMap; - - template - decltype(auto) getRankStepSize(const IndexId& id) const - { - auto ss = mI->stepSize(id); - if constexpr(std::is_same::value){ - if(mRankMap.count(id.id()) != 0){ - return FPos(ss.val(), mRankMap[id.id()].data(), ss.max(), ss.max2()); - } - else { - Vector mp(ss.max()); - const SizeT b = mI->pmax().val() / ss.val(); - for(SizeT i = 0; i != mp.size(); ++i){ - //max2 = num ranks in this dir (preliminary solution)!!! - mp[i] = ( ( ss.map()[i] / ss.max() ) % ss.max2() ) * b + ss.map()[i]; - } - mRankMap[id.id()] = mp; - return FPos(ss.val(), mp.data(), ss.max(), ss.max2()); - } - } - else { - return ss; - } - } }; template diff --git a/src/opt/mpi/tests/rindex_exp.cc b/src/opt/mpi/tests/rindex_exp.cc index a1a2a35..fc89eed 100644 --- a/src/opt/mpi/tests/rindex_exp.cc +++ b/src/opt/mpi/tests/rindex_exp.cc @@ -23,8 +23,8 @@ namespace Env() { CXZ_ASSERT(getNumRanks() == 4, "exptected 4 ranks"); - Vector xs(12); - Vector ts(16); + Vector xs(L); + Vector ts(T); for(SizeT i = 0; i != xs.size(); ++i){ const Int x = static_cast(i) - static_cast(xs.size()/2); xs[i] = x; @@ -45,6 +45,8 @@ namespace mRRange = rrange(mGRange, mGeom); } + SizeT T = 16; + SizeT L = 12; RangePtr mSRange; RangePtr mXRange; RangePtr mTRange; @@ -53,48 +55,6 @@ namespace RangePtr mRRange; }; - template - class IndexMap : public Index - { - public: - typedef typename Index::IB IB; - typedef typename Index::RangeType RangeType; - - DEFAULT_MEMBERS(IndexMap); - - IndexMap(const Sptr& i, const Vector& map, const Sptr& r) : - Index(*i), mI(i), mR(r), mMap(map) {} - - constexpr decltype(auto) id() const - { - return mI->id(); - } - - template - decltype(auto) stepSize(const IndexId& id) const - { - // TODO: new Pos Type!!! - return FPos( mI->stepSize(id).val(), mMap.data(), mI->lmax().val(), mR->lmax().val() ); - } - - private: - Sptr mI; - Sptr mR; - Vector mMap; - }; - - template - constexpr decltype(auto) shift(const Sptr& i, const Sptr& r) - { - // only one-dim indices!!! - auto j = *i; - Vector mp(j.lmax().val()); - const SizeT L = i->lmax().val()*r->lmax().val(); - for(j = 0; j.lex() != j.lmax().val(); ++j){ - mp[j.lex()] = ( j.lex() + 1 + L) % L; - } - return std::make_shared>(i, mp, r); - } template class PosOp : public COpInterface> @@ -105,17 +65,22 @@ namespace constexpr PosOp() = default; constexpr PosOp(const Sptr& i, SizeT block) : - mMyrank(getRankNumber()), mI(i), mBlock(block) {} + mMyrank(getRankNumber()), mI(i), mBlock(block) + { + //VCHECK(mI->id().id()); + } template constexpr decltype(auto) operator()(const PosT& pos) const { - return static_cast(pos)+mMyrank*mBlock; + return static_cast(pos); + //return static_cast(pos)+mMyrank*mBlock; } constexpr decltype(auto) operator()() const { - return static_cast(mMyrank*mBlock); + return static_cast(0); + //return static_cast(mMyrank*mBlock); } template @@ -135,6 +100,93 @@ namespace { return PosOp(i, block); } + + template + struct MkFPos + { + static constexpr decltype(auto) mk(const PosT& pos, const SizeT* map) + { + return FPos(pos.val(), map); + } + }; + + template + constexpr decltype(auto) mkMPos(const BPosT& bpos, const NPosT& npos) + { + return MPos(bpos, npos); + } + + template + struct MkFPos> + { + static constexpr decltype(auto) mk(const MPos& pos, const SizeT* map) + { + return mkMPos( MkFPos::mk( pos, map ), MkFPos::mk( pos.next(), map ) ); + } + }; + + template + constexpr decltype(auto) mkFPos(const PosT& pos, const SizeT* map) + { + return MkFPos::mk(pos, map); + } + + template + class MapXpr : public XprInterface> + { + private: + Sptr mTi; + Sptr mSi; + Vector mMap; + Xpr mXpr; + typedef decltype(mkFPos( mXpr.rootSteps(mTi->id()), mMap.data() )) Ext; + Ext mExt; + + public: + + MapXpr() = default; + + // src local + template + MapXpr(const Sptr& ti, const Sptr& si, const F& f, Xpr&& xpr) : + mTi(ti), mSi(si), mMap(mSi->local()->lmax().val()), mXpr(std::forward(xpr)), + mExt(mkFPos( mXpr.rootSteps(mTi->id()), mMap.data() )) + { + //VCHECK(mTi->id().id()); + auto six = *si; + auto sie = si->range()->end(); + auto tix = *ti; + for(six = 0; six != sie; ++six){ + tix.at( f(*six) ); + if(six.rank() == getRankNumber()){ + mMap[six.local()->lex()] = tix.pos(); + } + } + } + + template + decltype(auto) operator()(const PosT& last) const + { + return mXpr( last.next() + mExt( last ) ); + } + + decltype(auto) operator()() const + { + return mXpr( mExt( UPos(0) ) ); + } + + template + decltype(auto) rootSteps(const IndexId& id) const + { + return mSi->stepSize(id) << mXpr.rootSteps(id); + } + }; + + template + decltype(auto) mapXpr(const Sptr& ti, const Sptr& si, const F& f, Xpr&& xpr) + { + return MapXpr(ti,si,f,std::forward(xpr)); + } } void run2(const Env& env) @@ -145,6 +197,8 @@ void run2(const Env& env) typedef UIndex UI; typedef MIndex LocI; typedef MIndex RankI; + const SizeT T = env.T; + const SizeT L = env.L; auto rgi = std::make_shared>(env.mRRange); auto rgj = std::make_shared>(env.mRRange); auto rgk = std::make_shared>(env.mRRange); @@ -152,7 +206,7 @@ void run2(const Env& env) LocI gj(env.mGRange); auto ri = std::make_shared(env.mGeom); constexpr auto C0 = CSizeT<0> {}; - constexpr auto C1 = CSizeT<1> {}; + //constexpr auto C1 = CSizeT<1> {}; constexpr auto C2 = CSizeT<2> {}; constexpr auto C3 = CSizeT<3> {}; @@ -173,21 +227,27 @@ void run2(const Env& env) for(auto& sb: sendbuf){ sb.reserve(data.size()); } - - auto srgi = rindexPtr( mindexPtr( - shift(rgi->local()->pack()[C0], ri->pack()[C0]), - rgi->local()->pack()[C1], - shift(rgi->local()->pack()[C2], ri->pack()[C2]), - shift(rgi->local()->pack()[C3], ri->pack()[C3]) - ), ri ); - VCHECK(srgi->lmax().val()); - + *rgj = 0; while(rgj->rank() != 1){ ++*rgj; } *rgj->local() = 0; - rgi->ifor( operation + + auto shift = [&](const auto& x){ + auto o = x; + std::get<0>(o) += 1; + if(std::get<0>(o) >= static_cast(T)/2) { std::get<0>(o) -= T; } + std::get<2>(o) += 1; + if(std::get<2>(o) >= static_cast(L)/2) { std::get<2>(o) -= L; } + std::get<3>(o) += 1; + if(std::get<3>(o) >= static_cast(L)/2) { std::get<3>(o) -= L; } + return o; + }; + + SizeT cntx = 0; + rgi->ifor( mapXpr(rgj, rgi, shift, + operation ( [&](SizeT p) { gj = rgj->lex(); *gj.pack()[C0] = (gj.pack()[C0]->lex() + 1) % gj.pack()[C0]->lmax().val(); @@ -195,12 +255,15 @@ void run2(const Env& env) *gj.pack()[C3] = (gj.pack()[C3]->lex() + 1) % gj.pack()[C3]->lmax().val(); gj(); *rgk = gj.lex(); + if(myrank == 1){ + assert(p == rgk->pos()); std::cout << p << " " << rgk->pos() << " " << rgj->pos() << std::endl; } ++*rgj->local(); (*rgj)(); - } , posop(srgi, rgi->local()->pmax().val()) ) , + ++cntx; + } , posop(rgj, rgi->local()->pmax().val()) ) ) , NoF {} )(); MPI_Barrier(MPI_COMM_WORLD);