Skip to content

Commit

Permalink
fbcsr and its test
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Oct 25, 2024
1 parent 53f05b8 commit b7d4a15
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 38 deletions.
13 changes: 5 additions & 8 deletions core/test/utils/fb_matrix_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,15 @@ std::unique_ptr<matrix::Fbcsr<ValueType, IndexType>> generate_fbcsr_from_csr(
const IndexType* const row_ptrs = fmtx->get_const_row_ptrs();
const IndexType* const col_idxs = fmtx->get_const_col_idxs();
ValueType* const vals = fmtx->get_values();
std::uniform_real_distribution<gko::remove_complex<ValueType>>
off_diag_dist(-1.0, 1.0);
std::uniform_real_distribution<> off_diag_dist(-1.0, 1.0);

for (IndexType ibrow = 0; ibrow < nbrows; ibrow++) {
if (row_diag_dominant) {
const IndexType nrownz =
(row_ptrs[ibrow + 1] - row_ptrs[ibrow]) * block_size;

std::uniform_real_distribution<gko::remove_complex<ValueType>>
diag_dist(1.01 * nrownz, 2 * nrownz);
std::uniform_real_distribution<> diag_dist(1.01 * nrownz,
2 * nrownz);

for (IndexType ibz = row_ptrs[ibrow]; ibz < row_ptrs[ibrow + 1];
ibz++) {
Expand Down Expand Up @@ -205,13 +204,11 @@ std::unique_ptr<matrix::Fbcsr<ValueType, IndexType>> generate_random_fbcsr(
matrix::Csr<ValueType, IndexType>>(
nbrows, nbcols,
std::uniform_int_distribution<IndexType>(0, nbcols - 1),
std::normal_distribution<real_type>(0.0, 1.0),
std::move(engine), ref)
std::normal_distribution<>(0.0, 1.0), std::move(engine), ref)
: generate_random_matrix<matrix::Csr<ValueType, IndexType>>(
nbrows, nbcols,
std::uniform_int_distribution<IndexType>(0, nbcols - 1),
std::normal_distribution<real_type>(0.0, 1.0),
std::move(engine), ref);
std::normal_distribution<>(0.0, 1.0), std::move(engine), ref);
if (unsort && rand_csr_ref->is_sorted_by_column_index()) {
unsort_matrix(rand_csr_ref, engine);
}
Expand Down
6 changes: 4 additions & 2 deletions core/test/utils/value_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ template <typename ValueType, typename ValueDistribution, typename Engine>
typename std::enable_if<!is_complex_s<ValueType>::value, ValueType>::type
get_rand_value(ValueDistribution&& value_dist, Engine&& gen)
{
return value_dist(gen);
return static_cast<ValueType>(value_dist(gen));
}

/**
Expand All @@ -45,7 +45,9 @@ template <typename ValueType, typename ValueDistribution, typename Engine>
typename std::enable_if<is_complex_s<ValueType>::value, ValueType>::type
get_rand_value(ValueDistribution&& value_dist, Engine&& gen)
{
return ValueType(value_dist(gen), value_dist(gen));
using real_type = remove_complex<ValueType>;
return ValueType(static_cast<real_type>(value_dist(gen)),
static_cast<real_type>(value_dist(gen)));
}


Expand Down
56 changes: 38 additions & 18 deletions hip/test/matrix/fbcsr_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <gtest/gtest.h>

#include <ginkgo/core/base/exception.hpp>
#include <ginkgo/core/base/executor.hpp>
#include <ginkgo/core/matrix/fbcsr.hpp>

Expand Down Expand Up @@ -41,7 +42,7 @@ class Fbcsr : public HipTestFixture {

std::unique_ptr<const Mtx> rsorted_ref;

std::normal_distribution<gko::remove_complex<T>> distb;
std::normal_distribution<> distb;
std::default_random_engine engine;

value_type get_random_value()
Expand All @@ -60,7 +61,8 @@ class Fbcsr : public HipTestFixture {
}
};

TYPED_TEST_SUITE(Fbcsr, gko::test::RealValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(Fbcsr, gko::test::RealValueTypesWithHalf,
TypenameNameGenerator);


TYPED_TEST(Fbcsr, CanWriteFromMatrixOnDevice)
Expand Down Expand Up @@ -145,11 +147,15 @@ TYPED_TEST(Fbcsr, SpmvIsEquivalentToRefSorted)
this->ref, gko::dim<2>(this->rsorted_ref->get_size()[0], 1));
auto prod_hip = Dense::create(this->exec, prod_ref->get_size());

rand_hip->apply(x_hip, prod_hip);
this->rsorted_ref->apply(x_ref, prod_ref);
if (std::is_same<value_type, gko::half>::value) {
ASSERT_THROW(rand_hip->apply(x_hip, prod_hip), gko::NotImplemented);
} else {
rand_hip->apply(x_hip, prod_hip);
this->rsorted_ref->apply(x_ref, prod_ref);

const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
}
}


Expand All @@ -169,11 +175,15 @@ TYPED_TEST(Fbcsr, SpmvMultiIsEquivalentToRefSorted)
this->ref, gko::dim<2>(this->rsorted_ref->get_size()[0], 3));
auto prod_hip = Dense::create(this->exec, prod_ref->get_size());

rand_hip->apply(x_hip, prod_hip);
this->rsorted_ref->apply(x_ref, prod_ref);
if (std::is_same<value_type, gko::half>::value) {
ASSERT_THROW(rand_hip->apply(x_hip, prod_hip), gko::NotImplemented);
} else {
rand_hip->apply(x_hip, prod_hip);
this->rsorted_ref->apply(x_ref, prod_ref);

const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
}
}


Expand Down Expand Up @@ -205,11 +215,16 @@ TYPED_TEST(Fbcsr, AdvancedSpmvIsEquivalentToRefSorted)
auto beta = Dense::create(this->exec);
beta->copy_from(beta_ref);

rand_hip->apply(alpha, x_hip, beta, prod_hip);
this->rsorted_ref->apply(alpha_ref, x_ref, beta_ref, prod_ref);
if (std::is_same<value_type, gko::half>::value) {
ASSERT_THROW(rand_hip->apply(alpha, x_hip, beta, prod_hip),
gko::NotImplemented);
} else {
rand_hip->apply(alpha, x_hip, beta, prod_hip);
this->rsorted_ref->apply(alpha_ref, x_ref, beta_ref, prod_ref);

const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
}
}


Expand Down Expand Up @@ -241,11 +256,16 @@ TYPED_TEST(Fbcsr, AdvancedSpmvMultiIsEquivalentToRefSorted)
auto beta = Dense::create(this->exec);
beta->copy_from(beta_ref);

rand_hip->apply(alpha, x_hip, beta, prod_hip);
this->rsorted_ref->apply(alpha_ref, x_ref, beta_ref, prod_ref);
if (std::is_same<value_type, gko::half>::value) {
ASSERT_THROW(rand_hip->apply(alpha, x_hip, beta, prod_hip),
gko::NotImplemented);
} else {
rand_hip->apply(alpha, x_hip, beta, prod_hip);
this->rsorted_ref->apply(alpha_ref, x_ref, beta_ref, prod_ref);

const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
const double tol = r<value_type>::value;
GKO_ASSERT_MTX_NEAR(prod_ref, prod_hip, 5 * tol);
}
}


Expand Down
10 changes: 5 additions & 5 deletions reference/test/matrix/fbcsr_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ std::unique_ptr<gko::matrix::Dense<T>> get_some_vectors(
{
using RT = gko::remove_complex<T>;
std::default_random_engine engine(39);
std::normal_distribution<RT> dist(0.0, 5.0);
std::normal_distribution<> dist(0.0, 5.0);
std::uniform_int_distribution<> nnzdist(1, nrhs);
return gko::test::generate_random_matrix<gko::matrix::Dense<T>>(
nrows, nrhs, nnzdist, dist, engine, exec);
Expand Down Expand Up @@ -271,7 +271,7 @@ TYPED_TEST(Fbcsr, ConvertsToPrecision)
{
using ValueType = typename TestFixture::value_type;
using IndexType = typename TestFixture::index_type;
using OtherType = gko::next_precision<ValueType>;
using OtherType = gko::next_precision_with_half<ValueType>;
using Fbcsr = typename TestFixture::Mtx;
using OtherFbcsr = gko::matrix::Fbcsr<OtherType, IndexType>;
auto tmp = OtherFbcsr::create(this->exec);
Expand All @@ -294,7 +294,7 @@ TYPED_TEST(Fbcsr, MovesToPrecision)
{
using ValueType = typename TestFixture::value_type;
using IndexType = typename TestFixture::index_type;
using OtherType = gko::next_precision<ValueType>;
using OtherType = gko::next_precision_with_half<ValueType>;
using Fbcsr = typename TestFixture::Mtx;
using OtherFbcsr = gko::matrix::Fbcsr<OtherType, IndexType>;
auto tmp = OtherFbcsr::create(this->exec);
Expand Down Expand Up @@ -393,7 +393,7 @@ TYPED_TEST(Fbcsr, ConvertsEmptyToPrecision)
{
using ValueType = typename TestFixture::value_type;
using IndexType = typename TestFixture::index_type;
using OtherType = gko::next_precision<ValueType>;
using OtherType = gko::next_precision_with_half<ValueType>;
using Fbcsr = typename TestFixture::Mtx;
using OtherFbcsr = gko::matrix::Fbcsr<OtherType, IndexType>;
auto empty = OtherFbcsr::create(this->exec);
Expand All @@ -412,7 +412,7 @@ TYPED_TEST(Fbcsr, MovesEmptyToPrecision)
{
using ValueType = typename TestFixture::value_type;
using IndexType = typename TestFixture::index_type;
using OtherType = gko::next_precision<ValueType>;
using OtherType = gko::next_precision_with_half<ValueType>;
using Fbcsr = typename TestFixture::Mtx;
using OtherFbcsr = gko::matrix::Fbcsr<OtherType, IndexType>;
auto empty = OtherFbcsr::create(this->exec);
Expand Down
15 changes: 10 additions & 5 deletions test/matrix/fbcsr_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,23 @@ class Fbcsr : public CommonTestFixture {
void generate_sin(gko::ptr_param<Dense> x)
{
value_type* const xarr = x->get_values();
// we do not have sin for half, so we compute sin in double or
// complex<double>
using working_type = std::conditional_t<gko::is_complex<value_type>(),
std::complex<double>, double>;
for (index_type i = 0; i < x->get_size()[0] * x->get_size()[1]; i++) {
xarr[i] =
static_cast<real_type>(2.0) *
std::sin(static_cast<real_type>(i / 2.0) + get_random_value());
xarr[i] = static_cast<value_type>(
2.0 * std::sin(i / 2.0 +
static_cast<working_type>(get_random_value())));
}
}
};

#ifdef GKO_COMPILING_HIP
TYPED_TEST_SUITE(Fbcsr, gko::test::RealValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(Fbcsr, gko::test::RealValueTypesWithHalf,
TypenameNameGenerator);
#else
TYPED_TEST_SUITE(Fbcsr, gko::test::ValueTypes, TypenameNameGenerator);
TYPED_TEST_SUITE(Fbcsr, gko::test::ValueTypesWithHalf, TypenameNameGenerator);
#endif

TYPED_TEST(Fbcsr, CanWriteFromMatrixOnDevice)
Expand Down

0 comments on commit b7d4a15

Please sign in to comment.