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 2 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
36 changes: 36 additions & 0 deletions cuda/test/matrix/coo_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ TEST_F(Coo, SimpleApplyIsEquivalentToRef)
}


TEST_F(Coo, SimpleApplyDoesntOverwritePadding)
{
set_up_apply_data();
auto dresult_padded =
Vec::create(cuda, dresult->get_size(), dresult->get_stride() + 1);
dresult_padded->copy_from(dresult.get());
double padding_val{1234.0};
cuda->copy_from(cuda->get_master().get(), 1, &padding_val,
dresult_padded->get_values() + 1);

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

GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
ASSERT_EQ(cuda->copy_val_to_host(dresult_padded->get_values() + 1), 1234.0);
}


TEST_F(Coo, SimpleApplyIsEquivalentToRefUnsorted)
{
set_up_apply_data();
Expand All @@ -164,6 +182,24 @@ TEST_F(Coo, AdvancedApplyIsEquivalentToRef)
}


TEST_F(Coo, AdvancedApplyDoesntOverwritePadding)
{
set_up_apply_data();
auto dresult_padded =
Vec::create(cuda, dresult->get_size(), dresult->get_stride() + 1);
dresult_padded->copy_from(dresult.get());
double padding_val{1234.0};
cuda->copy_from(cuda->get_master().get(), 1, &padding_val,
dresult_padded->get_values() + 1);

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(cuda->copy_val_to_host(dresult_padded->get_values() + 1), 1234.0);
}


TEST_F(Coo, SimpleApplyAddIsEquivalentToRef)
{
set_up_apply_data();
Expand Down
36 changes: 36 additions & 0 deletions hip/test/matrix/coo_kernels.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ TEST_F(Coo, SimpleApplyIsEquivalentToRef)
}


TEST_F(Coo, SimpleApplyDoesntOverwritePadding)
{
set_up_apply_data();
auto dresult_padded =
Vec::create(hip, dresult->get_size(), dresult->get_stride() + 1);
dresult_padded->copy_from(dresult.get());
double padding_val{1234.0};
hip->copy_from(hip->get_master().get(), 1, &padding_val,
dresult_padded->get_values() + 1);

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

GKO_ASSERT_MTX_NEAR(dresult, expected, 1e-14);
ASSERT_EQ(hip->copy_val_to_host(dresult_padded->get_values() + 1), 1234.0);
}


TEST_F(Coo, SimpleApplyIsEquivalentToRefUnsorted)
{
set_up_apply_data();
Expand All @@ -165,6 +183,24 @@ TEST_F(Coo, AdvancedApplyIsEquivalentToRef)
}


TEST_F(Coo, AdvancedApplyDoesntOverwritePadding)
{
set_up_apply_data();
auto dresult_padded =
Vec::create(hip, dresult->get_size(), dresult->get_stride() + 1);
dresult_padded->copy_from(dresult.get());
double padding_val{1234.0};
hip->copy_from(hip->get_master().get(), 1, &padding_val,
dresult_padded->get_values() + 1);

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(hip->copy_val_to_host(dresult_padded->get_values() + 1), 1234.0);
}


TEST_F(Coo, SimpleApplyAddIsEquivalentToRef)
{
set_up_apply_data();
Expand Down
14 changes: 3 additions & 11 deletions omp/matrix/coo_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/matrix/dense.hpp>


#include "core/matrix/dense_kernels.hpp"
#include "omp/components/atomic.hpp"
#include "omp/components/format_conversion.hpp"

Expand All @@ -67,11 +68,7 @@ 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>();
}

dense::fill(exec, c, zero<ValueType>());
spmv2(exec, a, b, c);
}

Expand All @@ -86,12 +83,7 @@ void advanced_spmv(std::shared_ptr<const OmpExecutor> exec,
const matrix::Dense<ValueType> *beta,
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;
}

dense::scale(exec, beta, c);
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
10 changes: 3 additions & 7 deletions reference/matrix/coo_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/matrix/dense.hpp>


#include "core/matrix/dense_kernels.hpp"
#include "reference/components/format_conversion.hpp"


Expand All @@ -63,9 +64,7 @@ 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>();
}
dense::fill(exec, c, zero<ValueType>());
spmv2(exec, a, b, c);
}

Expand All @@ -80,10 +79,7 @@ void advanced_spmv(std::shared_ptr<const ReferenceExecutor> exec,
const matrix::Dense<ValueType> *beta,
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;
}
dense::scale(exec, beta, c);
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