Skip to content
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
1 change: 1 addition & 0 deletions src/core/tdecoration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace spla {
[[nodiscard]] bool is_valid(F format) const { return is_valid_i(static_cast<int>(format)); }
void validate(F format) { m_is_valid.set(static_cast<int>(format), true); }
void invalidate() { m_is_valid.reset(); }
void invalidate(F format) { m_is_valid.reset(static_cast<int>(format)); }

void set_fill_value(T value) { m_fill_value = value; }
T get_fill_value() const { return m_fill_value; }
Expand Down
5 changes: 3 additions & 2 deletions src/core/tmatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ namespace spla {
template<typename T>
Status TMatrix<T>::set_fill_value(const ref_ptr<Scalar>& value) {
if (value) {
m_storage.invalidate();

// All matrix formats are sparse and never materialize the fill
// value in storage (it is applied on read), so no representation
// needs to be invalidated and no data can be lost.
if constexpr (std::is_same<T, T_INT>::value) m_storage.set_fill_value(value->as_int());
if constexpr (std::is_same<T, T_UINT>::value) m_storage.set_fill_value(value->as_uint());
if constexpr (std::is_same<T, T_FLOAT>::value) m_storage.set_fill_value(value->as_float());
Expand Down
12 changes: 11 additions & 1 deletion src/core/tvector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,17 @@ namespace spla {
template<typename T>
Status TVector<T>::set_fill_value(const ref_ptr<Scalar>& value) {
if (value) {
m_storage.invalidate();
// Sparse representations (Coo, Dok) store explicit entries only and are
// unaffected by the fill value. Dense representations materialize it, so
// they must be rebuilt. If no sparse representation is valid, convert the
// data to COO first (using the old fill value) so it is not lost.
if (!m_storage.is_valid(FormatVector::CpuCoo) && !m_storage.is_valid(FormatVector::CpuDok)) {
validate_rw(FormatVector::CpuCoo);
}
m_storage.invalidate(FormatVector::CpuDense);
#if defined(SPLA_BUILD_OPENCL)
m_storage.invalidate(FormatVector::AccDense);
#endif

if constexpr (std::is_same<T, T_INT>::value) m_storage.set_fill_value(value->as_int());
if constexpr (std::is_same<T, T_UINT>::value) m_storage.set_fill_value(value->as_uint());
Expand Down
3 changes: 3 additions & 0 deletions src/cpu/cpu_format_coo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ namespace spla {

Rr[i].emplace_back(j, x);
}

// keep the LIL values counter in sync with the actual row contents
out.values = in.values;
}

template<typename T>
Expand Down
7 changes: 7 additions & 0 deletions src/cpu/cpu_mxm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ namespace spla {

std::vector<T> R_tmp(DN, I);

uint values = 0;

for (uint row_R = 0; row_R < DM; row_R++) {
const auto& A_lst = p_lil_A->Ar[row_R];
auto& R_lst = p_lil_R->Ar[row_R];
Expand All @@ -109,10 +111,15 @@ namespace spla {
for (uint col_R = 0; col_R < DN; col_R++) {
if (R_tmp[col_R] != I) {
R_lst.emplace_back(col_R, R_tmp[col_R]);
values += 1;
}
}
}

// keep the LIL values counter in sync with the actual row contents,
// otherwise conversions sized from it (e.g. Lil -> Coo) write out of bounds
p_lil_R->values = values;

return Status::Ok;
}
};
Expand Down
7 changes: 7 additions & 0 deletions src/cpu/cpu_mxmT_masked.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ namespace spla {
auto DM = R->get_n_rows();
auto I = init->get_value();

uint values = 0;

for (uint row_R = 0; row_R < DM; row_R++) {
const auto& mask_lst = p_lil_mask->Ar[row_R];
const auto& A_lst = p_lil_A->Ar[row_R];
Expand Down Expand Up @@ -120,10 +122,15 @@ namespace spla {

if (r != I) {
R_lst.emplace_back(mask_i, r);
values += 1;
}
}
}

// keep the LIL values counter in sync with the actual row contents,
// otherwise conversions sized from it (e.g. Lil -> Coo) write out of bounds
p_lil_R->values = values;

return Status::Ok;
}
};
Expand Down
34 changes: 34 additions & 0 deletions tests/test_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,38 @@ TEST(matrix, emult) {
}
}

TEST(matrix, set_fill_value_preserves_data) {
// Regression test: setting the fill value must not discard explicit
// entries. All matrix formats are sparse and never materialize the fill
// value in storage (it is applied on read), so the old implementation,
// which invalidated every format, silently lost all data on the next read.
const spla::uint M = 4, N = 4;

auto imat = spla::Matrix::make(M, N, spla::INT);
imat->set_int(0, 1, 10);
imat->set_int(2, 2, 20);

imat->set_fill_value(spla::Scalar::make_int(-1));

int v;
imat->get_int(0, 1, v);
EXPECT_EQ(10, v);
imat->get_int(2, 2, v);
EXPECT_EQ(20, v);
imat->get_int(0, 0, v);
EXPECT_EQ(-1, v);// missing entries report the new fill value

// data must survive round trips through the other formats
const spla::FormatMatrix formats[] = {spla::FormatMatrix::CpuLil, spla::FormatMatrix::CpuDok, spla::FormatMatrix::CpuCoo, spla::FormatMatrix::CpuCsr};
for (auto format : formats) {
imat->set_format(format);
imat->get_int(0, 1, v);
EXPECT_EQ(10, v);
imat->get_int(2, 2, v);
EXPECT_EQ(20, v);
imat->get_int(0, 0, v);
EXPECT_EQ(-1, v);
}
}

SPLA_GTEST_MAIN_WITH_FINALIZE
32 changes: 32 additions & 0 deletions tests/test_mxm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,36 @@ TEST(mxm, perf_zero) {
}
}

TEST(mxm, read_after_exec) {
// Regression test: the kernel must keep the LIL `values` counter in sync
// with the actual row contents. Otherwise the subsequent conversion to COO
// (e.g. via read()) sizes the output from the stale counter and writes
// out of bounds.
spla::uint M = 3, N = 4, K = 2;

auto R = spla::Matrix::make(M, K, spla::FLOAT);
auto A = spla::Matrix::make(M, N, spla::FLOAT);
auto B = spla::Matrix::make(N, K, spla::FLOAT);
auto init = spla::Scalar::make_float(0.0);

A->set_float(0, 0, 1.0f);
A->set_float(0, 2, 2.0f);
A->set_float(1, 1, 3.0f);
A->set_float(1, 3, 4.0f);
A->set_float(2, 0, 5.0f);
A->set_float(2, 2, 6.0f);

B->set_float(0, 0, 7.0f);
B->set_float(0, 1, 1.0f);
B->set_float(1, 1, 1.0f);
B->set_float(2, 1, 1.0f);
B->set_float(3, 1, 1.0f);

spla::exec_mxm(R, A, B, spla::MULT_FLOAT, spla::PLUS_FLOAT, init);

spla::ref_ptr<spla::MemView> vi, vj, vv;
ASSERT_EQ(R->read(vi, vj, vv), spla::Status::Ok);
EXPECT_EQ(vi->get_size() / sizeof(spla::uint), 5u);
}

SPLA_GTEST_MAIN
60 changes: 60 additions & 0 deletions tests/test_mxmT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,64 @@ TEST(mxmT_masked, perf_zero) {
}


TEST(mxmT_masked, read_after_exec) {
// Regression test: the kernel must keep the LIL `values` counter in sync
// with the actual row contents. Otherwise the subsequent conversion to COO
// (e.g. via read()) sizes the output from the stale counter and writes
// out of bounds.
spla::uint M = 3, N = 4, K = 2;

auto R = spla::Matrix::make(M, K, spla::FLOAT);
auto mask = spla::Matrix::make(M, K, spla::FLOAT);
auto A = spla::Matrix::make(M, N, spla::FLOAT);
auto B = spla::Matrix::make(K, N, spla::FLOAT);
auto init = spla::Scalar::make_float(0.0);

mask->set_float(0, 0, 1.0f);
mask->set_float(0, 1, 1.0f);
mask->set_float(2, 0, 1.0f);
mask->set_float(2, 1, 1.0f);

A->set_float(0, 0, 1.0f);
A->set_float(0, 2, 2.0f);
A->set_float(1, 1, 3.0f);
A->set_float(1, 3, 4.0f);
A->set_float(2, 0, 5.0f);
A->set_float(2, 2, 6.0f);

B->set_float(0, 0, 7.0f);
B->set_float(0, 2, 8.0f);
B->set_float(1, 1, 9.0f);
B->set_float(1, 3, 10.0f);

spla::exec_mxmT_masked(R, mask, A, B, spla::MULT_FLOAT, spla::PLUS_FLOAT, spla::GTZERO_FLOAT, init);

spla::ref_ptr<spla::MemView> vi, vj, vv;
ASSERT_EQ(R->read(vi, vj, vv), spla::Status::Ok);

// some backends keep explicit zeros in the result, so verify the entries
// instead of the exact count: (0,0) = 23, (2,0) = 83, everything else 0
const auto nnz = vi->get_size() / sizeof(spla::uint);
std::vector<spla::uint> i(nnz), j(nnz);
std::vector<float> x(nnz);
vi->read(0, nnz * sizeof(spla::uint), i.data());
vj->read(0, nnz * sizeof(spla::uint), j.data());
vv->read(0, nnz * sizeof(float), x.data());

bool found_00 = false, found_20 = false;
for (size_t k = 0; k < nnz; k++) {
if (i[k] == 0 && j[k] == 0) {
EXPECT_FLOAT_EQ(x[k], 23.f);
found_00 = true;
} else if (i[k] == 2 && j[k] == 0) {
EXPECT_FLOAT_EQ(x[k], 83.f);
found_20 = true;
} else {
EXPECT_FLOAT_EQ(x[k], 0.f);
}
}
EXPECT_TRUE(found_00);
EXPECT_TRUE(found_20);
}

SPLA_GTEST_MAIN
23 changes: 23 additions & 0 deletions tests/test_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,27 @@ TEST(vector, map) {
}
}

TEST(vector, set_fill_value_preserves_data) {
// Regression test: setting the fill value must not discard explicit entries.
// Only dense representations materialize the fill value, so they are
// invalidated; sparse representations keep their data.
auto ivec = spla::Vector::make(4, spla::INT);
ivec->set_int(0, 10);

ivec->set_fill_value(spla::Scalar::make_int(-1));

int x;
ivec->get_int(0, x);
EXPECT_EQ(x, 10);
ivec->get_int(1, x);
EXPECT_EQ(x, -1);

// data must survive a round trip through the dense representation
ivec->set_format(spla::FormatVector::CpuDense);
ivec->get_int(0, x);
EXPECT_EQ(x, 10);
ivec->get_int(1, x);
EXPECT_EQ(x, -1);
}

SPLA_GTEST_MAIN
Loading