Skip to content
Draft
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
5 changes: 5 additions & 0 deletions src/api/c/assign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ static void assign(Array<Tout>& out, const vector<af_seq> seqs,

out.eval();

// createSubArray copies a non-linear parent before slicing it, so a write
// through the slice would land in a temporary and be lost (#3534).
// Materialise the output as a linear array first.
if (!out.isLinear()) { out = copyArray(out); }

dim4 oDims = toDims(seqs, outDs);

bool isVec = true;
Expand Down
24 changes: 24 additions & 0 deletions test/assign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,3 +1037,27 @@ TEST(Index, ISSUE_2533) {

ASSERT_VEC_ARRAY_EQ(gold, dim4(5, 10), a);
}

// Assigning into an array that is itself a strided view of a larger buffer
// (here the first four rows of a 5x5) silently did nothing, because the
// write went into a temporary copy of the view.
TEST(Assign, IntoStridedView_ISSUE_3534) {
array a = af::range(dim4(5, 5));
a = a.rows(0, 3);
a(0, 0) = 1234;
ASSERT_EQ(1234.f, a.scalar<float>());

array b = af::range(dim4(5, 5));
b = b.rows(1, 3);
b(span, 2) = constant(-1, 3, 1);
vector<float> h(b.elements());
b.host(h.data());
for (int i = 0; i < 3; i++) { ASSERT_EQ(-1.f, h[i + 2 * 3]) << "row " << i; }
ASSERT_EQ(1.f, h[0]);

array c = af::range(dim4(5, 5));
c = c.rows(0, 3);
array idx = af::seq(0, 1);
c(idx.as(s32), 0) = constant(7, 2, 1);
ASSERT_EQ(7.f, c.scalar<float>());
}
Loading