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<
45 std::is_const<detail::pointee<Ptr>>::value,
const matrix::Dense<ValueType>,
46 matrix::Dense<ValueType>>>
49 using Pointee = detail::pointee<Ptr>;
52 using MaybeConstDense =
53 std::conditional_t<std::is_const<Pointee>::value,
const Dense, Dense>;
54 auto result = detail::temporary_conversion<
55 MaybeConstDense>::template create<NextDense>(matrix);
57 GKO_NOT_SUPPORTED(*matrix);
77template <
typename ValueType,
typename Function,
typename... Args>
93template <
typename ValueType,
typename Function>
100 auto complex_to_real =
103 if (complex_to_real) {
110 fn(
dynamic_cast<const Dense*
>(dense_in->create_real_view().get()),
111 dynamic_cast<Dense*
>(dense_out->create_real_view().get()));
127template <
typename ValueType,
typename Function>
135 auto complex_to_real =
138 if (complex_to_real) {
146 fn(dense_alpha.get(),
147 dynamic_cast<const Dense*
>(dense_in->create_real_view().get()),
148 dynamic_cast<Dense*
>(dense_out->create_real_view().get()));
164template <
typename ValueType,
typename Function>
173 auto complex_to_real =
176 if (complex_to_real) {
185 fn(dense_alpha.get(),
186 dynamic_cast<const Dense*
>(dense_in->create_real_view().get()),
188 dynamic_cast<Dense*
>(dense_out->create_real_view().get()));
224template <
typename ValueType,
typename Function>
227#ifdef GINKGO_MIXED_PRECISION
230 if (
auto dense_in =
dynamic_cast<const fst_type*
>(in)) {
231 if (
auto dense_out =
dynamic_cast<fst_type*
>(out)) {
232 fn(dense_in, dense_out);
233 }
else if (
auto dense_out =
dynamic_cast<snd_type*
>(out)) {
234 fn(dense_in, dense_out);
236 GKO_NOT_SUPPORTED(out);
238 }
else if (
auto dense_in =
dynamic_cast<const snd_type*
>(in)) {
239 if (
auto dense_out =
dynamic_cast<fst_type*
>(out)) {
240 fn(dense_in, dense_out);
241 }
else if (
auto dense_out =
dynamic_cast<snd_type*
>(out)) {
242 fn(dense_in, dense_out);
244 GKO_NOT_SUPPORTED(out);
247 GKO_NOT_SUPPORTED(in);
264template <
typename ValueType,
typename Function,
265 std::enable_if_t<is_complex<ValueType>()>* =
nullptr>
269#ifdef GINKGO_MIXED_PRECISION
277template <
typename ValueType,
typename Function,
278 std::enable_if_t<!is_complex<ValueType>()>* =
nullptr>
282#ifdef GINKGO_MIXED_PRECISION
283 if (!
dynamic_cast<const ConvertibleTo<matrix::Dense<>
>*>(in)) {
285 [&fn](
auto dense_in,
auto dense_out) {
286 fn(dense_in->create_real_view().get(),
287 dense_out->create_real_view().get());
299namespace experimental {
305namespace distributed {
333template <
typename ValueType>
334detail::temporary_conversion<experimental::distributed::Vector<ValueType>>
337 auto result = detail::temporary_conversion<
343 GKO_NOT_SUPPORTED(matrix);
352template <
typename ValueType>
353detail::temporary_conversion<const experimental::distributed::Vector<ValueType>>
356 auto result = detail::temporary_conversion<
362 GKO_NOT_SUPPORTED(matrix);
382template <
typename ValueType,
typename Function,
typename... Args>
398template <
typename ValueType,
typename Function>
401 auto complex_to_real = !(
405 if (complex_to_real) {
414 fn(
dynamic_cast<const Vector*
>(dense_in->create_real_view().get()),
415 dynamic_cast<Vector*
>(dense_out->create_real_view().get()));
425template <
typename ValueType,
typename Function>
429 auto complex_to_real = !(
433 if (complex_to_real) {
443 fn(dense_alpha.get(),
444 dynamic_cast<const Vector*
>(dense_in->create_real_view().get()),
445 dynamic_cast<Vector*
>(dense_out->create_real_view().get()));
457template <
typename ValueType,
typename Function>
462 auto complex_to_real = !(
466 if (complex_to_real) {
477 fn(dense_alpha.get(),
478 dynamic_cast<const Vector*
>(dense_in->create_real_view().get()),
480 dynamic_cast<Vector*
>(dense_out->create_real_view().get()));
506template <
typename ValueType,
typename Function>
507void precision_dispatch_real_complex_distributed(Function fn,
const LinOp* in,
523template <
typename ValueType,
typename Function>
524void precision_dispatch_real_complex_distributed(Function fn,
526 const LinOp* in, LinOp* out)
528 if (
dynamic_cast<const experimental::distributed::DistributedBase*
>(in)) {
541template <
typename ValueType,
typename Function>
542void precision_dispatch_real_complex_distributed(Function fn,
545 const LinOp* beta, LinOp* out)
547 if (
dynamic_cast<const experimental::distributed::DistributedBase*
>(in)) {
549 fn, alpha, in, beta, out);
570template <
typename ValueType,
typename Function,
typename... Args>
571void 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:471
Definition lin_op.hpp:118
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 dense.hpp:30
Dense is a matrix format which explicitly stores all values of the matrix.
Definition sparsity_csr.hpp:26
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:399
detail::temporary_conversion< experimental::distributed::Vector< ValueType > > make_temporary_conversion(LinOp *matrix)
Convert the given LinOp from experimental::distributed::Vector<...> to experimental::distributed::Vec...
Definition precision_dispatch.hpp:335
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:383
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:225
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:266
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:78
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:94
constexpr bool is_complex()
Checks if T is a complex type.
Definition math.hpp:287