5#ifndef GKO_PUBLIC_CORE_BASE_PRECISION_DISPATCH_HPP_
6#define GKO_PUBLIC_CORE_BASE_PRECISION_DISPATCH_HPP_
9#include <ginkgo/config.hpp>
10#include <ginkgo/core/base/math.hpp>
11#include <ginkgo/core/base/temporary_conversion.hpp>
12#include <ginkgo/core/distributed/vector.hpp>
13#include <ginkgo/core/matrix/dense.hpp>
43template <
typename ValueType,
typename Ptr>
44detail::temporary_conversion<std::conditional_t<
49 using Pointee = detail::pointee<Ptr>;
54 using MaybeConstDense =
55 std::conditional_t<std::is_const<Pointee>::value,
const Dense, Dense>;
56 auto result = detail::temporary_conversion<
57 MaybeConstDense>::template create<NextDense, NextNextDense>(
matrix);
79template <
typename ValueType,
typename Function,
typename... Args>
95template <
typename ValueType,
typename Function>
102 auto complex_to_real =
105 if (complex_to_real) {
112 fn(
dynamic_cast<const Dense*
>(dense_in->create_real_view().get()),
113 dynamic_cast<Dense*
>(dense_out->create_real_view().get()));
129template <
typename ValueType,
typename Function>
137 auto complex_to_real =
140 if (complex_to_real) {
148 fn(dense_alpha.get(),
149 dynamic_cast<const Dense*
>(dense_in->create_real_view().get()),
150 dynamic_cast<Dense*
>(dense_out->create_real_view().get()));
166template <
typename ValueType,
typename Function>
175 auto complex_to_real =
178 if (complex_to_real) {
187 fn(dense_alpha.get(),
188 dynamic_cast<const Dense*
>(dense_in->create_real_view().get()),
190 dynamic_cast<Dense*
>(dense_out->create_real_view().get()));
226template <
typename ValueType,
typename Function>
229#ifdef GINKGO_MIXED_PRECISION
233 auto dispatch_out_vector = [&](
auto dense_in) {
234 if (
auto dense_out =
dynamic_cast<fst_type*
>(out)) {
235 fn(dense_in, dense_out);
236 }
else if (
auto dense_out =
dynamic_cast<snd_type*
>(out)) {
237 fn(dense_in, dense_out);
238 }
else if (
auto dense_out =
dynamic_cast<trd_type*
>(out)) {
239 fn(dense_in, dense_out);
241 GKO_NOT_SUPPORTED(out);
244 if (
auto dense_in =
dynamic_cast<const fst_type*
>(in)) {
245 dispatch_out_vector(dense_in);
246 }
else if (
auto dense_in =
dynamic_cast<const snd_type*
>(in)) {
247 dispatch_out_vector(dense_in);
248 }
else if (
auto dense_in =
dynamic_cast<const trd_type*
>(in)) {
249 dispatch_out_vector(dense_in);
251 GKO_NOT_SUPPORTED(in);
268template <
typename ValueType,
typename Function,
269 std::enable_if_t<is_complex<ValueType>()>* =
nullptr>
273#ifdef GINKGO_MIXED_PRECISION
281template <
typename ValueType,
typename Function,
282 std::enable_if_t<!is_complex<ValueType>()>* =
nullptr>
286#ifdef GINKGO_MIXED_PRECISION
287 if (!
dynamic_cast<const ConvertibleTo<matrix::Dense<>
>*>(in)) {
289 [&fn](
auto dense_in,
auto dense_out) {
290 fn(dense_in->create_real_view().get(),
291 dense_out->create_real_view().get());
303namespace experimental {
337template <
typename ValueType>
342 gko::detail::temporary_conversion<Vector<ValueType>>::template create<
345 GKO_NOT_SUPPORTED(
matrix);
354template <
typename ValueType>
355gko::detail::temporary_conversion<const Vector<ValueType>>
358 auto result = gko::detail::temporary_conversion<const Vector<ValueType>>::
359 template create<Vector<next_precision_base<ValueType>>>(
matrix);
361 GKO_NOT_SUPPORTED(
matrix);
381template <
typename ValueType,
typename Function,
typename... Args>
384 if constexpr (std::is_same_v<remove_complex<ValueType>,
half>) {
385 GKO_NOT_SUPPORTED(
nullptr);
401template <
typename ValueType,
typename Function>
404 if constexpr (std::is_same_v<remove_complex<ValueType>,
half>) {
405 GKO_NOT_SUPPORTED(
nullptr);
407 auto complex_to_real = !(
411 if (complex_to_real) {
422 fn(
dynamic_cast<const Vector*
>(dense_in->create_real_view().get()),
423 dynamic_cast<Vector*
>(dense_out->create_real_view().get()));
434template <
typename ValueType,
typename Function>
438 if constexpr (std::is_same_v<remove_complex<ValueType>,
half>) {
439 GKO_NOT_SUPPORTED(
nullptr);
441 auto complex_to_real = !(
445 if (complex_to_real) {
457 fn(dense_alpha.get(),
458 dynamic_cast<const Vector*
>(dense_in->create_real_view().get()),
459 dynamic_cast<Vector*
>(dense_out->create_real_view().get()));
472template <
typename ValueType,
typename Function>
477 if constexpr (std::is_same_v<remove_complex<ValueType>,
half>) {
478 GKO_NOT_SUPPORTED(
nullptr);
480 auto complex_to_real = !(
484 if (complex_to_real) {
497 fn(dense_alpha.get(),
498 dynamic_cast<const Vector*
>(dense_in->create_real_view().get()),
500 dynamic_cast<Vector*
>(dense_out->create_real_view().get()));
527template <
typename ValueType,
typename Function>
528void precision_dispatch_real_complex_distributed(Function fn,
const LinOp* in,
544template <
typename ValueType,
typename Function>
545void precision_dispatch_real_complex_distributed(Function fn,
547 const LinOp* in, LinOp* out)
549 if (
dynamic_cast<const experimental::distributed::DistributedBase*
>(in)) {
562template <
typename ValueType,
typename Function>
563void precision_dispatch_real_complex_distributed(Function fn,
566 const LinOp* beta, LinOp* out)
568 if (
dynamic_cast<const experimental::distributed::DistributedBase*
>(in)) {
570 fn, alpha, in, beta, out);
592template <
typename ValueType,
typename Function,
typename... Args>
593void precision_dispatch_real_complex_distributed(Function fn, Args*... args)
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:470
Definition lin_op.hpp:117
A base class for distributed objects.
Definition base.hpp:32
Vector is a format which explicitly stores (multiple) distributed column vectors in a dense storage f...
Definition vector.hpp:71
A class providing basic support for half precision floating point types.
Definition half.hpp:286
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:117
The distributed namespace.
Definition precision_dispatch.hpp:309
gko::detail::temporary_conversion< Vector< ValueType > > make_temporary_conversion(LinOp *matrix)
Convert the given LinOp from experimental::distributed::Vector<...> to experimental::distributed::Vec...
Definition precision_dispatch.hpp:338
void precision_dispatch_real_complex(Function fn, const LinOp *in, LinOp *out)
Calls the given function with the given LinOps temporarily converted to experimental::distributed::Ve...
Definition precision_dispatch.hpp:402
void precision_dispatch(Function fn, Args *... linops)
Calls the given function with each given argument LinOp temporarily converted into experimental::dist...
Definition precision_dispatch.hpp:382
The matrix namespace.
Definition dense_cache.hpp:15
The Ginkgo namespace.
Definition abstract_factory.hpp:20
void mixed_precision_dispatch(Function fn, const LinOp *in, LinOp *out)
Calls the given function with each given argument LinOp converted into matrix::Dense<ValueType> as pa...
Definition precision_dispatch.hpp:227
void mixed_precision_dispatch_real_complex(Function fn, const LinOp *in, LinOp *out)
Calls the given function with the given LinOps cast to their dynamic type matrix::Dense<ValueType>* a...
Definition precision_dispatch.hpp:270
void precision_dispatch(Function fn, Args *... linops)
Calls the given function with each given argument LinOp temporarily converted into matrix::Dense<Valu...
Definition precision_dispatch.hpp:80
detail::temporary_conversion< std::conditional_t< std::is_const< detail::pointee< Ptr > >::value, const matrix::Dense< ValueType >, matrix::Dense< ValueType > > > make_temporary_conversion(Ptr &&matrix)
Convert the given LinOp from matrix::Dense<...> to matrix::Dense<ValueType>.
Definition precision_dispatch.hpp:47
void precision_dispatch_real_complex(Function fn, const LinOp *in, LinOp *out)
Calls the given function with the given LinOps temporarily converted to matrix::Dense<ValueType>* as ...
Definition precision_dispatch.hpp:96
constexpr bool is_complex()
Checks if T is a complex type.
Definition math.hpp:221