Skip to content

Commit

Permalink
add results rows checks + early return
Browse files Browse the repository at this point in the history
  • Loading branch information
greole committed Jun 27, 2024
1 parent 1d813ee commit 872b7ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions reference/matrix/dense_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,15 @@ void compute_mean(std::shared_ptr<const ReferenceExecutor> exec,
const matrix::Dense<ValueType>* x,
matrix::Dense<ValueType>* result, array<char>&)
{
GKO_ASSERT_EQ(result->get_size()[0], 1);

using ValueType_nc = gko::remove_complex<ValueType>;
for (size_type j = 0; j < x->get_size()[1]; ++j) {
result->at(0, j) = zero<ValueType>();
}

if (x->get_size()[0] == 0) return;

for (size_type i = 0; i < x->get_size()[1]; ++i) {
for (size_type j = 0; j < x->get_size()[0]; ++j) {
result->at(0, i) += x->at(j, i);
Expand Down
8 changes: 8 additions & 0 deletions reference/test/matrix/dense_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,14 @@ TYPED_TEST(Dense, ComputesMean)
GKO_EXPECT_NEAR(result->at(0, 2), T{1.0}, r<T>::value * 10);
}

TYPED_TEST(Dense, ComputesMeanFailsOnZeroRowResults)
{
using Mtx = typename TestFixture::Mtx;
using T = typename TestFixture::value_type;
auto result = Mtx::create(this->exec, gko::dim<2>{0, 1});

ASSERT_THROW(this->mtx4->compute_mean(result), gko::ValueMismatch);
}

TYPED_TEST(Dense, ComputesMeanFailsOnWrongResultSize)
{
Expand Down

0 comments on commit 872b7ec

Please sign in to comment.