Skip to content

Commit afd38a9

Browse files
committed
SubArray of subArray now returns subArray of original parent
1 parent 4061db8 commit afd38a9

File tree

9 files changed

+87
-19
lines changed

9 files changed

+87
-19
lines changed

src/backend/cpu/Array.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,23 @@ Array<T> createSubArray(const Array<T> &parent, const vector<af_seq> &index,
276276
bool copy) {
277277
parent.eval();
278278

279-
dim4 dDims = parent.getDataDims();
280-
dim4 parent_strides = parent.strides();
279+
const dim4 &dDims = parent.getDataDims();
280+
const dim4 &parent_strides = parent.strides();
281+
282+
// (sub)Arrays remain linear when the strides corresponds to dataStrides
283+
bool parent_isLinear = (parent_strides[0] == 1);
284+
for (dim_t i = parent.ndims() - 1; i > 0; i--) {
285+
parent_isLinear &=
286+
parent_strides[i] == dDims[i - 1] * parent_strides[i - 1];
287+
}
281288

282-
if (parent.isLinear() == false) {
289+
if (!parent_isLinear) {
290+
if (!copy) {
291+
// Linearizing parent through copy, is in conflict with the request
292+
// of remaining inLine.
293+
AF_ERROR("createSubArray inLine is impossible on non-Linear arrays",
294+
AF_ERR_INVALID_ARRAY);
295+
}
283296
const Array<T> parentCopy = copyArray(parent);
284297
return createSubArray(parentCopy, index, copy);
285298
}

src/backend/cpu/Array.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class Array {
239239
dim_t getOffset() const { return info.getOffset(); }
240240
shared_ptr<T> getData() const { return data; }
241241

242-
dim4 getDataDims() const { return data_dims; }
242+
const dim4 &getDataDims() const { return data_dims; }
243243

244244
void setDataDims(const dim4 &new_dims);
245245

src/backend/cuda/Array.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,23 @@ Array<T> createSubArray(const Array<T> &parent,
388388
const std::vector<af_seq> &index, bool copy) {
389389
parent.eval();
390390

391-
dim4 dDims = parent.getDataDims();
392-
dim4 parent_strides = parent.strides();
391+
const dim4 &dDims = parent.getDataDims();
392+
const dim4 &parent_strides = parent.strides();
393+
394+
// (sub)Arrays remain linear when the strides corresponds to dataStrides
395+
bool parent_isLinear = (parent_strides[0] == 1);
396+
for (dim_t i = parent.ndims() - 1; i > 0; i--) {
397+
parent_isLinear &=
398+
parent_strides[i] == dDims[i - 1] * parent_strides[i - 1];
399+
}
393400

394-
if (parent.isLinear() == false) {
401+
if (!parent_isLinear) {
402+
if (!copy) {
403+
// Linearizing parent through copy, is in conflict with the request
404+
// of remaining inLine.
405+
AF_ERROR("createSubArray inLine is impossible on non-Linear arrays",
406+
AF_ERR_INVALID_ARRAY);
407+
}
395408
const Array<T> parentCopy = copyArray(parent);
396409
return createSubArray(parentCopy, index, copy);
397410
}

src/backend/cuda/Array.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class Array {
237237

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

240-
dim4 getDataDims() const { return data_dims; }
240+
const dim4 &getDataDims() const { return data_dims; }
241241

242242
void setDataDims(const dim4 &new_dims);
243243

src/backend/oneapi/Array.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,23 @@ Array<T> createSubArray(const Array<T> &parent, const vector<af_seq> &index,
431431
bool copy) {
432432
parent.eval();
433433

434-
dim4 dDims = parent.getDataDims();
435-
dim4 parent_strides = parent.strides();
434+
const dim4 &dDims = parent.getDataDims();
435+
const dim4 &parent_strides = parent.strides();
436+
437+
// (sub)Arrays remain linear when the strides corresponds to dataStrides
438+
bool parent_isLinear = (parent_strides[0] == 1);
439+
for (dim_t i = parent.ndims() - 1; i > 0; i--) {
440+
parent_isLinear &=
441+
parent_strides[i] == dDims[i - 1] * parent_strides[i - 1];
442+
}
436443

437-
if (parent.isLinear() == false) {
444+
if (!parent_isLinear) {
445+
if (!copy) {
446+
// Linearizing parent through copy, is in conflict with the request
447+
// of remaining inLine.
448+
AF_ERROR("createSubArray inLine is impossible on non-Linear arrays",
449+
AF_ERR_INVALID_ARRAY);
450+
}
438451
const Array<T> parentCopy = copyArray(parent);
439452
return createSubArray(parentCopy, index, copy);
440453
}

src/backend/oneapi/Array.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ class Array {
286286

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

289-
dim4 getDataDims() const { return data_dims; }
289+
const dim4 &getDataDims() const { return data_dims; }
290290

291291
void setDataDims(const dim4 &new_dims);
292292

src/backend/opencl/Array.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -441,18 +441,30 @@ Array<T> createSubArray(const Array<T> &parent, const vector<af_seq> &index,
441441
bool copy) {
442442
parent.eval();
443443

444-
dim4 dDims = parent.getDataDims();
445-
dim4 parent_strides = parent.strides();
444+
const dim4 &dDims = parent.getDataDims();
445+
const dim4 &parent_strides = parent.strides();
446+
447+
// (sub)Arrays remain linear when the strides corresponds to dataStrides
448+
bool parent_isLinear = (parent_strides[0] == 1);
449+
for (dim_t i = parent.ndims() - 1; i > 0; i--) {
450+
parent_isLinear &=
451+
parent_strides[i] == dDims[i - 1] * parent_strides[i - 1];
452+
}
446453

447-
if (parent.isLinear() == false) {
454+
if (!parent_isLinear) {
455+
if (!copy) {
456+
// Linearizing parent through copy, is in conflict with the request
457+
// of remaining inLine.
458+
AF_ERROR("createSubArray inLine is impossible on non-Linear arrays",
459+
AF_ERR_INVALID_ARRAY);
460+
}
448461
const Array<T> parentCopy = copyArray(parent);
449462
return createSubArray(parentCopy, index, copy);
450463
}
451464

452465
const dim4 &pDims = parent.dims();
453-
454-
dim4 dims = toDims(index, pDims);
455-
dim4 strides = toStride(index, dDims);
466+
dim4 dims = toDims(index, pDims);
467+
dim4 strides = toStride(index, dDims);
456468

457469
// Find total offsets after indexing
458470
dim4 offsets = toOffset(index, pDims);

src/backend/opencl/Array.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class Array {
259259

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

262-
dim4 getDataDims() const { return data_dims; }
262+
const dim4 &getDataDims() const { return data_dims; }
263263

264264
void setDataDims(const dim4 &new_dims);
265265

test/array.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,23 @@ TEST(Array, ISSUE_951) {
420420
array b = a.cols(0, 20).rows(10, 20);
421421
}
422422

423+
TEST(Array, ISSUE_3534) {
424+
// This works
425+
// array a = range(dim4(5,5));
426+
// a = a.rows(0,3).copy();
427+
// Following assignment failed silently without above copy()
428+
// a(0,0) = 1234;
429+
430+
array a = range(dim4(5, 5));
431+
a = a.rows(1, 4);
432+
a(1, 1) = 1234;
433+
434+
array b = range(dim4(4, 5)) + 1.0;
435+
b(1, 1) = 1234;
436+
437+
ASSERT_ARRAYS_EQ(a, b);
438+
}
439+
423440
TEST(Array, CreateHandleInvalidNullDimsPointer) {
424441
af_array out = 0;
425442
EXPECT_EQ(AF_ERR_ARG, af_create_handle(&out, 1, NULL, f32));

0 commit comments

Comments
 (0)