| 1 |
//@HEADER |
| 2 |
// ************************************************************************ |
| 3 |
// |
| 4 |
// Kokkos v. 4.0 |
| 5 |
// Copyright (2022) National Technology & Engineering |
| 6 |
// Solutions of Sandia, LLC (NTESS). |
| 7 |
// |
| 8 |
// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 |
// the U.S. Government retains certain rights in this software. |
| 10 |
// |
| 11 |
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
// See https://kokkos.org/LICENSE for license information. |
| 13 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 |
// |
| 15 |
//@HEADER |
| 16 |
#ifndef KOKKOSBATCHED_HADAMARDPRODUCT_HPP |
| 17 |
#define KOKKOSBATCHED_HADAMARDPRODUCT_HPP |
| 18 |
|
| 19 |
/// \author Kim Liegeois ([email protected]) |
| 20 |
|
| 21 |
#include "KokkosBatched_Util.hpp" |
| 22 |
#include "KokkosBatched_Vector.hpp" |
| 23 |
|
| 24 |
namespace KokkosBatched { |
| 25 |
|
| 26 |
/// \brief Serial Batched Hadamard Product: |
| 27 |
/// v_ij <- x_ij * y_ij for all i = 1, ..., n and j = 1, ..., N |
| 28 |
/// where: |
| 29 |
/// * n is the number of rows, |
| 30 |
/// * N is the number of vectors. |
| 31 |
/// |
| 32 |
/// \tparam XViewType: Input type for X, needs to be a 2D view |
| 33 |
/// \tparam YViewType: Input type for Y, needs to be a 2D view |
| 34 |
/// \tparam VViewType: Input type for V, needs to be a 2D view |
| 35 |
/// |
| 36 |
/// \param X [in]: Input vector X, a rank 2 view |
| 37 |
/// \param Y [in]: Input vector Y, a rank 2 view |
| 38 |
/// \param V [out]: Output vector V, a rank 2 view |
| 39 |
/// |
| 40 |
/// No nested parallel_for is used inside of the function. |
| 41 |
/// |
| 42 |
|
| 43 |
struct SerialHadamardProduct { |
| 44 |
template <typename XViewType, typename YViewType, typename VViewType> |
| 45 |
KOKKOS_INLINE_FUNCTION static int invoke(const XViewType &X, const YViewType &Y, const VViewType &V); |
| 46 |
}; |
| 47 |
|
| 48 |
/// \brief Team Batched Hadamard Product: |
| 49 |
/// v_ij <- x_ij * y_ij for all i = 1, ..., n and j = 1, ..., N |
| 50 |
/// where: |
| 51 |
/// * n is the number of rows, |
| 52 |
/// * N is the number of vectors. |
| 53 |
/// |
| 54 |
/// \tparam XViewType: Input type for X, needs to be a 2D view |
| 55 |
/// \tparam YViewType: Input type for Y, needs to be a 2D view |
| 56 |
/// \tparam VViewType: Input type for V, needs to be a 2D view |
| 57 |
/// |
| 58 |
/// \param member [in]: TeamPolicy member |
| 59 |
/// \param X [in]: Input vector X, a rank 2 view |
| 60 |
/// \param Y [in]: Input vector Y, a rank 2 view |
| 61 |
/// \param V [out]: Output vector V, a rank 2 view |
| 62 |
/// |
| 63 |
/// A nested parallel_for with TeamThreadRange is used. |
| 64 |
/// |
| 65 |
|
| 66 |
template <typename MemberType> |
| 67 |
struct TeamHadamardProduct { |
| 68 |
template <typename XViewType, typename YViewType, typename VViewType> |
| 69 |
KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const XViewType &X, const YViewType &Y, |
| 70 |
const VViewType &V); |
| 71 |
}; |
| 72 |
|
| 73 |
/// \brief TeamVector Batched Hadamard Product: |
| 74 |
/// v_ij <- x_ij * y_ij for all i = 1, ..., n and j = 1, ..., N |
| 75 |
/// where: |
| 76 |
/// * n is the number of rows, |
| 77 |
/// * N is the number of vectors. |
| 78 |
/// |
| 79 |
/// \tparam XViewType: Input type for X, needs to be a 2D view |
| 80 |
/// \tparam YViewType: Input type for Y, needs to be a 2D view |
| 81 |
/// \tparam VViewType: Input type for V, needs to be a 2D view |
| 82 |
/// |
| 83 |
/// \param member [in]: TeamPolicy member |
| 84 |
/// \param X [in]: Input vector X, a rank 2 view |
| 85 |
/// \param Y [in]: Input vector Y, a rank 2 view |
| 86 |
/// \param V [out]: Output vector V, a rank 2 view |
| 87 |
/// |
| 88 |
/// Two nested parallel_for with both TeamThreadRange and ThreadVectorRange |
| 89 |
/// (or one with TeamVectorRange) are used inside. |
| 90 |
/// |
| 91 |
|
| 92 |
template <typename MemberType> |
| 93 |
struct TeamVectorHadamardProduct { |
| 94 |
template <typename XViewType, typename YViewType, typename VViewType> |
| 95 |
KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const XViewType &X, const YViewType &Y, |
| 96 |
const VViewType &V); |
| 97 |
}; |
| 98 |
|
| 99 |
template <typename MemberType, typename ArgMode> |
| 100 |
struct HadamardProduct { |
| 101 |
template <typename XViewType, typename YViewType, typename VViewType> |
| 102 |
KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const XViewType &X, const YViewType &Y, |
| 103 |
const VViewType &V) { |
| 104 |
int r_val = 0; |
| 105 |
if (std::is_same<ArgMode, Mode::Serial>::value) { |
| 106 |
r_val = SerialHadamardProduct::template invoke<XViewType, YViewType, VViewType>(X, Y, V); |
| 107 |
} else if (std::is_same<ArgMode, Mode::Team>::value) { |
| 108 |
r_val = TeamHadamardProduct<MemberType>::template invoke<XViewType, YViewType, VViewType>(member, X, Y, V); |
| 109 |
} else if (std::is_same<ArgMode, Mode::TeamVector>::value) { |
| 110 |
r_val = TeamVectorHadamardProduct<MemberType>::template invoke<XViewType, YViewType, VViewType>(member, X, Y, V); |
| 111 |
} |
| 112 |
return r_val; |
| 113 |
} |
| 114 |
}; |
| 115 |
} // namespace KokkosBatched |
| 116 |
|
| 117 |
#include "KokkosBatched_HadamardProduct_Impl.hpp" |
| 118 |
|
| 119 |
#endif |