//@HEADER // ************************************************************************ // // Kokkos v. 4.0 // Copyright (2022) National Technology & Engineering // Solutions of Sandia, LLC (NTESS). // // Under the terms of Contract DE-NA0003525 with NTESS, // the U.S. Government retains certain rights in this software. // // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. // See https://kokkos.org/LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //@HEADER #ifndef KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_HPP_ #define KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_HPP_ namespace KokkosBlas { namespace Impl { namespace { template inline void nrm1_print_specialization() { #ifdef KOKKOSKERNELS_ENABLE_CHECK_SPECIALIZATION printf("KokkosBlas1::nrm1<> TPL Blas specialization for < %s , %s >\n", typeid(RV).name(), typeid(XV).name()); #endif } } // namespace } // namespace Impl } // namespace KokkosBlas // Generic Host side BLAS (could be MKL or whatever) #ifdef KOKKOSKERNELS_ENABLE_TPL_BLAS #include "KokkosBlas_Host_tpl.hpp" namespace KokkosBlas { namespace Impl { #define KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(SCALAR, LAYOUT, EXECSPACE, MEMSPACE) \ template <> \ struct Nrm1::mag_type, LAYOUT, Kokkos::HostSpace, \ Kokkos::MemoryTraits>, \ Kokkos::View, \ Kokkos::MemoryTraits>, \ 1, true, \ nrm1_eti_spec_avail::mag_type, LAYOUT, \ Kokkos::HostSpace, Kokkos::MemoryTraits>, \ Kokkos::View, \ Kokkos::MemoryTraits>>::value> { \ using mag_type = typename Kokkos::ArithTraits::mag_type; \ using RV = Kokkos::View>; \ using XV = Kokkos::View, \ Kokkos::MemoryTraits>; \ using size_type = typename XV::size_type; \ \ static void nrm1(const EXECSPACE& space, RV& R, const XV& X) { \ Kokkos::Profiling::pushRegion("KokkosBlas::nrm1[TPL_BLAS," #SCALAR "]"); \ const size_type numElems = X.extent(0); \ if (numElems < static_cast(INT_MAX)) { \ nrm1_print_specialization(); \ int N = numElems; \ int one = 1; \ if constexpr (Kokkos::ArithTraits::is_complex) { \ R() = HostBlas>::asum(N, reinterpret_cast*>(X.data()), \ one); \ } else { \ R() = HostBlas::asum(N, X.data(), one); \ } \ } else { \ Nrm1::value>::nrm1(space, R, X); \ } \ Kokkos::Profiling::popRegion(); \ } \ }; #if defined(KOKKOS_ENABLE_SERIAL) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(float, Kokkos::LayoutLeft, Kokkos::Serial, Kokkos::HostSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(double, Kokkos::LayoutLeft, Kokkos::Serial, Kokkos::HostSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::Serial, Kokkos::HostSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::Serial, Kokkos::HostSpace) #endif #if defined(KOKKOS_ENABLE_OPENMP) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(float, Kokkos::LayoutLeft, Kokkos::OpenMP, Kokkos::HostSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(double, Kokkos::LayoutLeft, Kokkos::OpenMP, Kokkos::HostSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::OpenMP, Kokkos::HostSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::OpenMP, Kokkos::HostSpace) #endif #if defined(KOKKOS_ENABLE_THREADS) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(float, Kokkos::LayoutLeft, Kokkos::Threads, Kokkos::HostSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(double, Kokkos::LayoutLeft, Kokkos::Threads, Kokkos::HostSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::Threads, Kokkos::HostSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_BLAS(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::Threads, Kokkos::HostSpace) #endif } // namespace Impl } // namespace KokkosBlas #endif // cuBLAS #ifdef KOKKOSKERNELS_ENABLE_TPL_CUBLAS #include namespace KokkosBlas { namespace Impl { template void cublasAsumWrapper(const ExecutionSpace& space, RViewType& R, const XViewType& X) { using XScalar = typename XViewType::non_const_value_type; nrm1_print_specialization(); const int N = static_cast(X.extent(0)); constexpr int one = 1; KokkosBlas::Impl::CudaBlasSingleton& s = KokkosBlas::Impl::CudaBlasSingleton::singleton(); KOKKOSBLAS_IMPL_CUBLAS_SAFE_CALL(cublasSetStream(s.handle, space.cuda_stream())); if constexpr (std::is_same_v) { KOKKOSBLAS_IMPL_CUBLAS_SAFE_CALL(cublasSasum(s.handle, N, X.data(), one, R.data())); } if constexpr (std::is_same_v) { KOKKOSBLAS_IMPL_CUBLAS_SAFE_CALL(cublasDasum(s.handle, N, X.data(), one, R.data())); } if constexpr (std::is_same_v>) { KOKKOSBLAS_IMPL_CUBLAS_SAFE_CALL( cublasScasum(s.handle, N, reinterpret_cast(X.data()), one, R.data())); } if constexpr (std::is_same_v>) { KOKKOSBLAS_IMPL_CUBLAS_SAFE_CALL( cublasDzasum(s.handle, N, reinterpret_cast(X.data()), one, R.data())); } KOKKOSBLAS_IMPL_CUBLAS_SAFE_CALL(cublasSetStream(s.handle, NULL)); } #define KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_CUBLAS(SCALAR, LAYOUT, MEMSPACE) \ template <> \ struct Nrm1::mag_type, LAYOUT, Kokkos::HostSpace, \ Kokkos::MemoryTraits>, \ Kokkos::View, \ Kokkos::MemoryTraits>, \ 1, true, \ nrm1_eti_spec_avail::mag_type, LAYOUT, \ Kokkos::HostSpace, Kokkos::MemoryTraits>, \ Kokkos::View, \ Kokkos::MemoryTraits>>::value> { \ using execution_space = Kokkos::Cuda; \ using RV = Kokkos::View::mag_type, LAYOUT, Kokkos::HostSpace, \ Kokkos::MemoryTraits>; \ using XV = Kokkos::View, \ Kokkos::MemoryTraits>; \ using size_type = typename XV::size_type; \ \ static void nrm1(const execution_space& space, RV& R, const XV& X) { \ Kokkos::Profiling::pushRegion("KokkosBlas::nrm1[TPL_CUBLAS," #SCALAR "]"); \ const size_type numElems = X.extent(0); \ if (numElems < static_cast(INT_MAX)) { \ cublasAsumWrapper(space, R, X); \ } else { \ Nrm1::value>::nrm1(space, R, X); \ } \ Kokkos::Profiling::popRegion(); \ } \ }; KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_CUBLAS(float, Kokkos::LayoutLeft, Kokkos::CudaSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_CUBLAS(double, Kokkos::LayoutLeft, Kokkos::CudaSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_CUBLAS(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::CudaSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_CUBLAS(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::CudaSpace) #if defined(KOKKOSKERNELS_INST_MEMSPACE_CUDAUVMSPACE) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_CUBLAS(float, Kokkos::LayoutLeft, Kokkos::CudaUVMSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_CUBLAS(double, Kokkos::LayoutLeft, Kokkos::CudaUVMSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_CUBLAS(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::CudaUVMSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_CUBLAS(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::CudaUVMSpace) #endif } // namespace Impl } // namespace KokkosBlas #endif // KOKKOSKERNELS_ENABLE_TPL_CUBLAS // rocBLAS #ifdef KOKKOSKERNELS_ENABLE_TPL_ROCBLAS #include namespace KokkosBlas { namespace Impl { template void rocblasAsumWrapper(const ExecutionSpace& space, RViewType& R, const XViewType& X) { using XScalar = typename XViewType::non_const_value_type; nrm1_print_specialization(); const int N = static_cast(X.extent(0)); constexpr int one = 1; KokkosBlas::Impl::RocBlasSingleton& s = KokkosBlas::Impl::RocBlasSingleton::singleton(); KOKKOSBLAS_IMPL_ROCBLAS_SAFE_CALL(rocblas_set_stream(s.handle, space.hip_stream())); if constexpr (std::is_same_v) { KOKKOSBLAS_IMPL_ROCBLAS_SAFE_CALL(rocblas_sasum(s.handle, N, X.data(), one, R.data())); } if constexpr (std::is_same_v) { KOKKOSBLAS_IMPL_ROCBLAS_SAFE_CALL(rocblas_dasum(s.handle, N, X.data(), one, R.data())); } if constexpr (std::is_same_v>) { KOKKOSBLAS_IMPL_ROCBLAS_SAFE_CALL( rocblas_scasum(s.handle, N, reinterpret_cast(X.data()), one, R.data())); } if constexpr (std::is_same_v>) { KOKKOSBLAS_IMPL_ROCBLAS_SAFE_CALL( rocblas_dzasum(s.handle, N, reinterpret_cast(X.data()), one, R.data())); } KOKKOSBLAS_IMPL_ROCBLAS_SAFE_CALL(rocblas_set_stream(s.handle, NULL)); } #define KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_ROCBLAS(SCALAR, LAYOUT, MEMSPACE) \ template <> \ struct Nrm1::mag_type, LAYOUT, Kokkos::HostSpace, \ Kokkos::MemoryTraits>, \ Kokkos::View, \ Kokkos::MemoryTraits>, \ 1, true, \ nrm1_eti_spec_avail::mag_type, LAYOUT, \ Kokkos::HostSpace, Kokkos::MemoryTraits>, \ Kokkos::View, \ Kokkos::MemoryTraits>>::value> { \ using RV = Kokkos::View::mag_type, LAYOUT, Kokkos::HostSpace, \ Kokkos::MemoryTraits>; \ using XV = Kokkos::View, \ Kokkos::MemoryTraits>; \ using size_type = typename XV::size_type; \ \ static void nrm1(const Kokkos::HIP& space, RV& R, const XV& X) { \ Kokkos::Profiling::pushRegion("KokkosBlas::nrm1[TPL_ROCBLAS," #SCALAR "]"); \ const size_type numElems = X.extent(0); \ if (numElems < static_cast(INT_MAX)) { \ rocblasAsumWrapper(space, R, X); \ } else { \ Nrm1::value>::nrm1(space, R, X); \ } \ Kokkos::Profiling::popRegion(); \ } \ }; KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_ROCBLAS(float, Kokkos::LayoutLeft, Kokkos::HIPSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_ROCBLAS(double, Kokkos::LayoutLeft, Kokkos::HIPSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_ROCBLAS(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::HIPSpace) KOKKOSBLAS1_NRM1_TPL_SPEC_DECL_ROCBLAS(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::HIPSpace) } // namespace Impl } // namespace KokkosBlas #endif // KOKKOSKERNELS_ENABLE_TPL_ROCBLAS // oneMKL #ifdef KOKKOSKERNELS_ENABLE_TPL_MKL #if defined(KOKKOS_ENABLE_SYCL) #include #include namespace KokkosBlas { namespace Impl { template void onemklAsumWrapper(const ExecutionSpace& space, RViewType& R, const XViewType& X) { using XScalar = typename XViewType::non_const_value_type; using KAT_X = Kokkos::ArithTraits; using layout_t = typename XViewType::array_layout; const std::int64_t N = static_cast(X.extent(0)); // Create temp view on device to store the result Kokkos::View::mag_type, typename XViewType::memory_space> res( "sycl asum result"); // Decide to call row_major or column_major function if constexpr (std::is_same_v) { if constexpr (KAT_X::is_complex) { oneapi::mkl::blas::row_major::asum(space.sycl_queue(), N, reinterpret_cast*>(X.data()), 1, res.data()); } else { oneapi::mkl::blas::row_major::asum(space.sycl_queue(), N, X.data(), 1, res.data()); } } else { if constexpr (KAT_X::is_complex) { oneapi::mkl::blas::column_major::asum(space.sycl_queue(), N, reinterpret_cast*>(X.data()), 1, res.data()); } else { oneapi::mkl::blas::column_major::asum(space.sycl_queue(), X.extent_int(0), X.data(), 1, res.data()); } } // Bring result back to host Kokkos::deep_copy(space, R, res); } #define KOKKOSBLAS1_NRM1_ONEMKL(SCALAR, LAYOUT, MEMSPACE) \ template <> \ struct Nrm1< \ Kokkos::Experimental::SYCL, \ Kokkos::View::mag_type, LAYOUT, Kokkos::HostSpace, \ Kokkos::MemoryTraits>, \ Kokkos::View, \ Kokkos::MemoryTraits>, \ 1, true, \ nrm1_eti_spec_avail::mag_type, LAYOUT, Kokkos::HostSpace, \ Kokkos::MemoryTraits>, \ Kokkos::View, \ Kokkos::MemoryTraits>>::value> { \ using execution_space = Kokkos::Experimental::SYCL; \ using RV = Kokkos::View::mag_type, LAYOUT, Kokkos::HostSpace, \ Kokkos::MemoryTraits>; \ using XV = Kokkos::View, \ Kokkos::MemoryTraits>; \ using size_type = typename XV::size_type; \ \ static void nrm1(const execution_space& space, RV& R, const XV& X) { \ Kokkos::Profiling::pushRegion("KokkosBlas::nrm1[TPL_ONEMKL," #SCALAR "]"); \ const size_type numElems = X.extent(0); \ if (numElems < static_cast(INT_MAX)) { \ onemklAsumWrapper(space, R, X); \ } else { \ Nrm1::value>::nrm1( \ space, R, X); \ } \ Kokkos::Profiling::popRegion(); \ } \ }; KOKKOSBLAS1_NRM1_ONEMKL(float, Kokkos::LayoutLeft, Kokkos::Experimental::SYCLDeviceUSMSpace) KOKKOSBLAS1_NRM1_ONEMKL(double, Kokkos::LayoutLeft, Kokkos::Experimental::SYCLDeviceUSMSpace) KOKKOSBLAS1_NRM1_ONEMKL(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::Experimental::SYCLDeviceUSMSpace) KOKKOSBLAS1_NRM1_ONEMKL(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::Experimental::SYCLDeviceUSMSpace) #if defined(KOKKOSKERNELS_INST_MEMSPACE_SYCLSHAREDSPACE) KOKKOSBLAS1_NRM1_ONEMKL(float, Kokkos::LayoutLeft, Kokkos::Experimental::SYCLSharedUSMSpace) KOKKOSBLAS1_NRM1_ONEMKL(double, Kokkos::LayoutLeft, Kokkos::Experimental::SYCLSharedUSMSpace) KOKKOSBLAS1_NRM1_ONEMKL(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::Experimental::SYCLSharedUSMSpace) KOKKOSBLAS1_NRM1_ONEMKL(Kokkos::complex, Kokkos::LayoutLeft, Kokkos::Experimental::SYCLSharedUSMSpace) #endif } // namespace Impl } // namespace KokkosBlas #endif // KOKKOS_ENABLE_SYCL #endif // KOKKOSKERNELS_ENABLE_TPL_MKL #endif