2020#ifndef TILEDARRAY_DIST_EVAL_CONTRACTION_EVAL_H__INCLUDED
2121#define TILEDARRAY_DIST_EVAL_CONTRACTION_EVAL_H__INCLUDED
2222
23+ #include < limits>
24+ #include < memory>
2325#include < vector>
2426
2527#include < TiledArray/config.h>
@@ -135,13 +137,42 @@ class Summa
135137 // ordinary shared-grid batched contraction.
136138 const ordinal_type proc_h_; // /< Number of slab (h) groups
137139 const ordinal_type
138- proc_h_stride_; // /< World ranks per slab group (the
139- // /< group of slab h spans world ranks
140- // /< [(h % proc_h_) * proc_h_stride_, ...))
141- const ordinal_type first_slab_; // /< This rank's group's first slab (== its
142- // /< group index), or nh_ if this rank is
143- // /< in no group (idle for this eval)
140+ proc_h_stride_; // /< World ranks per slab group (the
141+ // /< group of slab h spans world ranks
142+ // /< [(h % proc_h_) * proc_h_stride_, ...))
143+ const std::shared_ptr<const std::vector<std::size_t >>
144+ slab_group_; // /< The group of every slab, in place of h % proc_h_
145+ // /< (null = h % proc_h_)
146+ static constexpr ordinal_type no_group =
147+ std::numeric_limits<ordinal_type>::max(); // /< "this rank is in no group"
148+ const ordinal_type first_slab_; // /< This rank's group index (with no
149+ // /< slab_group_ table also its group's
150+ // /< first slab), or no_group if this rank
151+ // /< is in no group (idle for this eval)
144152 const ordinal_type my_slabs_; // /< Number of slabs of this rank's group
153+ std::vector<ordinal_type>
154+ slab_ord_; // /< With a slab_group_ table: this rank's group-local
155+ // /< ordinal of each of its group's slabs
156+
157+ // / \return the group of slab \p h
158+ ordinal_type group_of (const ordinal_type h) const {
159+ if (proc_h_ == 1ul ) return 0ul ;
160+ return slab_group_ ? ordinal_type ((*slab_group_)[h]) : h % proc_h_;
161+ }
162+
163+ // / \return the smallest slab >= \p h of this rank's group, or nh_ if there
164+ // / is none
165+ ordinal_type next_slab (ordinal_type h) const {
166+ if (first_slab_ == no_group) return nh_; // not in any group
167+ if (!slab_group_ || proc_h_ == 1ul ) {
168+ if (proc_h_ == 1ul ) return std::min (h, nh_);
169+ if (h <= first_slab_) return std::min (first_slab_, nh_);
170+ const ordinal_type r = (h - first_slab_) % proc_h_;
171+ return std::min (r == 0ul ? h : h + (proc_h_ - r), nh_);
172+ }
173+ while (h < nh_ && (*slab_group_)[h] != first_slab_) ++h;
174+ return std::min (h, nh_);
175+ }
145176
146177 // / \return the world rank that owns result tile \p i: the within-group
147178 // / owner (from the group-local process grid) shifted by the world-rank
@@ -158,7 +189,7 @@ class Summa
158189 const ProcessID within_group = proc_row * proc_grid_.proc_cols () + proc_col;
159190 // shift by the offset of the group that owns this tile's slab
160191 const ordinal_type slab = source_index / result_slab_size_;
161- const ordinal_type group = (proc_h_ > 1ul ) ? ( slab % proc_h_) : 0ul ;
192+ const ordinal_type group = group_of ( slab) ;
162193 return ProcessID (group * proc_h_stride_) + within_group;
163194 }
164195
@@ -171,15 +202,16 @@ class Summa
171202 // / rank's group's slabs, or nsteps_ if there is none
172203 ordinal_type next_step (ordinal_type s) const {
173204 if (proc_h_ == 1ul ) return std::min (s, nsteps_);
174- if (first_slab_ >= nh_ ) return nsteps_; // not in any group
175- while (s < nsteps_ && (step_h (s) % proc_h_ ) != first_slab_)
205+ if (first_slab_ == no_group ) return nsteps_; // not in any group
206+ while (s < nsteps_ && group_of (step_h (s)) != first_slab_)
176207 s = (step_h (s) + 1ul ) * k_; // jump to the start of the next slab
177208 return std::min (s, nsteps_);
178209 }
179210
180211 // / \return this rank's group-local ordinal of slab \p h (which must
181212 // / belong to this rank's group)
182213 ordinal_type slab_ord (const ordinal_type h) const {
214+ if (slab_group_ && proc_h_ != 1ul ) return slab_ord_[h];
183215 return (h - first_slab_) / proc_h_;
184216 }
185217
@@ -1044,7 +1076,8 @@ class Summa
10441076 // two static keys are offset PAST that range and made group-unique so
10451077 // that two different groups' single-grid static groups never claim the
10461078 // same DistributedID with inconsistent membership)
1047- const std::size_t static_key_base = 2ul * nsteps_ + 2ul * first_slab_;
1079+ const std::size_t static_key_base =
1080+ 2ul * nsteps_ + 2ul * (first_slab_ == no_group ? nh_ : first_slab_);
10481081 const madness::DistributedID col_did (DistEvalImpl_::id (),
10491082 static_key_base);
10501083 col_group_ = proc_grid_.make_col_group (col_did);
@@ -1116,7 +1149,7 @@ class Summa
11161149 // does not depend on h)
11171150 ordinal_type tile_count = 0ul ;
11181151 ReducePairTask<op_type>* MADNESS_RESTRICT reduce_task = reduce_tasks_;
1119- for (ordinal_type h = first_slab_ ; h < nh_; h += proc_h_ ) {
1152+ for (ordinal_type h = next_slab ( 0ul ) ; h < nh_; h = next_slab (h + 1ul ) ) {
11201153 const ordinal_type slab_base = h * result_slab_size_;
11211154 ordinal_type row_start =
11221155 slab_base + proc_grid_.rank_row () * proc_grid_.cols ();
@@ -1186,7 +1219,7 @@ class Summa
11861219
11871220 // Iterate over all local tiles, slab by slab
11881221 ReducePairTask<op_type>* reduce_task = reduce_tasks_;
1189- for (ordinal_type h = first_slab_ ; h < nh_; h += proc_h_ ) {
1222+ for (ordinal_type h = next_slab ( 0ul ) ; h < nh_; h = next_slab (h + 1ul ) ) {
11901223 const ordinal_type slab_base = h * result_slab_size_;
11911224 ordinal_type row_start =
11921225 slab_base + proc_grid_.rank_row () * proc_grid_.cols ();
@@ -1228,7 +1261,7 @@ class Summa
12281261
12291262 // Iterate over all local tiles, slab by slab
12301263 ReducePairTask<op_type>* reduce_task = reduce_tasks_;
1231- for (ordinal_type h = first_slab_ ; h < nh_; h += proc_h_ ) {
1264+ for (ordinal_type h = next_slab ( 0ul ) ; h < nh_; h = next_slab (h + 1ul ) ) {
12321265 const ordinal_type slab_base = h * result_slab_size_;
12331266 ordinal_type row_start =
12341267 slab_base + proc_grid_.rank_row () * proc_grid_.cols ();
@@ -1762,7 +1795,8 @@ class Summa
17621795 const std::shared_ptr<const pmap_interface>& pmap, const Perm& perm,
17631796 const op_type& op, const ordinal_type k, const ProcGrid& proc_grid,
17641797 const ordinal_type nh = 1ul , const ordinal_type proc_h = 1ul ,
1765- const ordinal_type proc_h_stride = 0ul )
1798+ const ordinal_type proc_h_stride = 0ul ,
1799+ std::shared_ptr<const std::vector<std::size_t >> slab_group = {})
17661800 : DistEvalImpl_(world, trange, shape, pmap, outer(perm)),
17671801 left_(left),
17681802 right_(right),
@@ -1778,9 +1812,9 @@ class Summa
17781812 result_slab_size_(proc_grid.rows() * proc_grid.cols()),
17791813 proc_h_(proc_h),
17801814 proc_h_stride_(proc_h_stride),
1815+ slab_group_(proc_h == 1ul ? nullptr : std::move(slab_group)),
17811816 first_slab_(compute_first_slab(world, nh, proc_h, proc_h_stride)),
1782- my_slabs_(first_slab_ < nh ? (nh - first_slab_ + proc_h - 1ul ) / proc_h
1783- : 0ul),
1817+ my_slabs_(count_my_slabs(nh, proc_h, first_slab_, slab_group_.get())),
17841818 reduce_tasks_(NULL ),
17851819 left_start_local_(proc_grid_.rank_row() * k),
17861820 left_end_(left.size() / nh),
@@ -1801,7 +1835,33 @@ class Summa
18011835 TA_ASSERT (right.size () % nh_ == 0 );
18021836 TA_ASSERT (proc_h_ > 0 );
18031837 TA_ASSERT (proc_h_ == 1ul || proc_h_stride > 0ul );
1804- TA_ASSERT (proc_h_ <= nh_);
1838+ TA_ASSERT (slab_group_ || proc_h_ <= nh_);
1839+ TA_ASSERT (!slab_group_ || slab_group_->size () == nh_);
1840+ if (slab_group_) {
1841+ // this rank's group-local ordinals of its group's slabs
1842+ slab_ord_.assign (nh_, nh_);
1843+ ordinal_type ord = 0ul ;
1844+ for (ordinal_type h = 0ul ; h < nh_; ++h) {
1845+ TA_ASSERT ((*slab_group_)[h] < proc_h_);
1846+ if ((*slab_group_)[h] == first_slab_) slab_ord_[h] = ord++;
1847+ }
1848+ }
1849+ }
1850+
1851+ // / \return the number of slabs of the group \p my_group (the value of
1852+ // / compute_first_slab), given the slab-to-group table \p slab_group or,
1853+ // / if null, slab h -> group h % proc_h
1854+ static ordinal_type count_my_slabs (
1855+ const ordinal_type nh, const ordinal_type proc_h,
1856+ const ordinal_type my_group, const std::vector<std::size_t >* slab_group) {
1857+ if (proc_h == 1ul ) return nh;
1858+ if (!slab_group)
1859+ return my_group < nh ? (nh - my_group + proc_h - 1ul ) / proc_h : 0ul ;
1860+ if (my_group == no_group) return 0ul ;
1861+ ordinal_type n = 0ul ;
1862+ for (const auto g : *slab_group)
1863+ if (g == my_group) ++n;
1864+ return n;
18051865 }
18061866
18071867 // / \return this rank's group's first slab (== its group index), or
@@ -1811,7 +1871,7 @@ class Summa
18111871 const ordinal_type proc_h_stride) {
18121872 if (proc_h == 1ul ) return 0ul ;
18131873 const auto rank = ordinal_type (world.rank ());
1814- return (rank < proc_h * proc_h_stride) ? (rank / proc_h_stride) : nh ;
1874+ return (rank < proc_h * proc_h_stride) ? (rank / proc_h_stride) : no_group ;
18151875 }
18161876
18171877 virtual ~Summa () {}
@@ -1919,7 +1979,9 @@ class Summa
19191979#endif // TILEDARRAY_ENABLE_SUMMA_TRACE_EVAL
19201980
19211981 ordinal_type tile_count = 0ul ;
1922- if (proc_grid_.local_size () > 0ul ) {
1982+ // a rank outside the process grid, or whose slab group was given no slab
1983+ // (possible with a caller's slab-to-group table), has nothing to compute
1984+ if (proc_grid_.local_size () > 0ul && my_slabs_ > 0ul ) {
19231985 tile_count = initialize ();
19241986
19251987 // depth controls the number of simultaneous SUMMA iterations
0 commit comments