Skip to content

fix Tensor(range, elemop) ctor to use placement-new instead of (move) assignment #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2024
Merged
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
31 changes: 20 additions & 11 deletions src/TiledArray/tensor/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,12 @@ class Tensor {
size_t size = range_.volume() * nbatch;
allocator_type allocator;
auto* ptr = allocator.allocate(size);
if (default_construct) {
std::uninitialized_default_construct_n(ptr, size);
// std::uninitialized_value_construct_n(ptr, size);
// default construct elements of data only if can have any effect ...
if constexpr (!std::is_trivially_default_constructible_v<T>) {
// .. and requested
if (default_construct) {
std::uninitialized_default_construct_n(ptr, size);
}
}
auto deleter = [
#ifdef TA_TENSOR_MEM_TRACE
Expand Down Expand Up @@ -182,9 +185,12 @@ class Tensor {
size_t size = range_.volume() * nbatch;
allocator_type allocator;
auto* ptr = allocator.allocate(size);
if (default_construct) {
std::uninitialized_default_construct_n(ptr, size);
// std::uninitialized_value_construct_n(ptr, size);
// default construct elements of data only if can have any effect ...
if constexpr (!std::is_trivially_default_constructible_v<T>) {
// .. and requested
if (default_construct) {
std::uninitialized_default_construct_n(ptr, size);
}
}
auto deleter = [
#ifdef TA_TENSOR_MEM_TRACE
Expand Down Expand Up @@ -288,7 +294,8 @@ class Tensor {
}

/// Construct a tensor with a range equal to \c range. The data is
/// uninitialized.
/// default-initialized (which, for `T` with trivial default constructor,
/// means data is uninitialized).
/// \param range The range of the tensor
/// \param nbatch The number of batches (default is 1)
explicit Tensor(const range_type& range, size_type nbatch = 1)
Expand Down Expand Up @@ -336,9 +343,10 @@ class Tensor {
value_type, ElementIndexOp, const Range::index_type&>>>
Tensor(const range_type& range, const ElementIndexOp& element_idx_op)
: Tensor(range, 1, default_construct{false}) {
auto* data_ptr = data_.get();
pointer MADNESS_RESTRICT const data = this->data();
for (auto&& element_idx : range) {
data_ptr[range.ordinal(element_idx)] = element_idx_op(element_idx);
const auto ord = range.ordinal(element_idx);
new (data + ord) value_type(element_idx_op(element_idx));
}
}

Expand All @@ -350,8 +358,9 @@ class Tensor {
Tensor(const range_type& range, InIter it)
: Tensor(range, 1, default_construct{false}) {
auto n = range.volume();
pointer MADNESS_RESTRICT const data = this->data();
for (size_type i = 0ul; i < n; ++i, ++it) data[i] = *it;
pointer MADNESS_RESTRICT data = this->data();
for (size_type i = 0ul; i < n; ++i, ++it, ++data)
new (data) value_type(*it);
}

template <typename U>
Expand Down
9 changes: 9 additions & 0 deletions tests/tensor_of_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(default_constructor, ITensor, itensor_types) {

BOOST_AUTO_TEST_CASE_TEMPLATE(unary_constructor, ITensor, itensor_types) {
const auto& a = ToT<ITensor>(0);

// apply element-wise op with default initializer
// this is a reproducer for
// https://github.com/ValeevGroup/tiledarray/issues/445
{
BOOST_CHECK_NO_THROW(
Tensor<ITensor> t(a.range(), [](auto&& l) { return ITensor(); }));
}

// apply element-wise op
BOOST_CHECK_NO_THROW(Tensor<ITensor> t(a, [](const int l) { return l * 2; }));
Tensor<ITensor> t(a, [](const int l) { return l * 2; });
Expand Down