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

Gemm mixed scalars #1615

Merged
merged 7 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions blas/src/KokkosBlas1_dot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ dot(const XVector& x, const YVector& y) {
using result_type =
typename KokkosBlas::Impl::DotAccumulatingScalar<dot_type>::type;
using RVector_Internal =
Kokkos::View<dot_type, typename XVector_Internal::array_layout,
Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
Kokkos::View<dot_type, default_layout, Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
using RVector_Result =
Kokkos::View<result_type, typename XVector_Internal::array_layout,
Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
Kokkos::View<result_type, default_layout, Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged>>;

result_type result{};
RVector_Result R = RVector_Result(&result);
Expand Down
16 changes: 7 additions & 9 deletions blas/src/KokkosBlas1_nrm1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,17 @@ nrm1(const XVector& x) {
static_assert(XVector::rank == 1,
"KokkosBlas::nrm1: "
"Both Vector inputs must have rank 1.");
typedef typename Kokkos::Details::InnerProductSpaceTraits<
typename XVector::non_const_value_type>::mag_type mag_type;
using mag_type = typename Kokkos::Details::InnerProductSpaceTraits<
typename XVector::non_const_value_type>::mag_type;

typedef Kokkos::View<
using XVector_Internal = Kokkos::View<
typename XVector::const_value_type*,
typename KokkosKernels::Impl::GetUnifiedLayout<XVector>::array_layout,
typename XVector::device_type, Kokkos::MemoryTraits<Kokkos::Unmanaged> >
XVector_Internal;
typename XVector::device_type, Kokkos::MemoryTraits<Kokkos::Unmanaged> >;

typedef Kokkos::View<mag_type, typename XVector_Internal::array_layout,
Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged> >
RVector_Internal;
using RVector_Internal =
Kokkos::View<mag_type, default_layout, Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged> >;

mag_type result;
RVector_Internal R = RVector_Internal(&result);
Expand Down
3 changes: 1 addition & 2 deletions blas/src/KokkosBlas1_nrm2_squared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ nrm2_squared(const XVector& x) {
typename XVector::device_type, Kokkos::MemoryTraits<Kokkos::Unmanaged> >
XVector_Internal;

typedef Kokkos::View<mag_type, typename XVector_Internal::array_layout,
Kokkos::HostSpace,
typedef Kokkos::View<mag_type, default_layout, Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged> >
RVector_Internal;

Expand Down
3 changes: 1 addition & 2 deletions blas/src/KokkosBlas1_nrm2w_squared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ nrm2w_squared(const XVector& x, const XVector& w) {
typename XVector::device_type, Kokkos::MemoryTraits<Kokkos::Unmanaged> >
XVector_Internal;

typedef Kokkos::View<mag_type, typename XVector_Internal::array_layout,
Kokkos::HostSpace,
typedef Kokkos::View<mag_type, default_layout, Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged> >
RVector_Internal;

Expand Down
69 changes: 43 additions & 26 deletions blas/unit_test/Test_Blas1_axpby.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,33 @@
namespace Test {
template <class ViewTypeA, class ViewTypeB, class Device>
void impl_test_axpby(int N) {
typedef typename ViewTypeA::value_type ScalarA;
typedef typename ViewTypeB::value_type ScalarB;
using ScalarA = typename ViewTypeA::value_type;
using ScalarB = typename ViewTypeB::value_type;
using MagnitudeB = typename Kokkos::ArithTraits<ScalarB>::mag_type;

typedef Kokkos::View<
using BaseTypeA = Kokkos::View<
ScalarA * [2],
typename std::conditional<std::is_same<typename ViewTypeA::array_layout,
Kokkos::LayoutStride>::value,
Kokkos::LayoutRight, Kokkos::LayoutLeft>::type,
Device>
BaseTypeA;
typedef Kokkos::View<
Device>;
using BaseTypeB = Kokkos::View<
ScalarB * [2],
typename std::conditional<std::is_same<typename ViewTypeB::array_layout,
Kokkos::LayoutStride>::value,
Kokkos::LayoutRight, Kokkos::LayoutLeft>::type,
Device>
BaseTypeB;

ScalarA a = 3;
ScalarB b = 5;
double eps = std::is_same<ScalarA, float>::value ? 2 * 1e-5 : 1e-7;
Device>;

ScalarA a = 3;
ScalarB b = 5;
// eps should probably be based on ScalarB since that is the type
// in which the result is computed.
const MagnitudeB eps = Kokkos::ArithTraits<ScalarB>::epsilon();
const MagnitudeB max_val = 10;
const MagnitudeB max_error =
(static_cast<MagnitudeB>(Kokkos::ArithTraits<ScalarA>::abs(a)) +
Kokkos::ArithTraits<ScalarB>::abs(b)) *
max_val * eps;

BaseTypeA b_x("X", N);
BaseTypeB b_y("Y", N);
Expand All @@ -67,12 +73,12 @@ void impl_test_axpby(int N) {

{
ScalarA randStart, randEnd;
Test::getRandomBounds(10.0, randStart, randEnd);
Test::getRandomBounds(max_val, randStart, randEnd);
Kokkos::fill_random(b_x, rand_pool, randStart, randEnd);
}
{
ScalarB randStart, randEnd;
Test::getRandomBounds(10.0, randStart, randEnd);
Test::getRandomBounds(max_val, randStart, randEnd);
Kokkos::fill_random(b_y, rand_pool, randStart, randEnd);
}

Expand All @@ -85,22 +91,25 @@ void impl_test_axpby(int N) {
KokkosBlas::axpby(a, x, b, y);
Kokkos::deep_copy(h_b_y, b_y);
for (int i = 0; i < N; i++) {
EXPECT_NEAR_KK(a * h_x(i) + b * h_b_org_y(i, 0), h_y(i), eps);
EXPECT_NEAR_KK(static_cast<ScalarB>(a * h_x(i) + b * h_b_org_y(i, 0)),
h_y(i), 2 * max_error);
}

Kokkos::deep_copy(b_y, b_org_y);
// Run again with const input (c_x)
KokkosBlas::axpby(a, c_x, b, y);
Kokkos::deep_copy(h_b_y, b_y);
for (int i = 0; i < N; i++) {
EXPECT_NEAR_KK(a * h_x(i) + b * h_b_org_y(i, 0), h_y(i), eps);
EXPECT_NEAR_KK(static_cast<ScalarB>(a * h_x(i) + b * h_b_org_y(i, 0)),
h_y(i), 2 * max_error);
}
}

template <class ViewTypeA, class ViewTypeB, class Device>
void impl_test_axpby_mv(int N, int K) {
typedef typename ViewTypeA::value_type ScalarA;
typedef typename ViewTypeB::value_type ScalarB;
using ScalarA = typename ViewTypeA::value_type;
using ScalarB = typename ViewTypeB::value_type;
using MagnitudeB = typename Kokkos::ArithTraits<ScalarB>::mag_type;

typedef multivector_layout_adapter<ViewTypeA> vfA_type;
typedef multivector_layout_adapter<ViewTypeB> vfB_type;
Expand All @@ -121,6 +130,15 @@ void impl_test_axpby_mv(int N, int K) {
typename ViewTypeA::HostMirror h_x = h_vfA_type::view(h_b_x);
typename ViewTypeB::HostMirror h_y = h_vfB_type::view(h_b_y);

ScalarA a = 3;
ScalarB b = 5;
const MagnitudeB eps = Kokkos::ArithTraits<ScalarB>::epsilon();
const MagnitudeB max_val = 10;
const MagnitudeB max_error =
(static_cast<MagnitudeB>(Kokkos::ArithTraits<ScalarA>::abs(a)) +
Kokkos::ArithTraits<ScalarB>::abs(b)) *
max_val * eps;

Kokkos::Random_XorShift64_Pool<typename Device::execution_space> rand_pool(
13718);

Expand All @@ -136,26 +154,24 @@ void impl_test_axpby_mv(int N, int K) {
}

Kokkos::deep_copy(b_org_y, b_y);
auto h_b_org_y =
Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), b_org_y);
ViewTypeB org_y = vfB_type::view(b_org_y);
auto h_org_y =
Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), org_y);

Kokkos::deep_copy(h_b_x, b_x);
Kokkos::deep_copy(h_b_y, b_y);

ScalarA a = 3;
ScalarB b = 5;
typename ViewTypeA::const_type c_x = x;

double eps = std::is_same<ScalarA, float>::value ? 2 * 1e-5 : 1e-7;

Kokkos::View<ScalarB*, Kokkos::HostSpace> r("Dot::Result", K);

KokkosBlas::axpby(a, x, b, y);
Kokkos::deep_copy(h_b_y, b_y);

for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
EXPECT_NEAR_KK(a * h_x(i, j) + b * h_b_org_y(i, j), h_y(i, j), eps);
EXPECT_NEAR_KK(static_cast<ScalarB>(a * h_x(i, j) + b * h_org_y(i, j)),
h_y(i, j), 2 * max_error);
}
}

Expand All @@ -165,7 +181,8 @@ void impl_test_axpby_mv(int N, int K) {

for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
EXPECT_NEAR_KK(a * h_x(i, j) + b * h_b_org_y(i, j), h_y(i, j), eps);
EXPECT_NEAR_KK(static_cast<ScalarB>(a * h_x(i, j) + b * h_org_y(i, j)),
h_y(i, j), 2 * max_error);
}
}
}
Expand Down
65 changes: 38 additions & 27 deletions blas/unit_test/Test_Blas1_axpy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,30 @@
namespace Test {
template <class ViewTypeA, class ViewTypeB, class Device>
void impl_test_axpy(int N) {
typedef typename ViewTypeA::value_type ScalarA;
typedef typename ViewTypeB::value_type ScalarB;
using ScalarA = typename ViewTypeA::value_type;
using ScalarB = typename ViewTypeB::value_type;
using MagnitudeB = typename Kokkos::ArithTraits<ScalarB>::mag_type;

typedef Kokkos::View<
using BaseTypeA = Kokkos::View<
ScalarA * [2],
typename std::conditional<std::is_same<typename ViewTypeA::array_layout,
Kokkos::LayoutStride>::value,
Kokkos::LayoutRight, Kokkos::LayoutLeft>::type,
Device>
BaseTypeA;
typedef Kokkos::View<
Device>;
using BaseTypeB = Kokkos::View<
ScalarB * [2],
typename std::conditional<std::is_same<typename ViewTypeB::array_layout,
Kokkos::LayoutStride>::value,
Kokkos::LayoutRight, Kokkos::LayoutLeft>::type,
Device>
BaseTypeB;
Device>;

using MagnitudeA = typename Kokkos::ArithTraits<ScalarA>::mag_type;

ScalarA a = 3;
double eps = std::is_same<MagnitudeA, float>::value ? 2e-5 : 1e-7;
ScalarA a = 3;
const MagnitudeB max_val = 10;
const MagnitudeB eps = Kokkos::ArithTraits<ScalarB>::epsilon();
const MagnitudeB max_error =
(static_cast<MagnitudeB>(Kokkos::ArithTraits<ScalarA>::abs(a)) * max_val +
max_val) *
eps;

BaseTypeA b_x("X", N);
BaseTypeB b_y("Y", N);
Expand All @@ -66,12 +68,12 @@ void impl_test_axpy(int N) {

{
ScalarA randStart, randEnd;
Test::getRandomBounds(10.0, randStart, randEnd);
Test::getRandomBounds(max_val, randStart, randEnd);
Kokkos::fill_random(x, rand_pool, randStart, randEnd);
}
{
ScalarB randStart, randEnd;
Test::getRandomBounds(10.0, randStart, randEnd);
Test::getRandomBounds(max_val, randStart, randEnd);
Kokkos::fill_random(y, rand_pool, randStart, randEnd);
}

Expand All @@ -86,7 +88,7 @@ void impl_test_axpy(int N) {

for (int i = 0; i < N; i++) {
ScalarB expected = a * h_x(i) + h_b_org_y(i, 0);
EXPECT_NEAR_KK(expected, h_y(i), eps);
EXPECT_NEAR_KK(expected, h_y(i), 2 * max_error);
}

// reset y to orig, and run again with const-valued x
Expand All @@ -95,14 +97,15 @@ void impl_test_axpy(int N) {
Kokkos::deep_copy(h_b_y, b_y);
for (int i = 0; i < N; i++) {
ScalarB expected = a * h_x(i) + h_b_org_y(i, 0);
EXPECT_NEAR_KK(expected, h_y(i), eps);
EXPECT_NEAR_KK(expected, h_y(i), 2 * max_error);
}
}

template <class ViewTypeA, class ViewTypeB, class Device>
void impl_test_axpy_mv(int N, int K) {
typedef typename ViewTypeA::value_type ScalarA;
typedef typename ViewTypeB::value_type ScalarB;
using ScalarA = typename ViewTypeA::value_type;
using ScalarB = typename ViewTypeB::value_type;
using MagnitudeB = typename Kokkos::ArithTraits<ScalarB>::mag_type;

typedef multivector_layout_adapter<ViewTypeA> vfA_type;
typedef multivector_layout_adapter<ViewTypeB> vfB_type;
Expand All @@ -123,37 +126,44 @@ void impl_test_axpy_mv(int N, int K) {
typename ViewTypeA::HostMirror h_x = h_vfA_type::view(h_b_x);
typename ViewTypeB::HostMirror h_y = h_vfB_type::view(h_b_y);

ScalarA a = 3;
const MagnitudeB eps = Kokkos::ArithTraits<ScalarB>::epsilon();
const MagnitudeB max_val = 10;
const MagnitudeB max_error =
(static_cast<MagnitudeB>(Kokkos::ArithTraits<ScalarA>::abs(a)) * max_val +
max_val) *
eps;

Kokkos::Random_XorShift64_Pool<typename Device::execution_space> rand_pool(
13718);

{
ScalarA randStart, randEnd;
Test::getRandomBounds(10.0, randStart, randEnd);
Test::getRandomBounds(max_val, randStart, randEnd);
Kokkos::fill_random(b_x, rand_pool, randStart, randEnd);
}
{
ScalarB randStart, randEnd;
Test::getRandomBounds(10.0, randStart, randEnd);
Test::getRandomBounds(max_val, randStart, randEnd);
Kokkos::fill_random(b_y, rand_pool, randStart, randEnd);
}

Kokkos::deep_copy(b_org_y, b_y);
auto h_b_org_y =
Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), b_org_y);
ViewTypeB org_y = vfB_type::view(b_org_y);
auto h_org_y =
Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), org_y);

Kokkos::deep_copy(h_b_x, b_x);
Kokkos::deep_copy(h_b_y, b_y);

ScalarA a = 3;
typename ViewTypeA::const_type c_x = x;

double eps = std::is_same<ScalarA, float>::value ? 2 * 1e-5 : 1e-7;

KokkosBlas::axpy(a, x, y);
Kokkos::deep_copy(h_b_y, b_y);
for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
EXPECT_NEAR_KK(a * h_x(i, j) + h_b_org_y(i, j), h_y(i, j), eps);
EXPECT_NEAR_KK(static_cast<ScalarB>(a * h_x(i, j) + h_org_y(i, j)),
h_y(i, j), 2 * max_error);
}
}

Expand All @@ -162,7 +172,8 @@ void impl_test_axpy_mv(int N, int K) {
Kokkos::deep_copy(h_b_y, b_y);
for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
EXPECT_NEAR_KK(a * h_x(i, j) + h_b_org_y(i, j), h_y(i, j), eps);
EXPECT_NEAR_KK(static_cast<ScalarB>(a * h_x(i, j) + h_org_y(i, j)),
h_y(i, j), 2 * max_error);
}
}
}
Expand Down
Loading