Skip to content

Commit 2fa5125

Browse files
authored
Merge pull request #535 from ValeevGroup/ajay/fix/blk-expr-empty-tile-case
Handle empty blocks in block expressions
2 parents 386b568 + 6126f22 commit 2fa5125

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

src/TiledArray/expressions/blk_tsr_expr.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#include <TiledArray/expressions/unary_expr.h>
3333
#include "blk_tsr_engine.h"
3434

35+
#include <range/v3/algorithm.hpp>
36+
#include <range/v3/view.hpp>
37+
3538
#include <optional>
3639

3740
namespace TiledArray {
@@ -313,8 +316,18 @@ class BlkTsrExprBase : public Expr<Derived> {
313316

314317
/// Sets result trange lobound such that the tile lobounds are not changed
315318
Derived& preserve_lobound() {
316-
return set_trange_lobound(
317-
array_.trange().make_tile_range(lower_bound()).lobound());
319+
// only set lobound if *all* dimensions have non-zero extents
320+
const bool empty = ranges::any_of(
321+
ranges::views::zip(lower_bound_, upper_bound_), [](auto&& p) {
322+
auto [lb, ub] = p;
323+
return lb >= ub;
324+
});
325+
326+
if (!empty) {
327+
return set_trange_lobound(
328+
array_.trange().make_tile_range(lower_bound()).lobound());
329+
}
330+
return static_cast<Derived&>(*this);
318331
}
319332

320333
/// @return optional to result trange lobound; if null, the result trange

src/TiledArray/expressions/expr.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,10 @@ class Expr {
518518
// Move the data from dist_eval into the sub-block of result array.
519519
// This step may involve communication when the tiles are moved from the
520520
// sub-block distribution to the array distribution.
521-
// N.B. handle the corner case of zero-volume host array, then no data needs
522-
// to be moved
523-
if (tsr.array().trange().tiles_range().volume() != 0) {
521+
// N.B. handle the corner cases of zero-volume host array and zero-volume
522+
// block, then no data needs to be moved
523+
if (tsr.array().trange().tiles_range().volume() != 0 &&
524+
blk_range.volume() != 0) {
524525
// N.B. must deep copy
525526
TA_ASSERT(tsr.array().trange().tiles_range().includes(tsr.lower_bound()));
526527
// N.B. this expression's range,

tests/expressions_impl.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3075,6 +3075,53 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(empty_trange1, F, Fixtures, F) {
30753075
}
30763076
}
30773077

3078+
// corner case: expressions involving empty blocks
3079+
BOOST_FIXTURE_TEST_CASE_TEMPLATE(empty_block_expressions, F, Fixtures, F) {
3080+
auto& a = F::a;
3081+
auto& c = F::c;
3082+
3083+
// one of the dimensions has zero extent
3084+
const auto lb1 = std::array{3, 2, 0};
3085+
const auto ub1 = std::array{4, 2, 2};
3086+
3087+
const int ntiles = F::ntiles;
3088+
const auto lb2 = std::array{ntiles, 0, 0};
3089+
const auto ub2 = std::array{ntiles, ntiles, ntiles};
3090+
3091+
// preserve_lobound should go through without issues
3092+
{
3093+
std::decay_t<decltype(c)> result;
3094+
BOOST_CHECK_NO_THROW(result("a,b,c") =
3095+
a("a,b,c").block(lb1, ub1, preserve_lobound));
3096+
3097+
BOOST_CHECK_EQUAL(result.trange().tiles_range().volume(), 0);
3098+
}
3099+
{
3100+
std::decay_t<decltype(c)> result;
3101+
BOOST_CHECK_NO_THROW(result("a,b,c") =
3102+
a("a,b,c").block(lb2, ub2, preserve_lobound));
3103+
BOOST_CHECK_EQUAL(result.trange().tiles_range().volume(), 0);
3104+
}
3105+
3106+
// assignment of empty (zero volume) block
3107+
{
3108+
BOOST_CHECK_NO_THROW(c("a,b,c").block(lb1, ub1) =
3109+
a("a,b,c").block(lb1, ub1));
3110+
}
3111+
{
3112+
BOOST_CHECK_NO_THROW(c("a,b,c").block(lb2, ub2) =
3113+
a("a,b,c").block(lb2, ub2));
3114+
}
3115+
3116+
// assignment from empty array
3117+
{
3118+
std::decay_t<decltype(c)> z;
3119+
z("a,b,c") = a("a,b,c").block(lb2, ub2); // z is a full empty array
3120+
BOOST_CHECK_NO_THROW(c("a,b,c").block(lb2, ub2, preserve_lobound) =
3121+
z("a,b,c"));
3122+
}
3123+
}
3124+
30783125
BOOST_AUTO_TEST_SUITE_END()
30793126

30803127
#endif // TILEDARRAY_TEST_EXPRESSIONS_IMPL_H

0 commit comments

Comments
 (0)