diff --git a/src/core/tdecoration.hpp b/src/core/tdecoration.hpp index fecbf7e90..52aba9787 100644 --- a/src/core/tdecoration.hpp +++ b/src/core/tdecoration.hpp @@ -82,6 +82,7 @@ namespace spla { [[nodiscard]] bool is_valid(F format) const { return is_valid_i(static_cast(format)); } void validate(F format) { m_is_valid.set(static_cast(format), true); } void invalidate() { m_is_valid.reset(); } + void invalidate(F format) { m_is_valid.reset(static_cast(format)); } void set_fill_value(T value) { m_fill_value = value; } T get_fill_value() const { return m_fill_value; } diff --git a/src/core/tmatrix.hpp b/src/core/tmatrix.hpp index cd36e72b5..52c2f6b4e 100644 --- a/src/core/tmatrix.hpp +++ b/src/core/tmatrix.hpp @@ -130,8 +130,9 @@ namespace spla { template Status TMatrix::set_fill_value(const ref_ptr& 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::value) m_storage.set_fill_value(value->as_int()); if constexpr (std::is_same::value) m_storage.set_fill_value(value->as_uint()); if constexpr (std::is_same::value) m_storage.set_fill_value(value->as_float()); diff --git a/src/core/tvector.hpp b/src/core/tvector.hpp index af3e5c71f..ab0e674b3 100644 --- a/src/core/tvector.hpp +++ b/src/core/tvector.hpp @@ -130,7 +130,17 @@ namespace spla { template Status TVector::set_fill_value(const ref_ptr& 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::value) m_storage.set_fill_value(value->as_int()); if constexpr (std::is_same::value) m_storage.set_fill_value(value->as_uint()); diff --git a/src/cpu/cpu_format_coo.hpp b/src/cpu/cpu_format_coo.hpp index 96080cba0..cb5c15e0a 100644 --- a/src/cpu/cpu_format_coo.hpp +++ b/src/cpu/cpu_format_coo.hpp @@ -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 diff --git a/src/cpu/cpu_mxm.hpp b/src/cpu/cpu_mxm.hpp index d310817ba..97408c18c 100644 --- a/src/cpu/cpu_mxm.hpp +++ b/src/cpu/cpu_mxm.hpp @@ -84,6 +84,8 @@ namespace spla { std::vector 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]; @@ -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; } }; diff --git a/src/cpu/cpu_mxmT_masked.hpp b/src/cpu/cpu_mxmT_masked.hpp index 162bfd631..e57abdfab 100644 --- a/src/cpu/cpu_mxmT_masked.hpp +++ b/src/cpu/cpu_mxmT_masked.hpp @@ -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]; @@ -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; } }; diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index 14ecc4e17..1fcb0f016 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -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 diff --git a/tests/test_mxm.cpp b/tests/test_mxm.cpp index b727975ab..e2bbea776 100644 --- a/tests/test_mxm.cpp +++ b/tests/test_mxm.cpp @@ -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 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 diff --git a/tests/test_mxmT.cpp b/tests/test_mxmT.cpp index 2fa385494..82c682e5a 100644 --- a/tests/test_mxmT.cpp +++ b/tests/test_mxmT.cpp @@ -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 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 i(nnz), j(nnz); + std::vector 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 diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp index 4bd310b43..49bbf7920 100644 --- a/tests/test_vector.cpp +++ b/tests/test_vector.cpp @@ -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