Skip to content

Commit

Permalink
conversion-based mixed precision SpMV & BLAS
Browse files Browse the repository at this point in the history
  • Loading branch information
upsj committed Mar 8, 2021
1 parent 61f7b7c commit 5458c5d
Show file tree
Hide file tree
Showing 25 changed files with 752 additions and 326 deletions.
30 changes: 22 additions & 8 deletions core/base/combination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/matrix/dense.hpp>


#include "core/base/precision_dispatch.hpp"


namespace gko {
namespace {

Expand Down Expand Up @@ -101,12 +104,20 @@ std::unique_ptr<LinOp> Combination<ValueType>::conj_transpose() const
template <typename ValueType>
void Combination<ValueType>::apply_impl(const LinOp *b, LinOp *x) const
{
auto converted_b = make_temporary_conversion<ValueType>(b);
auto converted_x = make_temporary_conversion<ValueType>(x);
initialize_scalars<ValueType>(this->get_executor(), cache_.zero,
cache_.one);
operators_[0]->apply(lend(coefficients_[0]), b, lend(cache_.zero), x);
for (size_type i = 1; i < operators_.size(); ++i) {
operators_[i]->apply(lend(coefficients_[i]), b, lend(cache_.one), x);
}
precision_dispatch_spmv<ValueType>(
[&](auto dense_b, auto dense_x) {
operators_[0]->apply(lend(coefficients_[0]), dense_b,
lend(cache_.zero), dense_x);
for (size_type i = 1; i < operators_.size(); ++i) {
operators_[i]->apply(lend(coefficients_[i]), dense_b,
lend(cache_.one), dense_x);
}
},
b, x);
}


Expand All @@ -118,10 +129,13 @@ void Combination<ValueType>::apply_impl(const LinOp *alpha, const LinOp *b,
cache_.intermediate_x->get_size() != x->get_size()) {
cache_.intermediate_x = x->clone();
}
this->apply_impl(b, lend(cache_.intermediate_x));
auto dense_x = as<matrix::Dense<ValueType>>(x);
dense_x->scale(beta);
dense_x->add_scaled(alpha, lend(cache_.intermediate_x));
precision_dispatch_spmv<ValueType>(
[&](auto dense_alpha, auto dense_b, auto dense_beta, auto dense_x) {
this->apply_impl(dense_b, lend(cache_.intermediate_x));
dense_x->scale(dense_beta);
dense_x->add_scaled(dense_alpha, lend(cache_.intermediate_x));
},
alpha, b, beta, x);
}


Expand Down
32 changes: 22 additions & 10 deletions core/base/composition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/matrix/dense.hpp>


#include "core/base/precision_dispatch.hpp"
#include "core/components/fill_array.hpp"


Expand Down Expand Up @@ -164,25 +165,36 @@ std::unique_ptr<LinOp> Composition<ValueType>::conj_transpose() const
template <typename ValueType>
void Composition<ValueType>::apply_impl(const LinOp *b, LinOp *x) const
{
if (operators_.size() > 1) {
operators_[0]->apply(
lend(apply_inner_operators(operators_, storage_, b)), x);
} else {
operators_[0]->apply(b, x);
}
precision_dispatch_spmv<ValueType>(
[&](auto dense_b, auto dense_x) {
if (operators_.size() > 1) {
operators_[0]->apply(
lend(apply_inner_operators(operators_, storage_, dense_b)),
dense_x);
} else {
operators_[0]->apply(dense_b, dense_x);
}
},
b, x);
}


template <typename ValueType>
void Composition<ValueType>::apply_impl(const LinOp *alpha, const LinOp *b,
const LinOp *beta, LinOp *x) const
{
auto converted_b = make_temporary_conversion<ValueType>(b);
auto converted_x = make_temporary_conversion<ValueType>(x);
auto converted_alpha = make_temporary_conversion<ValueType>(alpha);
auto converted_beta = make_temporary_conversion<ValueType>(beta);
if (operators_.size() > 1) {
operators_[0]->apply(
alpha, lend(apply_inner_operators(operators_, storage_, b)), beta,
x);
operators_[0]->apply(converted_alpha.get(),
lend(apply_inner_operators(operators_, storage_,
converted_b.get())),
converted_beta.get(), converted_x.get());
} else {
operators_[0]->apply(alpha, b, beta, x);
operators_[0]->apply(converted_alpha.get(), converted_b.get(),
converted_beta.get(), converted_x.get());
}
}

Expand Down
32 changes: 21 additions & 11 deletions core/base/perturbation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/matrix/dense.hpp>


#include "core/base/precision_dispatch.hpp"


namespace gko {


Expand All @@ -51,10 +54,14 @@ void Perturbation<ValueType>::apply_impl(const LinOp *b, LinOp *x) const
auto intermediate_size =
gko::dim<2>(projector_->get_size()[0], b->get_size()[1]);
cache_.allocate(exec, intermediate_size);
projector_->apply(b, lend(cache_.intermediate));
x->copy_from(b);
basis_->apply(lend(scalar_), lend(cache_.intermediate), lend(cache_.one),
x);
precision_dispatch_spmv<ValueType>(
[&](auto dense_b, auto dense_x) {
projector_->apply(dense_b, lend(cache_.intermediate));
dense_x->copy_from(dense_b);
basis_->apply(lend(scalar_), lend(cache_.intermediate),
lend(cache_.one), dense_x);
},
b, x);
}


Expand All @@ -74,13 +81,16 @@ void Perturbation<ValueType>::apply_impl(const LinOp *alpha, const LinOp *b,
auto intermediate_size =
gko::dim<2>(projector_->get_size()[0], b->get_size()[1]);
cache_.allocate(exec, intermediate_size);
projector_->apply(b, lend(cache_.intermediate));
auto vec_x = as<vec>(x);
vec_x->scale(beta);
vec_x->add_scaled(alpha, b);
alpha->apply(lend(scalar_), lend(cache_.alpha_scalar));
basis_->apply(lend(cache_.alpha_scalar), lend(cache_.intermediate),
lend(cache_.one), vec_x);
precision_dispatch_spmv<ValueType>(
[&](auto dense_alpha, auto dense_b, auto dense_beta, auto dense_x) {
projector_->apply(dense_b, lend(cache_.intermediate));
dense_x->scale(dense_beta);
dense_x->add_scaled(dense_alpha, dense_b);
alpha->apply(lend(scalar_), lend(cache_.alpha_scalar));
basis_->apply(lend(cache_.alpha_scalar), lend(cache_.intermediate),
lend(cache_.one), dense_x);
},
alpha, b, beta, x);
}


Expand Down
153 changes: 153 additions & 0 deletions core/base/precision_dispatch.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2021, the Ginkgo authors
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#ifndef GKO_CORE_BASE_PRECISION_DISPATCH_HPP_
#define GKO_CORE_BASE_PRECISION_DISPATCH_HPP_


#include <ginkgo/core/matrix/dense.hpp>


#include "core/base/temporary_conversion.hpp"


namespace gko {


template <typename ValueType>
detail::temporary_conversion<matrix::Dense<ValueType>>
make_temporary_conversion(LinOp *matrix)
{
auto result =
detail::temporary_conversion<matrix::Dense<ValueType>>::template create<
matrix::Dense<next_precision<ValueType>>>(matrix);
if (!result) {
GKO_NOT_SUPPORTED(matrix);
}
return result;
}


template <typename ValueType>
detail::temporary_conversion<const matrix::Dense<ValueType>>
make_temporary_conversion(const LinOp *matrix)
{
auto result = detail::temporary_conversion<const matrix::Dense<ValueType>>::
template create<matrix::Dense<next_precision<ValueType>>>(matrix);
if (!result) {
GKO_NOT_SUPPORTED(matrix);
}
return result;
}


template <typename ValueType, typename Function, typename... Args>
void precision_dispatch(Function fn, Args *... linops)
{
fn(make_temporary_conversion<ValueType>(linops).get()...);
}

template <typename ValueType, typename Function>
void precision_dispatch_spmv(Function fn, const LinOp *in, LinOp *out)
{
// do we need to convert complex Dense to real Dense?
auto complex_to_real =
!(is_complex<ValueType>() ||
dynamic_cast<const ConvertibleTo<matrix::Dense<>> *>(in));
if (complex_to_real) {
auto dense_in = make_temporary_conversion<to_complex<ValueType>>(in);
auto dense_out = make_temporary_conversion<to_complex<ValueType>>(out);
using Dense = matrix::Dense<ValueType>;
// These dynamic_casts are only needed to make the code compile
// If ValueType is complex, this branch will never be taken
// If ValueType is real, the cast is a no-op
fn(dynamic_cast<const Dense *>(dense_in->create_real_view().get()),
dynamic_cast<Dense *>(dense_out->create_real_view().get()));
} else {
precision_dispatch<ValueType>(fn, in, out);
}
}

template <typename ValueType, typename Function>
void precision_dispatch_spmv(Function fn, const LinOp *alpha, const LinOp *in,
LinOp *out)
{
// do we need to convert complex Dense to real Dense?
auto complex_to_real =
!(is_complex<ValueType>() ||
dynamic_cast<const ConvertibleTo<matrix::Dense<>> *>(in));
if (complex_to_real) {
auto dense_in = make_temporary_conversion<to_complex<ValueType>>(in);
auto dense_out = make_temporary_conversion<to_complex<ValueType>>(out);
auto dense_alpha = make_temporary_conversion<ValueType>(alpha);
using Dense = matrix::Dense<ValueType>;
// These dynamic_casts are only needed to make the code compile
// If ValueType is complex, this branch will never be taken
// If ValueType is real, the cast is a no-op
fn(dense_alpha.get(),
dynamic_cast<const Dense *>(dense_in->create_real_view().get()),
dynamic_cast<Dense *>(dense_out->create_real_view().get()));
} else {
precision_dispatch<ValueType>(fn, alpha, in, out);
}
}

template <typename ValueType, typename Function>
void precision_dispatch_spmv(Function fn, const LinOp *alpha, const LinOp *in,
const LinOp *beta, LinOp *out)
{
// do we need to convert complex Dense to real Dense?
auto complex_to_real =
!(is_complex<ValueType>() ||
dynamic_cast<const ConvertibleTo<matrix::Dense<>> *>(in));
if (complex_to_real) {
auto dense_in = make_temporary_conversion<to_complex<ValueType>>(in);
auto dense_out = make_temporary_conversion<to_complex<ValueType>>(out);
auto dense_alpha = make_temporary_conversion<ValueType>(alpha);
auto dense_beta = make_temporary_conversion<ValueType>(beta);
using Dense = matrix::Dense<ValueType>;
// These dynamic_casts are only needed to make the code compile
// If ValueType is complex, this branch will never be taken
// If ValueType is real, the cast is a no-op
fn(dense_alpha.get(),
dynamic_cast<const Dense *>(dense_in->create_real_view().get()),
dense_beta.get(),
dynamic_cast<Dense *>(dense_out->create_real_view().get()));
} else {
precision_dispatch<ValueType>(fn, alpha, in, beta, out);
}
}

} // namespace gko


#endif // GKO_CORE_BASE_PRECISION_DISPATCH_HPP_
Loading

0 comments on commit 5458c5d

Please sign in to comment.