Skip to content

Commit b13ff28

Browse files
committed
feat: size_of over all live DistArrays of a type in a World
Adds a ground-truth memory-accounting facility that finds every live DistArray of a given type by walking a World's WorldObject registry, rather than summing size_of over a set of handles. Because each array's tile storage is a single DistributedStorage WorldObject, an array referenced by N shallow-copy handles is counted exactly once -- handle summation double-counts shared storage, which makes it unsuitable as a cross-check. API (in TiledArray namespace, dist_array.h): - size_of<DistArrayT, S>(World&) Walks world.get_object_ids(), recovers each registered pointer as the common polymorphic base madness::WorldObjectBase, and dynamic_casts to the DistributedStorage matching DistArrayT's tile type; non-matching objects are skipped. Sums DistributedStorage::local_size_in_bytes<S>(). - size_of_live_distarrays<S, DistArrayTs...>(worlds) Returns a [world][type] matrix of the above. DistributedStorage gains local_size_in_bytes<S>(): sums TiledArray::size_of<S>(tile) over locally-owned, set futures -- the same tile set size_of(DistArray) iterates. Type-safety rests on WorldObjectBase sitting at offset 0 of every registered WorldObject; verified that across MADNESS, TiledArray, and MPQC no WorldObject-derived class has WorldObject as a non-primary base (the single-inheritance "class X : public WorldObject<X>" idiom). The recovered base is dynamic_cast, so a wrong type yields nullptr, not UB. Counts only locally-owned set tiles; excludes TiledRange/shape metadata and remote-tile caches. Call at a quiescent point (after a fence). Test (array_suite/size_of_live_in_world): builds two distinct arrays plus a shallow copy of one, checks the World walk equals the two-array (deduplicated) tile total -- not the three-handle sum -- that a ToT-typed walk does not pick up regular-tensor arrays, and the variadic matrix form. Passes at np=1; np=2 left to CI (the local macOS ASan+mpirun environment crashes all tests, touched or not).
1 parent 600c4ad commit b13ff28

3 files changed

Lines changed: 137 additions & 0 deletions

File tree

src/TiledArray/dist_array.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,64 @@ std::size_t size_of(const DistArray<Tile, Policy>& da) {
20152015
return result;
20162016
}
20172017

2018+
/// \return the per-rank bytes (in memory space `S`) held by *all* live
2019+
/// `DistArray<Tile,Policy>` of the requested type currently registered in
2020+
/// \p world, discovered by walking the World's `WorldObject` registry.
2021+
///
2022+
/// Each array's tile storage is a single `detail::DistributedStorage`
2023+
/// `WorldObject`, so an array referenced by N shallow-copy handles is counted
2024+
/// exactly once — unlike summing `size_of` over a set of handles, which
2025+
/// double-counts shared storage. This makes the result suitable as ground
2026+
/// truth for validating handle-based accounting.
2027+
///
2028+
/// Discovery is type-safe: each registered pointer is recovered as the common
2029+
/// polymorphic base `madness::WorldObjectBase` and `dynamic_cast` to the
2030+
/// `DistributedStorage` matching `DistArrayT`'s tile type; non-matching
2031+
/// objects (other tile types, MADNESS containers) are skipped. Assumes the
2032+
/// registered `WorldObject`s place `WorldObjectBase` at offset 0 (true for
2033+
/// the single-inheritance `class X : public WorldObject<X>` idiom TA uses).
2034+
///
2035+
/// \note Counts only locally-owned tiles whose futures are set — the same set
2036+
/// `size_of(DistArray)` iterates. Excludes TiledRange/shape metadata and
2037+
/// remote-tile caches. Call at a quiescent point (after a fence).
2038+
/// \tparam DistArrayT the `DistArray` specialization to look for
2039+
/// \tparam S the memory space to report (default `Host`)
2040+
template <typename DistArrayT, MemorySpace S = MemorySpace::Host>
2041+
std::size_t size_of(World& world) {
2042+
using tile_type = typename DistArrayT::value_type;
2043+
using storage_type = detail::DistributedStorage<tile_type>;
2044+
std::size_t result = 0;
2045+
for (const auto& id : world.get_object_ids()) {
2046+
auto base_opt = world.template ptr_from_id<madness::WorldObjectBase>(id);
2047+
if (!base_opt || !*base_opt) continue;
2048+
if (auto* storage = dynamic_cast<storage_type*>(*base_opt)) {
2049+
result += storage->template local_size_in_bytes<S>();
2050+
}
2051+
}
2052+
return result;
2053+
}
2054+
2055+
/// \return a matrix of per-rank live-`DistArray` byte totals indexed
2056+
/// `[world_index][type_index]`: for each `World` in \p worlds (rows) and each
2057+
/// `DistArray` type in the pack `DistArrayTs` (columns), the value of
2058+
/// `size_of<DistArrayT, S>(world)`. Lets a caller inventory which array types
2059+
/// hold how much in which world at a checkpoint, deduplicated across
2060+
/// shallow-copy handles.
2061+
///
2062+
/// \note `S` is the leading template argument (it has a default but precedes
2063+
/// the type pack), so callers must spell it out:
2064+
/// `size_of_live_distarrays<MemorySpace::Host, ArrayA, ArrayB>(worlds)`.
2065+
template <MemorySpace S = MemorySpace::Host, typename... DistArrayTs>
2066+
std::vector<std::array<std::size_t, sizeof...(DistArrayTs)>>
2067+
size_of_live_distarrays(const std::vector<World*>& worlds) {
2068+
std::vector<std::array<std::size_t, sizeof...(DistArrayTs)>> result;
2069+
result.reserve(worlds.size());
2070+
for (World* w : worlds) {
2071+
result.push_back({size_of<DistArrayTs, S>(*w)...});
2072+
}
2073+
return result;
2074+
}
2075+
20182076
#ifndef TILEDARRAY_HEADER_ONLY
20192077

