Skip to content
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

Buggy behaviour of non-contiguous xadaptor and scalar assignment #2773

Open
ewoudwempe opened this issue Feb 8, 2024 · 1 comment
Open

Comments

@ewoudwempe
Copy link
Contributor

Hi,
When assigning with a scalar on a xadaptor with non-contiguous strides, there is some problem where contiguous assignment is done instead, producing unexpected results.
For instance, the following does not work:

void test_strided_scalar_assign() {
  auto data = std::vector<double>(8);
  std::fill(data.begin(), data.end(), 0.);
  auto adapter_strided_noncont = xt::adapt(
      data.data(), 6, xt::no_ownership(), std::vector<size_t>{2, 3},
      std::vector<size_t>{4, 1});
  xt::noalias(adapter_strided_noncont) += 1;
  auto expected = std::vector<double>({1, 1, 1, 0, 1, 1, 1, 0});
  assert(expected == data);
}

When running this, the first 6 values of data are incremented, instead of taking into account the {4,1} strides.
What's do you think the best way is to fix this?

@spectre-ns
Copy link
Contributor

spectre-ns commented Dec 26, 2024

This is the source of your issue:

image

In the case of contiguous containers this implementation works fine. We don't care about strides because adding a scalar to the whole container is independent of the order in which the container is traversed. For discontinuous strides that is not true as we would stride over some locations. Additionally, when we call begin to the storage in this case we get a contiguous iterator from the underlying data pointer which also isn't aware of strides.

@JohanMabille does the adaptor assume a contiguous underlying data structure?

@ewoudwempe I think your best way of fixing this would be to create your own xtensor compatible container which provides iterators which step through your data in the right order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants