Skip to content

Commit 564defd

Browse files
committed
expressions: a general product computes its slabs where a requested map says
Expr::set_slab_pmap() takes a process map over the tile index tuples of a general product's fused (Hadamard) modes. Each slab is then computed by the one rank the map names, instead of being dealt round-robin to groups of ranks, so operands distributed by the same rule are used where they are and the result stays there. SlabbedPmap and Summa take the slab -> rank table; ranks with no slab idle. TILEDARRAY_HAS_EXPR_SLAB_PMAP announces the interface. Trial implementation for measuring inter-rank traffic of pair-keyed intermediates; balancing the work over the ranks is left to the map.
1 parent eae2ad7 commit 564defd

6 files changed

Lines changed: 484 additions & 29 deletions

File tree

‎src/TiledArray/dist_eval/contraction_eval.h‎

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
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

‎src/TiledArray/expressions/cont_engine.h‎

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ class ContEngine : public BinaryEngine<Derived> {
246246
size_type proc_h_ = 1; ///< process-grid extent along the slab (h)
247247
///< axis of the 3-d grid (# of h-planes)
248248
size_type proc_h_stride_ = 0; ///< ranks per h-plane (0 = ungrouped 2-d)
249+
std::shared_ptr<const std::vector<std::size_t>>
250+
slab_group_; ///< the h-plane of every slab, if the caller chose where
251+
///< slabs are computed (Expr::set_slab_pmap); null = slab
252+
///< h belongs to plane h % proc_h_
249253
std::shared_ptr<const pmap_interface>
250254
canonical_pmap_; ///< the slab-replicated map the batched Summa
251255
///< computes the canonical-layout result on; built
@@ -987,6 +991,55 @@ class ContEngine : public BinaryEngine<Derived> {
987991
op_.gemm_helper(), n_fused_modes_);
988992
}
989993

994+
/// The h-plane of every slab of a general product, from the caller's slab
995+
/// map (see Expr::set_slab_pmap)
996+
997+
/// \param slab_pmap a process map over the tile index tuples of the fused
998+
/// modes, in the order in which they appear in the target layout
999+
/// \param fused_extent the tile extents of the fused modes, in the
1000+
/// canonical layout (where they lead)
1001+
/// \param nh the number of fused modes
1002+
/// \return for every slab (in the canonical order) the plane that contains
1003+
/// the rank \p slab_pmap gives for it
1004+
/// \pre proc_h_ and proc_h_stride_ are set, and proc_h_ > 1
1005+
template <typename Extent>
1006+
std::shared_ptr<const std::vector<std::size_t>> make_slab_groups(
1007+
const pmap_interface& slab_pmap, const Extent* fused_extent,
1008+
const unsigned int nh) const {
1009+
// fused mode i of the canonical layout is mode outer(perm_)[i] of the
1010+
// target layout; order the fused modes as the target layout does
1011+
const auto& outer_perm = outer(perm_);
1012+
container::svector<unsigned int> by_target(nh);
1013+
for (unsigned int i = 0u; i != nh; ++i) by_target[i] = i;
1014+
if (outer_perm && !outer_perm.is_identity())
1015+
std::sort(by_target.begin(), by_target.end(),
1016+
[&](unsigned int a, unsigned int b) {
1017+
return outer_perm[a] < outer_perm[b];
1018+
});
1019+
// weight of canonical fused mode i in the slab map's ordinal
1020+
container::svector<std::size_t> weight(nh);
1021+
std::size_t w = 1ul;
1022+
for (unsigned int j = nh; j-- != 0u;) {
1023+
weight[by_target[j]] = w;
1024+
w *= fused_extent[by_target[j]];
1025+
}
1026+
const std::size_t n_slabs = w;
1027+
auto groups = std::make_shared<std::vector<std::size_t>>(n_slabs);
1028+
container::svector<std::size_t> idx(nh, 0ul);
1029+
for (std::size_t h = 0ul; h != n_slabs; ++h) {
1030+
std::size_t ord = 0ul;
1031+
for (unsigned int i = 0u; i != nh; ++i) ord += idx[i] * weight[i];
1032+
(*groups)[h] = std::min<std::size_t>(
1033+
slab_pmap.owner(ord) / proc_h_stride_, proc_h_ - 1ul);
1034+
// next canonical (row-major) fused tile index tuple
1035+
for (unsigned int i = nh; i-- != 0u;) {
1036+
if (++idx[i] < std::size_t(fused_extent[i])) break;
1037+
idx[i] = 0ul;
1038+
}
1039+
}
1040+
return groups;
1041+
}
1042+
9901043
/// Initialize the result distribution of a general product
9911044

9921045
/// The 2-d process grid spans the external (free) modes only; the fused
@@ -1061,10 +1114,25 @@ class ContEngine : public BinaryEngine<Derived> {
10611114
proc_h_ = std::min<size_type>(n_slabs_,
10621115
std::max<size_type>(1ul, P / p2d_cap));
10631116
}
1117+
// the caller's choice of where the slabs are computed, if any
1118+
const auto slab_pmap = (ExprEngine_::override_ptr_ && P > 1ul)
1119+
? ExprEngine_::override_ptr_->slab_pmap
1120+
: std::shared_ptr<const pmap_interface>{};
1121+
const bool use_slab_pmap =
1122+
slab_pmap && slab_pmap->size() == n_slabs_ && slab_pmap->procs() == P;
1123+
// a requested slab map names the rank that computes each slab: give
1124+
// every rank its own plane, so that the map is honored exactly (planes
1125+
// with no slab idle) and a slab whose operands are already on that rank
1126+
// moves no data, however many result tiles it has. Balancing the work
1127+
// over the ranks is then up to the map.
1128+
if (use_slab_pmap) proc_h_ = P;
10641129
// keep the invariant proc_h_ == 1 => proc_h_stride_ == 0 (the ungrouped
10651130
// 2-d case) so downstream logic can key off either field; for grouped
10661131
// grids it is the per-plane world-rank count P / proc_h_.
10671132
proc_h_stride_ = (proc_h_ == 1ul) ? 0ul : P / proc_h_;
1133+
slab_group_.reset();
1134+
if (use_slab_pmap && proc_h_ > 1ul)
1135+
slab_group_ = make_slab_groups(*slab_pmap, left_tiles_size, nh);
10681136

