| 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_IMPL_HPP |
| 17 |
#define KOKKOSBATCHED_HADAMARDPRODUCT_IMPL_HPP |
| 18 |
|
| 19 |
/// \author Kim Liegeois ([email protected]) |
| 20 |
|
| 21 |
#include "KokkosBatched_Util.hpp" |
| 22 |
|
| 23 |
namespace KokkosBatched { |
| 24 |
|
| 25 |
/// |
| 26 |
/// Serial Internal Impl |
| 27 |
/// ==================== |
| 28 |
struct SerialHadamardProductInternal { |
| 29 |
template <typename ScalarType, typename ValueType> |
| 30 |
KOKKOS_INLINE_FUNCTION static int invoke(const int m, const int n, const ValueType* KOKKOS_RESTRICT X, const int xs0, |
| 31 |
const int xs1, const ValueType* KOKKOS_RESTRICT Y, const int ys0, |
| 32 |
const int ys1, |
| 33 |
/* */ ValueType* KOKKOS_RESTRICT V, const int vs0, const int vs1) { |
| 34 |
for (int i = 0; i < m; ++i) |
| 35 |
for (int j = 0; j < n; ++j) V[i * vs0 + j * vs1] = X[i * xs0 + j * xs1] * Y[i * ys0 + j * ys1]; |
| 36 |
|
| 37 |
return 0; |
| 38 |
} |
| 39 |
}; |
| 40 |
|
| 41 |
/// |
| 42 |
/// Team Internal Impl |
| 43 |
/// ==================== |
| 44 |
struct TeamHadamardProductInternal { |
| 45 |
template <typename MemberType, typename ValueType, typename layout> |
| 46 |
KOKKOS_INLINE_FUNCTION static int invoke(const MemberType& member, const int m, const int n, |
| 47 |
const ValueType* KOKKOS_RESTRICT X, const int xs0, const int xs1, |
| 48 |
const ValueType* KOKKOS_RESTRICT Y, const int ys0, const int ys1, |
| 49 |
/* */ ValueType* KOKKOS_RESTRICT V, const int vs0, const int vs1) { |
| 50 |
Kokkos::parallel_for(Kokkos::TeamThreadRange(member, 0, m * n), [&](const int& iTemp) { |
| 51 |
int i, j; |
| 52 |
getIndices<int, layout>(iTemp, n, m, j, i); |
| 53 |
V[i * vs0 + j * vs1] = X[i * xs0 + j * xs1] * Y[i * ys0 + j * ys1]; |
| 54 |
}); |
| 55 |
// member.team_barrier(); |
| 56 |
return 0; |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
/// |
| 61 |
/// TeamVector Internal Impl |
| 62 |
/// ======================== |
| 63 |
struct TeamVectorHadamardProductInternal { |
| 64 |
template <typename MemberType, typename ValueType, typename layout> |
| 65 |
KOKKOS_INLINE_FUNCTION static int invoke(const MemberType& member, const int m, const int n, |
| 66 |
const ValueType* KOKKOS_RESTRICT X, const int xs0, const int xs1, |
| 67 |
const ValueType* KOKKOS_RESTRICT Y, const int ys0, const int ys1, |
| 68 |
/* */ ValueType* KOKKOS_RESTRICT V, const int vs0, const int vs1) { |
| 69 |
Kokkos::parallel_for(Kokkos::TeamVectorRange(member, 0, m * n), [&](const int& iTemp) { |
| 70 |
int i, j; |
| 71 |
getIndices<int, layout>(iTemp, n, m, j, i); |
| 72 |
V[i * vs0 + j * vs1] = X[i * xs0 + j * xs1] * Y[i * ys0 + j * ys1]; |
| 73 |
}); |
| 74 |
// member.team_barrier(); |
| 75 |
return 0; |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
/// |
| 80 |
/// Serial Impl |
| 81 |
/// =========== |
| 82 |
template <typename XViewType, typename YViewType, typename VViewType> |
| 83 |
KOKKOS_INLINE_FUNCTION int SerialHadamardProduct::invoke(const XViewType& X, const YViewType& Y, const VViewType& V) { |
| 84 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 85 |
static_assert(Kokkos::is_view<XViewType>::value, "KokkosBatched::HadamardProduct: XViewType is not a Kokkos::View."); |
| 86 |
static_assert(Kokkos::is_view<YViewType>::value, "KokkosBatched::HadamardProduct: YViewType is not a Kokkos::View."); |
| 87 |
static_assert(Kokkos::is_view<VViewType>::value, "KokkosBatched::HadamardProduct: VViewType is not a Kokkos::View."); |
| 88 |
static_assert(XViewType::rank == 2, "KokkosBatched::HadamardProduct: XViewType must have rank 2."); |
| 89 |
static_assert(YViewType::rank == 2, "KokkosBatched::HadamardProduct: YViewType must have rank 2."); |
| 90 |
static_assert(VViewType::rank == 2, "KokkosBatched::HadamardProduct: VViewType must have rank 2."); |
| 91 |
|
| 92 |
// Check compatibility of dimensions at run time. |
| 93 |
if (X.extent(0) != Y.extent(0) || X.extent(1) != Y.extent(1)) { |
| 94 |
Kokkos::printf( |
| 95 |
"KokkosBatched::HadamardProduct: Dimensions of X and Y do not match: " |
| 96 |
"X: %d x %d, " |
| 97 |
"Y: %d x %d\n", |
| 98 |
(int)X.extent(0), (int)X.extent(1), (int)Y.extent(0), (int)Y.extent(1)); |
| 99 |
return 1; |
| 100 |
} |
| 101 |
if (X.extent(0) != V.extent(0) || X.extent(1) != V.extent(1)) { |
| 102 |
Kokkos::printf( |
| 103 |
"KokkosBatched::HadamardProduct: Dimensions of X and V do not match: " |
| 104 |
"X: %d x %d, " |
| 105 |
"V: %d x %d\n", |
| 106 |
(int)X.extent(0), (int)X.extent(1), (int)V.extent(0), (int)V.extent(1)); |
| 107 |
return 1; |
| 108 |
} |
| 109 |
#endif |
| 110 |
|
| 111 |
return SerialHadamardProductInternal::template invoke<typename XViewType::non_const_value_type>( |
| 112 |
X.extent(0), X.extent(1), X.data(), X.stride_0(), X.stride_1(), Y.data(), Y.stride_0(), Y.stride_1(), V.data(), |
| 113 |
V.stride_0(), V.stride_1()); |
| 114 |
} |
| 115 |
|
| 116 |
/// |
| 117 |
/// Team Impl |
| 118 |
/// ========= |
| 119 |
|
| 120 |
template <typename MemberType> |
| 121 |
template <typename XViewType, typename YViewType, typename VViewType> |
| 122 |
KOKKOS_INLINE_FUNCTION int TeamHadamardProduct<MemberType>::invoke(const MemberType& member, const XViewType& X, |
| 123 |
const YViewType& Y, const VViewType& V) { |
| 124 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 125 |
static_assert(Kokkos::is_view<XViewType>::value, "KokkosBatched::HadamardProduct: XViewType is not a Kokkos::View."); |
| 126 |
static_assert(Kokkos::is_view<YViewType>::value, "KokkosBatched::HadamardProduct: YViewType is not a Kokkos::View."); |
| 127 |
static_assert(Kokkos::is_view<VViewType>::value, "KokkosBatched::HadamardProduct: VViewType is not a Kokkos::View."); |
| 128 |
static_assert(XViewType::rank == 2, "KokkosBatched::HadamardProduct: XViewType must have rank 2."); |
| 129 |
static_assert(YViewType::rank == 2, "KokkosBatched::HadamardProduct: YViewType must have rank 2."); |
| 130 |
static_assert(VViewType::rank == 2, "KokkosBatched::HadamardProduct: VViewType must have rank 2."); |
| 131 |
|
| 132 |
// Check compatibility of dimensions at run time. |
| 133 |
if (X.extent(0) != Y.extent(0) || X.extent(1) != Y.extent(1)) { |
| 134 |
Kokkos::printf( |
| 135 |
"KokkosBatched::HadamardProduct: Dimensions of X and Y do not match: " |
| 136 |
"X: %d x %d, " |
| 137 |
"Y: %d x %d\n", |
| 138 |
(int)X.extent(0), (int)X.extent(1), (int)Y.extent(0), (int)Y.extent(1)); |
| 139 |
return 1; |
| 140 |
} |
| 141 |
if (X.extent(0) != V.extent(0) || X.extent(1) != V.extent(1)) { |
| 142 |
Kokkos::printf( |
| 143 |
"KokkosBatched::HadamardProduct: Dimensions of X and V do not match: " |
| 144 |
"X: %d x %d, " |
| 145 |
"V: %d x %d\n", |
| 146 |
(int)X.extent(0), (int)X.extent(1), (int)V.extent(0), (int)V.extent(1)); |
| 147 |
return 1; |
| 148 |
} |
| 149 |
#endif |
| 150 |
|
| 151 |
return TeamHadamardProductInternal::template invoke<MemberType, typename XViewType::non_const_value_type, |
| 152 |
typename XViewType::array_layout>( |
| 153 |
member, X.extent(0), X.extent(1), X.data(), X.stride_0(), X.stride_1(), Y.data(), Y.stride_0(), Y.stride_1(), |
| 154 |
V.data(), V.stride_0(), V.stride_1()); |
| 155 |
} |
| 156 |
|
| 157 |
/// |
| 158 |
/// TeamVector Impl |
| 159 |
/// =============== |
| 160 |
|
| 161 |
template <typename MemberType> |
| 162 |
template <typename XViewType, typename YViewType, typename VViewType> |
| 163 |
KOKKOS_INLINE_FUNCTION int TeamVectorHadamardProduct<MemberType>::invoke(const MemberType& member, const XViewType& X, |
| 164 |
const YViewType& Y, const VViewType& V) { |
| 165 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 166 |
static_assert(Kokkos::is_view<XViewType>::value, "KokkosBatched::HadamardProduct: XViewType is not a Kokkos::View."); |
| 167 |
static_assert(Kokkos::is_view<YViewType>::value, "KokkosBatched::HadamardProduct: YViewType is not a Kokkos::View."); |
| 168 |
static_assert(Kokkos::is_view<VViewType>::value, "KokkosBatched::HadamardProduct: VViewType is not a Kokkos::View."); |
| 169 |
static_assert(XViewType::rank == 2, "KokkosBatched::HadamardProduct: XViewType must have rank 2."); |
| 170 |
static_assert(YViewType::rank == 2, "KokkosBatched::HadamardProduct: YViewType must have rank 2."); |
| 171 |
static_assert(VViewType::rank == 2, "KokkosBatched::HadamardProduct: VViewType must have rank 2."); |
| 172 |
|
| 173 |
// Check compatibility of dimensions at run time. |
| 174 |
if (X.extent(0) != Y.extent(0) || X.extent(1) != Y.extent(1)) { |
| 175 |
Kokkos::printf( |
| 176 |
"KokkosBatched::HadamardProduct: Dimensions of X and Y do not match: " |
| 177 |
"X: %d x %d, " |
| 178 |
"Y: %d x %d\n", |
| 179 |
(int)X.extent(0), (int)X.extent(1), (int)Y.extent(0), (int)Y.extent(1)); |
| 180 |
return 1; |
| 181 |
} |
| 182 |
if (X.extent(0) != V.extent(0) || X.extent(1) != V.extent(1)) { |
| 183 |
Kokkos::printf( |
| 184 |
"KokkosBatched::HadamardProduct: Dimensions of X and V do not match: " |
| 185 |
"X: %d x %d, " |
| 186 |
"V: %d x %d\n", |
| 187 |
(int)X.extent(0), (int)X.extent(1), (int)V.extent(0), (int)V.extent(1)); |
| 188 |
return 1; |
| 189 |
} |
| 190 |
#endif |
| 191 |
|
| 192 |
return TeamVectorHadamardProductInternal::invoke<MemberType, typename XViewType::non_const_value_type, |
| 193 |
typename XViewType::array_layout>( |
| 194 |
member, X.extent(0), X.extent(1), X.data(), X.stride_0(), X.stride_1(), Y.data(), Y.stride_0(), Y.stride_1(), |
| 195 |
V.data(), V.stride_0(), V.stride_1()); |
| 196 |
} |
| 197 |
|
| 198 |
} // namespace KokkosBatched |
| 199 |
|
| 200 |
#endif |