20202078
extern template class DistArray<Tensor<double>, DensePolicy>;

src/TiledArray/distributed_storage.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#ifndef TILEDARRAY_DISTRIBUTED_STORAGE_H__INCLUDED
2121
#define TILEDARRAY_DISTRIBUTED_STORAGE_H__INCLUDED
2222

23+
#include <TiledArray/platform.h>
2324
#include <TiledArray/pmap/pmap.h>
2425

2526
namespace TiledArray {
@@ -360,6 +361,25 @@ class DistributedStorage : public madness::WorldObject<DistributedStorage<T>> {
360361
/// \throw nothing
361362
size_type size() const { return data_.size(); }
362363

364+
/// Number of bytes occupied by the locally-owned tiles in memory space \p S.
365+
366+
/// Sums `TiledArray::size_of<S>(tile)` over every locally-stored element
367+
/// whose future is already set; pending (unset) and remote-cached elements
368+
/// are skipped. No communication; intended to be called at a quiescent
369+
/// point (e.g. after a fence). This is the per-rank local footprint, the
370+
/// same set of tiles that `size_of(DistArray)` iterates.
371+
/// \tparam S the memory space to report
372+
/// \return local tile bytes in space \p S
373+
template <MemorySpace S>
374+
std::size_t local_size_in_bytes() const {
375+
std::size_t result = 0;
376+
for (auto it = data_.begin(); it != data_.end(); ++it) {
377+
const future& f = it->second;
378+
if (f.probe()) result += TiledArray::size_of<S>(f.get());
379+
}
380+
return result;
381+
}
382+
363383
/// Max size accessor
364384

365385
/// The maximum size is the total number of elements that can be held by

tests/dist_array.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,65 @@ BOOST_AUTO_TEST_CASE(size_of) {
10201020
BOOST_REQUIRE(sz0 == sz0_expected);
10211021
}
10221022

1023+
BOOST_AUTO_TEST_CASE(size_of_live_in_world) {
1024+
using T = Tensor<double>;
1025+
using ToT = Tensor<T>;
1026+
using Policy = SparsePolicy;
1027+
using ArrayT = DistArray<T, Policy>;
1028+
using ArrayToT = DistArray<ToT, Policy>;
1029+
1030+
auto& world = get_default_world();
1031+
world.gop.fence();
1032+
1033+
// arrays from earlier test cases may still be registered (destruction is
1034+
// deferred to the next fence), so measure a baseline and compare deltas
1035+
auto const base_T = TiledArray::size_of<ArrayT>(world);
1036+
auto const base_ToT = TiledArray::size_of<ArrayToT>(world);
1037+
1038+
TiledRange const trange({{0, 2, 5, 7}, {0, 5, 7, 10, 12}});
1039+
1040+
// two distinct regular arrays
1041+
auto a1 = make_array<ArrayT>(world, trange, [](T& tile, Range const& rng) {
1042+
tile = T(rng, 1.0);
1043+
return tile.norm();
1044+
});
1045+
auto a2 = make_array<ArrayT>(world, trange, [](T& tile, Range const& rng) {
1046+
tile = T(rng, 2.0);
1047+
return tile.norm();
1048+
});
1049+
// shallow copy: shares a1's storage WorldObject, must NOT be double-counted
1050+
ArrayT a1_copy = a1;
1051+
BOOST_REQUIRE(a1_copy.trange() == a1.trange()); // keep a1_copy alive & used
1052+
1053+
world.gop.fence();
1054+
1055+
// per-array local-tile bytes = size_of(array) - size_of(shape)
1056+
auto tiles_only = [](ArrayT const& a) {
1057+
return TiledArray::size_of<MemorySpace::Host>(a) -
1058+
TiledArray::size_of<MemorySpace::Host>(a.shape());
1059+
};
1060+
1061+
// the World walk counts each distinct DistributedStorage once: a1 + a2,
1062+
// NOT a1 + a2 + a1_copy
1063+
auto const expected_T = tiles_only(a1) + tiles_only(a2);
1064+
auto const got_T = TiledArray::size_of<ArrayT>(world) - base_T;
1065+
BOOST_CHECK_EQUAL(got_T, expected_T);
1066+
1067+
// the ToT-typed walk must not pick up the regular (T) arrays
1068+
auto const got_ToT_delta = TiledArray::size_of<ArrayToT>(world) - base_ToT;
1069+
BOOST_CHECK_EQUAL(got_ToT_delta, 0u);
1070+
1071+
// variadic matrix: one world (one row), two types (two columns)
1072+
auto const mat =
1073+
TiledArray::size_of_live_distarrays<MemorySpace::Host, ArrayT, ArrayToT>(
1074+
std::vector<World*>{&world});
1075+
BOOST_REQUIRE_EQUAL(mat.size(), 1u);
1076+
BOOST_CHECK_EQUAL(mat[0][0], TiledArray::size_of<ArrayT>(world));
1077+
BOOST_CHECK_EQUAL(mat[0][1], TiledArray::size_of<ArrayToT>(world));
1078+
1079+
world.gop.fence();
1080+
}
1081+
10231082
BOOST_FIXTURE_TEST_CASE(fill_zero_sparse, ArrayFixture) {
10241083
// construct a sparse array with some non-zero tiles and fill it
10251084
SpArrayN as(world, tr, TiledArray::SparseShape<float>(shape_tensor, tr));

0 commit comments

Comments
 (0)