| 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_HOSTLEVEL_GEMM_DECL_HPP |
| 17 |
#define KOKKOSBATCHED_HOSTLEVEL_GEMM_DECL_HPP |
| 18 |
|
| 19 |
// Include explicit specializations of BatchedGemm. |
| 20 |
// If ETI_ONLY is disabled, the primary template will |
| 21 |
// be inlined into each caller's invocation using non- |
| 22 |
// ETI'd template arguments. |
| 23 |
#include "KokkosBatched_HostLevel_Gemm_Spec.hpp" |
| 24 |
|
| 25 |
namespace KokkosBatched { |
| 26 |
// clang-format off |
| 27 |
/// \brief Non-blocking solve of general matrix multiply on a batch of |
| 28 |
/// uniform matrices. |
| 29 |
/// |
| 30 |
/// Note: If a TPL is selected, this interface follows the blocking |
| 31 |
/// behavior (either blocking or non-blocking) of the TPL vendor's API. |
| 32 |
/// |
| 33 |
/// Note: To leverage SIMD instructions, 4-rank views must be selected via the |
| 34 |
/// template parameters documented below. |
| 35 |
/// |
| 36 |
/// C = alpha * op(A) * op(B) + beta * C |
| 37 |
/// |
| 38 |
/// \tparam ArgTransA Specifies what op does to A: |
| 39 |
/// |
| 40 |
/// Trans::NoTranspose for non-transpose |
| 41 |
/// Trans::Transpose for transpose |
| 42 |
/// Trans::ConjTranspose for conjugate transpose |
| 43 |
/// \tparam ArgTransB Specifies what op does to B: |
| 44 |
/// |
| 45 |
/// Trans::NoTranspose for non-transpose |
| 46 |
/// Trans::Transpose for transpose |
| 47 |
/// Trans::ConjTranspose for conjugate transpose |
| 48 |
/// \tparam ArgBatchSzDim Specifies where the batch dimension is allocated in |
| 49 |
/// |
| 50 |
/// AViewType, BViewType, and CViewType: |
| 51 |
/// BatchLayout::Left Batch dimension is leftmost |
| 52 |
/// BatchLayout::Right Batch dimension is rightmost |
| 53 |
/// \tparam ScalarType Specifies the scalar type of alpha and beta |
| 54 |
/// \tparam AViewType Input matrix, as either a 3-rank Kokkos::View or a |
| 55 |
/// 4-rank Kokkos::View for SIMD operations. |
| 56 |
/// \tparam BViewType Input matrix, as either a 3-rank Kokkos::View or a |
| 57 |
/// 4-rank Kokkos::View for SIMD operations. |
| 58 |
/// \tparam CViewType Input(RHS)/Output(LHS) matrix, as either a 3-rank |
| 59 |
/// Kokkos::View or a 4-rank Kokkos::View for SIMD |
| 60 |
/// operations. |
| 61 |
/// |
| 62 |
/// \param handle [in] A handle which specifies how to invoke the batched |
| 63 |
/// gemm. |
| 64 |
/// See struct BatchedGemmHandle for details. |
| 65 |
/// \param alpha [in] Input coefficient used for multiplication with A |
| 66 |
/// \param A [in] Input matrix, as a 3-rank Kokkos::View |
| 67 |
/// |
| 68 |
/// If ArgBatchSzDim == "BatchLayout::Right", matrix A is MxKxB |
| 69 |
/// If ArgBatchSzDim == "BatchLayout::Left", matrix A is BxMxK |
| 70 |
/// \param B [in] Input matrix, as a 3-rank Kokkos::View |
| 71 |
/// |
| 72 |
/// If ArgBatchSzDim == "BatchLayout::Right", matrix B is KxNxB |
| 73 |
/// If ArgBatchSzDim == "BatchLayout::Left", matrix B is BxKxN |
| 74 |
/// \param beta [in] Input coefficient used for multiplication with C |
| 75 |
/// \param C [in/out] Input/Output matrix, as a 3-rank Kokkos::View |
| 76 |
/// |
| 77 |
/// If ArgBatchSzDim == "BatchLayout::Right", matrix C is MxNxB |
| 78 |
/// If ArgBatchSzDim == "BatchLayout::Left", matrix C is BxMxN |
| 79 |
/// \return 0 upon success, non-zero otherwise |
| 80 |
/// |
| 81 |
/// Usage Example: |
| 82 |
/// BatchedGemm<ArgTransA, ArgTransB, |
| 83 |
/// ArgBatchSzDim>(handle, alpha, A, B, beta, C); |
| 84 |
// clang-format on |
| 85 |
template <typename ArgTransA, typename ArgTransB, typename ArgBatchSzDim, typename BatchedGemmHandleType, |
| 86 |
typename ScalarType, typename AViewType, typename BViewType, typename CViewType> |
| 87 |
inline int BatchedGemm(BatchedGemmHandleType *const handle, const ScalarType alpha, const AViewType &A, |
| 88 |
const BViewType &B, const ScalarType beta, const CViewType &C) { |
| 89 |
// Minimize the number of ImplBatchedGemmWrapper instantiations, by |
| 90 |
// standardizing on particular View specializations for its template |
| 91 |
// parameters. |
| 92 |
using UnifiedAVT = Kokkos::View<typename AViewType::value_type ***, typename AViewType::array_layout, |
| 93 |
typename AViewType::device_type, Kokkos::MemoryTraits<Kokkos::Unmanaged>>; |
| 94 |
using UnifiedBVT = Kokkos::View<typename BViewType::value_type ***, typename BViewType::array_layout, |
| 95 |
typename BViewType::device_type, Kokkos::MemoryTraits<Kokkos::Unmanaged>>; |
| 96 |
using UnifiedCVT = Kokkos::View<typename CViewType::non_const_value_type ***, typename CViewType::array_layout, |
| 97 |
typename CViewType::device_type, Kokkos::MemoryTraits<Kokkos::Unmanaged>>; |
| 98 |
|
| 99 |
// Go through specialization layer in case ETI'd symbols are available. |
| 100 |
return Impl::BatchedGemmSpec<ArgTransA, ArgTransB, ArgBatchSzDim, BatchedGemmHandleType, ScalarType, UnifiedAVT, |
| 101 |
UnifiedBVT, UnifiedCVT>::run(handle, alpha, A, B, beta, C); |
| 102 |
} |
| 103 |
} // namespace KokkosBatched |
| 104 |
#endif // KOKKOSBATCHED_HOSTLEVEL_GEMM_DECL_HPP |