10691137
if (proc_h_ == 1ul) {
10701138
// Construct the per-slab process grid over the whole world.
@@ -1100,14 +1168,15 @@ class ContEngine : public BinaryEngine<Derived> {
11001168
left_.init_distribution(
11011169
world, std::make_shared<TiledArray::detail::SlabbedPmap>(
11021170
*world, proc_grid_.make_row_phase_pmap(K_), n_slabs_,
1103-
proc_h_, proc_h_stride_));
1171+
proc_h_, proc_h_stride_, slab_group_));
11041172
right_.init_distribution(
11051173
world, std::make_shared<TiledArray::detail::SlabbedPmap>(
11061174
*world, proc_grid_.make_col_phase_pmap(K_), n_slabs_,
1107-
proc_h_, proc_h_stride_));
1175+
proc_h_, proc_h_stride_, slab_group_));
11081176

11091177
canonical_pmap_ = std::make_shared<TiledArray::detail::SlabbedPmap>(
1110-
*world, proc_grid_.make_pmap(), n_slabs_, proc_h_, proc_h_stride_);
1178+
*world, proc_grid_.make_pmap(), n_slabs_, proc_h_, proc_h_stride_,
1179+
slab_group_);
11111180
}
11121181

11131182
// Initialize the process map if not already defined
@@ -1174,7 +1243,7 @@ class ContEngine : public BinaryEngine<Derived> {
11741243
std::shared_ptr<impl_type> pimpl = std::make_shared<impl_type>(
11751244
left, right, *world_, trange_, shape_, pmap_, perm_,
11761245
batched_op_type(op_, n_fused_modes_), K_, proc_grid_, n_slabs_,
1177-
proc_h_, proc_h_stride_);
1246+
proc_h_, proc_h_stride_, slab_group_);
11781247
return dist_eval_type(pimpl);
11791248
}
11801249

@@ -1202,7 +1271,7 @@ class ContEngine : public BinaryEngine<Derived> {
12021271
left, right, *world_, canonical_trange, canonical_shape,
12031272
canonical_pmap_, BipartitePermutation{},
12041273
batched_op_type(op_, n_fused_modes_), K_, proc_grid_, n_slabs_, proc_h_,
1205-
proc_h_stride_);
1274+
proc_h_stride_, slab_group_);
12061275
dist_eval_type canonical(pimpl);
12071276

12081277
typedef TiledArray::detail::UnaryEvalImpl<

‎src/TiledArray/expressions/expr.h‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
#include <range/v3/range/conversion.hpp>
5252
#include <range/v3/view/zip_with.hpp>
5353

54+
/// Defined if Expr::set_slab_pmap() is available
55+
#define TILEDARRAY_HAS_EXPR_SLAB_PMAP 1
56+
5457
namespace TiledArray::expressions {
5558

5659
template <typename Engine>
@@ -67,6 +70,8 @@ struct EngineParamOverride {
6770
World* world;
6871
std::shared_ptr<const pmap_interface> pmap;
6972
const shape_type* shape;
73+
std::shared_ptr<const pmap_interface>
74+
slab_pmap; ///< see Expr::set_slab_pmap()
7075
};
7176

7277
/// \brief type trait checks if T has array() member
@@ -146,6 +151,29 @@ class Expr {
146151
}
147152
return derived();
148153
}
154+
/// Chooses where the slabs of a general product are computed
155+
156+
/// A product with fused (Hadamard) outer indices, e.g.
157+
/// `c("i,j,a,b") = a("i,j,a,k") * b("i,j,k,b")`, is a batch of
158+
/// independent contractions, one per slab (one per tile index tuple of the
159+
/// fused modes). With no slab map each slab is computed by a group of
160+
/// ranks on a 2-d process grid, and slabs are dealt to the groups
161+
/// round-robin. A slab map names the one rank that computes each slab, so
162+
/// that operands distributed by the same rule are used where they are, and
163+
/// the result (unless a result map is requested with set_pmap()) stays
164+
/// where it was computed. The map then also decides how the work is
165+
/// balanced over the ranks.
166+
/// \param slab_pmap a process map over the tile index tuples of the fused
167+
/// modes, taken in the order in which they appear in the result of
168+
/// this expression (row-major). Ignored by expressions that are not
169+
/// general products, and if its size or process count do not match.
170+
Expr<Derived>& set_slab_pmap(
171+
const std::shared_ptr<const typename override_type::pmap_interface>
172+
slab_pmap) {
173+
if (!override_ptr_) override_ptr_ = std::make_shared<override_type>();
174+
override_ptr_->slab_pmap = slab_pmap;
175+
return derived();
176+
}
149177

150178
private:
151179
/// Task function used to evaluate a lazy tile and apply an op

0 commit comments

Comments
 (0)