diff --git a/include/xtensor/generators/xbuilder.hpp b/include/xtensor/generators/xbuilder.hpp index 1026b21c7..d007c50c9 100644 --- a/include/xtensor/generators/xbuilder.hpp +++ b/include/xtensor/generators/xbuilder.hpp @@ -390,6 +390,24 @@ namespace xt }; } + template + struct is_memory_independent_generator> + : std::conjunction, std::is_arithmetic, std::is_arithmetic> + { + }; + + template + struct is_memory_independent_generator> : std::true_type + { + }; + + // fn_impl only forwards to the wrapped functor, which may hold expressions + // (e.g. diagonal_fn, trilu_fn). + template + struct is_memory_independent_generator> : is_memory_independent_generator + { + }; + /** * Generates an array with ones on the diagonal. * @param shape shape of the resulting expression diff --git a/include/xtensor/generators/xgenerator.hpp b/include/xtensor/generators/xgenerator.hpp index 57d7d5d6c..f104a2cb8 100644 --- a/include/xtensor/generators/xgenerator.hpp +++ b/include/xtensor/generators/xgenerator.hpp @@ -61,6 +61,38 @@ namespace xt template concept xgenerator_concept = is_specialization_of>::value; + /** + * Trait telling whether a generator functor only produces values, or may + * read them from the memory of other expressions. + * + * An xgenerator whose functor is memory independent can never alias the + * destination of an assignment, so it is assigned without a temporary. + * The default is ``std::false_type``: unknown functors are assumed to read + * memory and keep the overlap protection. Specialize it to inherit from + * ``std::true_type`` only for functors that hold no reference to, and no + * copy of, an expression that can refer to the assignment destination. + * + * @tparam F the functor type + */ + template + struct is_memory_independent_generator : std::false_type + { + }; + + namespace detail + { + template + struct xgenerator_is_memory_independent : std::false_type + { + }; + + template + struct xgenerator_is_memory_independent> + : is_memory_independent_generator> + { + }; + } + template struct xiterable_inner_types> { @@ -87,7 +119,10 @@ namespace xt { static bool check_overlap(const E&, const memory_range&) { - return true; + // A functor that reads other expressions can alias the destination + // (e.g. a = concatenate(xtuple(a, a))), one that only produces + // values cannot. + return !detail::xgenerator_is_memory_independent>::value; } }; diff --git a/include/xtensor/generators/xrandom.hpp b/include/xtensor/generators/xrandom.hpp index 73b1763ae..5f2a5f921 100644 --- a/include/xtensor/generators/xrandom.hpp +++ b/include/xtensor/generators/xrandom.hpp @@ -243,6 +243,12 @@ namespace xt }; } + // Values come from the engine and the distribution, never from an expression. + template + struct is_memory_independent_generator> : std::true_type + { + }; + namespace random { /** diff --git a/test/test_xassign.cpp b/test/test_xassign.cpp index de81c6dec..751a3e664 100644 --- a/test/test_xassign.cpp +++ b/test/test_xassign.cpp @@ -8,17 +8,70 @@ ****************************************************************************/ #include +#include #include #include #include "xtensor/containers/xarray.hpp" #include "xtensor/containers/xtensor.hpp" #include "xtensor/core/xassign.hpp" +#include "xtensor/generators/xbuilder.hpp" +#include "xtensor/generators/xrandom.hpp" #include "xtensor/misc/xmanipulation.hpp" +#include "xtensor/views/xview.hpp" #include "test_common.hpp" #include "test_common_macros.hpp" +namespace +{ + // Counts the buffers allocated for the data of a container. + template + struct counting_allocator : std::allocator + { + using value_type = T; + inline static std::size_t allocations = 0; + + template + struct rebind + { + using other = counting_allocator; + }; + + counting_allocator() = default; + + template + counting_allocator(const counting_allocator&) + { + } + + T* allocate(std::size_t n) + { + ++allocations; + return std::allocator::allocate(n); + } + }; + + // A generator functor that reads another expression, and therefore must + // keep the overlap protection. + struct reverse_reader + { + using value_type = int; + const xt::xarray& source; + + int operator()(std::size_t i) const + { + return source(source.size() - 1 - i); + } + + template + int element(It first, It) const + { + return (*this)(*first); + } + }; +} + // a dummy shape *not derived* from std::vector but compatible template class my_vector @@ -136,6 +189,120 @@ class my_vector namespace xt { + TEST(xassign, memory_independent_generator_trait) + { + xarray a = {1, 2, 3, 4}; + auto reverse = detail::make_xgenerator(reverse_reader{a}, {4}); + auto independent = [](const auto& e) + { + return detail::xgenerator_is_memory_independent>::value; + }; + + EXPECT_TRUE(independent(arange(4))); + EXPECT_TRUE(independent(arange(0, 8, 2))); + EXPECT_TRUE(independent(linspace(0., 1., 4))); + EXPECT_TRUE(independent(eye(3))); + EXPECT_TRUE(independent(random::rand({4}))); + EXPECT_TRUE(independent(random::randint({4}))); + + // Functors holding expressions, and unknown functors, are not. + EXPECT_FALSE(independent(reverse)); + EXPECT_FALSE(independent(concatenate(xtuple(a, a)))); + EXPECT_FALSE(independent(stack(xtuple(a, a)))); + EXPECT_FALSE(independent(diag(a))); + EXPECT_FALSE(independent(a)); + } + +#ifndef XTENSOR_FORCE_TEMPORARY_MEMORY_IN_ASSIGNMENTS + TEST(xassign, memory_independent_generator_reuses_storage) + { + using allocator = counting_allocator; + using vector_type = xtensor; + using matrix_type = xtensor; + + vector_type a = vector_type::from_shape({16}); + allocator::allocations = 0; + a = arange(16); + EXPECT_EQ(allocator::allocations, 0u); + for (std::size_t i = 0; i < a.size(); ++i) + { + EXPECT_EQ(a(i), double(i)); + } + + allocator::allocations = 0; + a = linspace(0., 30., 16); + EXPECT_EQ(allocator::allocations, 0u); + for (std::size_t i = 0; i < a.size(); ++i) + { + EXPECT_EQ(a(i), double(2 * i)); + } + + allocator::allocations = 0; + a = random::rand({16}, 2., 3.); + EXPECT_EQ(allocator::allocations, 0u); + for (std::size_t i = 0; i < a.size(); ++i) + { + EXPECT_GE(a(i), 2.); + EXPECT_LT(a(i), 3.); + } + + matrix_type m = matrix_type::from_shape({4, 4}); + allocator::allocations = 0; + m = eye(4); + EXPECT_EQ(allocator::allocations, 0u); + for (std::size_t i = 0; i < 4; ++i) + { + for (std::size_t j = 0; j < 4; ++j) + { + EXPECT_EQ(m(i, j), i == j ? 1. : 0.); + } + } + } +#endif + + TEST(xassign, memory_independent_generator_resizes) + { + xarray a = {{-1., -1.}, {-1., -1.}}; + a = arange(5); + EXPECT_EQ(a, (xarray{0., 1., 2., 3., 4.})); + + a = arange(0); + EXPECT_EQ(a.size(), 0u); + + a = linspace(2., 4., 3); + EXPECT_EQ(a, (xarray{2., 3., 4.})); + + a = eye(2); + EXPECT_EQ(a, (xarray{{1., 0.}, {0., 1.}})); + } + + TEST(xassign, memory_independent_generator_to_view) + { + xarray a = {{-1., -1., -1.}, {-1., -1., -1.}, {-1., -1., -1.}}; + view(a, 1, all()) = arange(3); + EXPECT_EQ(a, (xarray{{-1., -1., -1.}, {0., 1., 2.}, {-1., -1., -1.}})); + + view(a, all(), 2) = linspace(4., 6., 3); + EXPECT_EQ(a, (xarray{{-1., -1., 4.}, {0., 1., 5.}, {-1., -1., 6.}})); + + view(a, range(0, 2), range(0, 2)) = eye(2); + EXPECT_EQ(a, (xarray{{1., 0., 4.}, {0., 1., 5.}, {-1., -1., 6.}})); + } + + TEST(xassign, reading_generators_keep_overlap_protection) + { + xarray a = {1, 2, 3, 4}; + a = detail::make_xgenerator(reverse_reader{a}, {4}); + EXPECT_EQ(a, (xarray{4, 3, 2, 1})); + + a = concatenate(xtuple(a, a)); + EXPECT_EQ(a, (xarray{4, 3, 2, 1, 4, 3, 2, 1})); + + xarray m = {{1, 2}, {3, 4}}; + m = diagonal(m); + EXPECT_EQ(m, (xarray{1, 4})); + } + template struct rebind_container> {