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

Fix Coo out of bounds access for strided vectors #807

Merged
merged 3 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 10 additions & 7 deletions omp/matrix/coo_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ void spmv(std::shared_ptr<const OmpExecutor> exec,
const matrix::Coo<ValueType, IndexType> *a,
const matrix::Dense<ValueType> *b, matrix::Dense<ValueType> *c)
{
#pragma omp parallel for
for (size_type i = 0; i < c->get_num_stored_elements(); i++) {
c->at(i) = zero<ValueType>();
#pragma omp parallel for collapse(2)
for (size_type i = 0; i < c->get_size()[0]; i++) {
for (size_type j = 0; j < c->get_size()[1]; j++) {
c->at(i, j) = zero<ValueType>();
}
}

spmv2(exec, a, b, c);
}

Expand All @@ -87,9 +88,11 @@ void advanced_spmv(std::shared_ptr<const OmpExecutor> exec,
matrix::Dense<ValueType> *c)
{
auto beta_val = beta->at(0, 0);
#pragma omp parallel for
for (size_type i = 0; i < c->get_num_stored_elements(); i++) {
c->at(i) *= beta_val;
#pragma omp parallel for collapse(2)
for (size_type i = 0; i < c->get_size()[0]; i++) {
for (size_type j = 0; j < c->get_size()[1]; j++) {
c->at(i, j) *= beta_val;
}
}

advanced_spmv2(exec, alpha, a, b, c);
Expand Down
32 changes: 32 additions & 0 deletions omp/test/matrix/coo_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ TEST_F(Coo, SimpleApplyIsEquivalentToRef)
}


TEST_F(Coo, SimpleApplyDoesntOverwritePadding)
{
set_up_apply_data();
auto dresult_padded =
Vec::create(omp, dresult->get_size(), dresult->get_stride() + 1);
dresult_padded->copy_from(dresult.get());
dresult_padded->get_values()[1] = 1234.0;

mtx->apply(y.get(), expected.get());
dmtx->apply(dy.get(), dresult.get());

GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
ASSERT_EQ(dresult_padded->get_values()[1], 1234.0);
}


TEST_F(Coo, AdvancedApplyIsEquivalentToRef)
{
set_up_apply_data();
Expand All @@ -168,6 +184,22 @@ TEST_F(Coo, AdvancedApplyIsEquivalentToRef)
}


TEST_F(Coo, AdvancedApplyDoesntOverwritePadding)
{
set_up_apply_data();
auto dresult_padded =
Vec::create(omp, dresult->get_size(), dresult->get_stride() + 1);
dresult_padded->copy_from(dresult.get());
dresult_padded->get_values()[1] = 1234.0;

mtx->apply(alpha.get(), y.get(), beta.get(), expected.get());
dmtx->apply(dalpha.get(), dy.get(), dbeta.get(), dresult.get());

GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
ASSERT_EQ(dresult_padded->get_values()[1], 1234.0);
}


TEST_F(Coo, SimpleApplyAddIsEquivalentToRef)
{
set_up_apply_data();
Expand Down
12 changes: 8 additions & 4 deletions reference/matrix/coo_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ void spmv(std::shared_ptr<const ReferenceExecutor> exec,
const matrix::Coo<ValueType, IndexType> *a,
const matrix::Dense<ValueType> *b, matrix::Dense<ValueType> *c)
{
for (size_type i = 0; i < c->get_num_stored_elements(); i++) {
c->at(i) = zero<ValueType>();
for (size_type i = 0; i < c->get_size()[0]; i++) {
for (size_type j = 0; j < c->get_size()[1]; j++) {
c->at(i, j) = zero<ValueType>();
}
}
spmv2(exec, a, b, c);
}
Expand All @@ -81,8 +83,10 @@ void advanced_spmv(std::shared_ptr<const ReferenceExecutor> exec,
matrix::Dense<ValueType> *c)
{
auto beta_val = beta->at(0, 0);
for (size_type i = 0; i < c->get_num_stored_elements(); i++) {
c->at(i) *= beta_val;
for (size_type i = 0; i < c->get_size()[0]; i++) {
for (size_type j = 0; j < c->get_size()[1]; j++) {
c->at(i, j) *= beta_val;
}
}
advanced_spmv2(exec, alpha, a, b, c);
}
Expand Down
34 changes: 34 additions & 0 deletions reference/test/matrix/coo_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,21 @@ TYPED_TEST(Coo, AppliesToDenseVector)
}


TYPED_TEST(Coo, ApplyToStridedVectorKeepsPadding)
{
using Vec = typename TestFixture::Vec;
using T = typename TestFixture::value_type;
auto x = gko::initialize<Vec>({2.0, 1.0, 4.0}, this->exec);
auto y = Vec::create(this->exec, gko::dim<2>{2, 1}, 2);
y->get_values()[1] = 1234;

this->mtx->apply(x.get(), y.get());

GKO_ASSERT_MTX_NEAR(y, l({13.0, 5.0}), 0.0);
ASSERT_EQ(y->get_values()[1], T{1234});
}


TYPED_TEST(Coo, AppliesToMixedDenseVector)
{
using MixedVec = typename TestFixture::MixedVec;
Expand Down Expand Up @@ -432,6 +447,25 @@ TYPED_TEST(Coo, AppliesLinearCombinationToDenseVector)
}


TYPED_TEST(Coo, ApplyLinearCombinationToStridedVectorKeepsPadding)
{
using Vec = typename TestFixture::Vec;
using T = typename TestFixture::value_type;
auto alpha = gko::initialize<Vec>({-1.0}, this->exec);
auto beta = gko::initialize<Vec>({2.0}, this->exec);
auto x = gko::initialize<Vec>({2.0, 1.0, 4.0}, this->exec);
auto y = Vec::create(this->exec, gko::dim<2>{2, 1}, 2);
y->get_values()[1] = 1234;
y->at(0, 0) = 1.0;
y->at(1, 0) = 2.0;

this->mtx->apply(alpha.get(), x.get(), beta.get(), y.get());

GKO_ASSERT_MTX_NEAR(y, l({-11.0, -1.0}), 0.0);
ASSERT_EQ(y->get_values()[1], T{1234});
}


TYPED_TEST(Coo, AppliesLinearCombinationToMixedDenseVector)
{
using MixedVec = typename TestFixture::MixedVec;
Expand Down