Skip to content

Commit

Permalink
Unit tests for unary reductions involving ToT.
Browse files Browse the repository at this point in the history
  • Loading branch information
bimalgaudel committed Jun 27, 2024
1 parent fc89883 commit 512464e
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions tests/dist_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,4 +883,65 @@ BOOST_AUTO_TEST_CASE(volume) {
BOOST_REQUIRE(vol == TA::volume(array));
}

BOOST_AUTO_TEST_CASE(unary_reduction_tot) {
using Numeric = double;
using T = Tensor<Numeric>;
using ToT = Tensor<T>;
using Policy = SparsePolicy;
using ArrayToT = DistArray<ToT, Policy>;

auto unit_T = [](Range const& rng) { return T(rng, Numeric{1}); };

auto unit_ToT = [unit_T](Range const& rngo, Range const& rngi) {
return ToT(rngo, unit_T(rngi));
};

size_t constexpr nrows = 3;
size_t constexpr ncols = 4;
TiledRange const trange({{0, 2, 5, 7}, {0, 5, 7, 10, 12}});
TA_ASSERT(trange.tiles_range().extent().at(0) == nrows &&
trange.tiles_range().extent().at(1) == ncols,
"Following code depends on this condition.");

// this Range is used to construct all inner tensors of the tile with
// tile index @c tix.
auto inner_dims = [nrows, ncols](Range::index_type const& tix) -> Range {
static std::array<size_t, nrows> const rows{7, 8, 9};
static std::array<size_t, ncols> const cols{7, 8, 9, 10};

TA_ASSERT(tix.size() == 2, "Only rank-2 tensor expected.");
return Range({rows[tix.at(0) % nrows], cols[tix.at(1) % ncols]});
};

// let's make all 'diagonal' tiles zero
auto zero_tile = [](Range::index_type const& tix) -> bool {
return tix.at(0) == tix.at(1);
};

auto make_tile = [inner_dims, //
zero_tile, //
&trange, //
unit_ToT](auto& tile, auto const& rng) {
auto&& tix = trange.element_to_tile(rng.lobound());
if (zero_tile(tix))
return 0.;
else {
tile = unit_ToT(rng, inner_dims(tix));
return tile.norm();
}
};

auto& world = get_default_world();

// all non-zero inner tensors of this ToT array are unit (ie all
// inner tensors' elements are 1.)
auto array = make_array<ArrayToT>(world, trange, make_tile);

// since all inner tensors are filled with 1.
double array_norm = std::sqrt(TA::volume(array));

BOOST_REQUIRE(array_norm == TA::norm2(array));
BOOST_REQUIRE(array_norm = std::sqrt(TA::dot(array, array)));
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 512464e

Please sign in to comment.