Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/xtensor/generators/xbuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,24 @@ namespace xt
};
}

template <class T, class R, class S>
struct is_memory_independent_generator<detail::arange_generator<T, R, S>>
: std::conjunction<std::is_arithmetic<T>, std::is_arithmetic<R>, std::is_arithmetic<S>>
{
};

template <class T>
struct is_memory_independent_generator<detail::eye_fn<T>> : std::true_type
{
};

// fn_impl only forwards to the wrapped functor, which may hold expressions
// (e.g. diagonal_fn, trilu_fn).
template <class F>
struct is_memory_independent_generator<detail::fn_impl<F>> : is_memory_independent_generator<F>
{
};

/**
* Generates an array with ones on the diagonal.
* @param shape shape of the resulting expression
Expand Down
37 changes: 36 additions & 1 deletion include/xtensor/generators/xgenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,38 @@ namespace xt
template <typename T>
concept xgenerator_concept = is_specialization_of<xgenerator, std::decay_t<T>>::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 <class F>
struct is_memory_independent_generator : std::false_type
{
};

namespace detail
{
template <class E>
struct xgenerator_is_memory_independent : std::false_type
{
};

template <class F, class R, class S>
struct xgenerator_is_memory_independent<xgenerator<F, R, S>>
: is_memory_independent_generator<std::decay_t<F>>
{
};
}

template <class C, class R, class S>
struct xiterable_inner_types<xgenerator<C, R, S>>
{
Expand All @@ -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<std::decay_t<E>>::value;
}
};

Expand Down
6 changes: 6 additions & 0 deletions include/xtensor/generators/xrandom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ namespace xt
};
}

// Values come from the engine and the distribution, never from an expression.
template <class T, class E, class D>
struct is_memory_independent_generator<detail::random_impl<T, E, D>> : std::true_type
{
};

namespace random
{
/**
Expand Down
167 changes: 167 additions & 0 deletions test/test_xassign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,70 @@
****************************************************************************/

#include <iterator>
#include <memory>
#include <type_traits>
#include <vector>

#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 <class T>
struct counting_allocator : std::allocator<T>
{
using value_type = T;
inline static std::size_t allocations = 0;

template <class U>
struct rebind
{
using other = counting_allocator<U>;
};

counting_allocator() = default;

template <class U>
counting_allocator(const counting_allocator<U>&)
{
}

T* allocate(std::size_t n)
{
++allocations;
return std::allocator<T>::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<int>& source;

int operator()(std::size_t i) const
{
return source(source.size() - 1 - i);
}

template <class It>
int element(It first, It) const
{
return (*this)(*first);
}
};
}

// a dummy shape *not derived* from std::vector but compatible
template <class T>
class my_vector
Expand Down Expand Up @@ -136,6 +189,120 @@ class my_vector
namespace xt
{

TEST(xassign, memory_independent_generator_trait)
{
xarray<int> 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<std::decay_t<decltype(e)>>::value;
};

EXPECT_TRUE(independent(arange<double>(4)));
EXPECT_TRUE(independent(arange<int>(0, 8, 2)));
EXPECT_TRUE(independent(linspace<double>(0., 1., 4)));
EXPECT_TRUE(independent(eye<double>(3)));
EXPECT_TRUE(independent(random::rand<double>({4})));
EXPECT_TRUE(independent(random::randint<int>({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<double>;
using vector_type = xtensor<double, 1, layout_type::row_major, allocator>;
using matrix_type = xtensor<double, 2, layout_type::row_major, allocator>;

vector_type a = vector_type::from_shape({16});
allocator::allocations = 0;
a = arange<double>(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<double>(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<double>({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<double>(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<double> a = {{-1., -1.}, {-1., -1.}};
a = arange<double>(5);
EXPECT_EQ(a, (xarray<double>{0., 1., 2., 3., 4.}));

a = arange<double>(0);
EXPECT_EQ(a.size(), 0u);

a = linspace<double>(2., 4., 3);
EXPECT_EQ(a, (xarray<double>{2., 3., 4.}));

a = eye<double>(2);
EXPECT_EQ(a, (xarray<double>{{1., 0.}, {0., 1.}}));
}

TEST(xassign, memory_independent_generator_to_view)
{
xarray<double> a = {{-1., -1., -1.}, {-1., -1., -1.}, {-1., -1., -1.}};
view(a, 1, all()) = arange<double>(3);
EXPECT_EQ(a, (xarray<double>{{-1., -1., -1.}, {0., 1., 2.}, {-1., -1., -1.}}));

view(a, all(), 2) = linspace<double>(4., 6., 3);
EXPECT_EQ(a, (xarray<double>{{-1., -1., 4.}, {0., 1., 5.}, {-1., -1., 6.}}));

view(a, range(0, 2), range(0, 2)) = eye<double>(2);
EXPECT_EQ(a, (xarray<double>{{1., 0., 4.}, {0., 1., 5.}, {-1., -1., 6.}}));
}

TEST(xassign, reading_generators_keep_overlap_protection)
{
xarray<int> a = {1, 2, 3, 4};
a = detail::make_xgenerator(reverse_reader{a}, {4});
EXPECT_EQ(a, (xarray<int>{4, 3, 2, 1}));

a = concatenate(xtuple(a, a));
EXPECT_EQ(a, (xarray<int>{4, 3, 2, 1, 4, 3, 2, 1}));

xarray<int> m = {{1, 2}, {3, 4}};
m = diagonal(m);
EXPECT_EQ(m, (xarray<int>{1, 4}));
}

template <class T, class C_T>
struct rebind_container<T, my_vector<C_T>>
{
Expand Down