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

SubArray of subArray now returns subArray of original parent #3538

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/backend/cpu/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,23 @@ Array<T> createSubArray(const Array<T> &parent, const vector<af_seq> &index,
bool copy) {
parent.eval();

dim4 dDims = parent.getDataDims();
dim4 parent_strides = parent.strides();
const dim4 &dDims = parent.getDataDims();
const dim4 &parent_strides = parent.strides();

// (sub)Arrays remain linear when the strides corresponds to dataStrides
bool parent_isLinear = (parent_strides[0] == 1);
for (dim_t i = parent.ndims() - 1; i > 0; i--) {
parent_isLinear &=
parent_strides[i] == dDims[i - 1] * parent_strides[i - 1];
}

if (parent.isLinear() == false) {
if (!parent_isLinear) {
if (!copy) {
// Linearizing parent through copy, is in conflict with the request
// of remaining inLine.
AF_ERROR("createSubArray inLine is impossible on non-Linear arrays",
AF_ERR_INVALID_ARRAY);
}
const Array<T> parentCopy = copyArray(parent);
return createSubArray(parentCopy, index, copy);
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/cpu/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class Array {
dim_t getOffset() const { return info.getOffset(); }
shared_ptr<T> getData() const { return data; }

dim4 getDataDims() const { return data_dims; }
const dim4 &getDataDims() const { return data_dims; }

void setDataDims(const dim4 &new_dims);

Expand Down
19 changes: 16 additions & 3 deletions src/backend/cuda/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,23 @@ Array<T> createSubArray(const Array<T> &parent,
const std::vector<af_seq> &index, bool copy) {
parent.eval();

dim4 dDims = parent.getDataDims();
dim4 parent_strides = parent.strides();
const dim4 &dDims = parent.getDataDims();
const dim4 &parent_strides = parent.strides();

// (sub)Arrays remain linear when the strides corresponds to dataStrides
bool parent_isLinear = (parent_strides[0] == 1);
for (dim_t i = parent.ndims() - 1; i > 0; i--) {
parent_isLinear &=
parent_strides[i] == dDims[i - 1] * parent_strides[i - 1];
}

if (parent.isLinear() == false) {
if (!parent_isLinear) {
if (!copy) {
// Linearizing parent through copy, is in conflict with the request
// of remaining inLine.
AF_ERROR("createSubArray inLine is impossible on non-Linear arrays",
AF_ERR_INVALID_ARRAY);
}
const Array<T> parentCopy = copyArray(parent);
return createSubArray(parentCopy, index, copy);
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/cuda/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class Array {

dim_t getOffset() const { return info.getOffset(); }

dim4 getDataDims() const { return data_dims; }
const dim4 &getDataDims() const { return data_dims; }

void setDataDims(const dim4 &new_dims);

Expand Down
19 changes: 16 additions & 3 deletions src/backend/oneapi/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,23 @@ Array<T> createSubArray(const Array<T> &parent, const vector<af_seq> &index,
bool copy) {
parent.eval();

dim4 dDims = parent.getDataDims();
dim4 parent_strides = parent.strides();
const dim4 &dDims = parent.getDataDims();
const dim4 &parent_strides = parent.strides();

// (sub)Arrays remain linear when the strides corresponds to dataStrides
bool parent_isLinear = (parent_strides[0] == 1);
for (dim_t i = parent.ndims() - 1; i > 0; i--) {
parent_isLinear &=
parent_strides[i] == dDims[i - 1] * parent_strides[i - 1];
}

if (parent.isLinear() == false) {
if (!parent_isLinear) {
if (!copy) {
// Linearizing parent through copy, is in conflict with the request
// of remaining inLine.
AF_ERROR("createSubArray inLine is impossible on non-Linear arrays",
AF_ERR_INVALID_ARRAY);
}
const Array<T> parentCopy = copyArray(parent);
return createSubArray(parentCopy, index, copy);
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/oneapi/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class Array {

dim_t getOffset() const { return info.getOffset(); }

dim4 getDataDims() const { return data_dims; }
const dim4 &getDataDims() const { return data_dims; }

void setDataDims(const dim4 &new_dims);

Expand Down
24 changes: 18 additions & 6 deletions src/backend/opencl/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,18 +441,30 @@ Array<T> createSubArray(const Array<T> &parent, const vector<af_seq> &index,
bool copy) {
parent.eval();

dim4 dDims = parent.getDataDims();
dim4 parent_strides = parent.strides();
const dim4 &dDims = parent.getDataDims();
const dim4 &parent_strides = parent.strides();

// (sub)Arrays remain linear when the strides corresponds to dataStrides
bool parent_isLinear = (parent_strides[0] == 1);
for (dim_t i = parent.ndims() - 1; i > 0; i--) {
parent_isLinear &=
parent_strides[i] == dDims[i - 1] * parent_strides[i - 1];
}

if (parent.isLinear() == false) {
if (!parent_isLinear) {
if (!copy) {
// Linearizing parent through copy, is in conflict with the request
// of remaining inLine.
AF_ERROR("createSubArray inLine is impossible on non-Linear arrays",
AF_ERR_INVALID_ARRAY);
}
const Array<T> parentCopy = copyArray(parent);
return createSubArray(parentCopy, index, copy);
}

const dim4 &pDims = parent.dims();

dim4 dims = toDims(index, pDims);
dim4 strides = toStride(index, dDims);
dim4 dims = toDims(index, pDims);
dim4 strides = toStride(index, dDims);

// Find total offsets after indexing
dim4 offsets = toOffset(index, pDims);
Expand Down
2 changes: 1 addition & 1 deletion src/backend/opencl/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class Array {

dim_t getOffset() const { return info.getOffset(); }

dim4 getDataDims() const { return data_dims; }
const dim4 &getDataDims() const { return data_dims; }

void setDataDims(const dim4 &new_dims);

Expand Down
17 changes: 17 additions & 0 deletions test/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,23 @@ TEST(Array, ISSUE_951) {
array b = a.cols(0, 20).rows(10, 20);
}

TEST(Array, ISSUE_3534) {
// This works
// array a = range(dim4(5,5));
// a = a.rows(0,3).copy();
// Following assignment failed silently without above copy()
// a(0,0) = 1234;

array a = range(dim4(5, 5));
a = a.rows(1, 4);
a(1, 1) = 1234;

array b = range(dim4(4, 5)) + 1.0;
b(1, 1) = 1234;

ASSERT_ARRAYS_EQ(a, b);
}

TEST(Array, CreateHandleInvalidNullDimsPointer) {
af_array out = 0;
EXPECT_EQ(AF_ERR_ARG, af_create_handle(&out, 1, NULL, f32));
Expand Down