|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | + |
| 3 | +#if defined(TTG_ENABLE_LEVEL_ZERO) |
| 4 | +#include <oneapi/mkl.hpp> |
| 5 | +#include <sys/time.h> |
| 6 | +#endif |
| 7 | + |
| 8 | +#include "../devblas_helper.h" |
| 9 | + |
| 10 | + |
| 11 | +template <typename Blk> |
| 12 | +inline void device_gemm(Blk &C, const Blk &A, const Blk &B) { |
| 13 | + using blk_t = Blk; |
| 14 | + using T = typename blk_t::value_type; |
| 15 | + static_assert(std::is_same_v<T,double> || std::is_same_v<T,float>); |
| 16 | + static const T alpha = 1.0; |
| 17 | + static const T beta = 1.0; |
| 18 | + // make sure all memory is on the device |
| 19 | + // TODO: A and B are read-only so the owner device will be 0. How to fix? |
| 20 | + //assert(A.b.get_current_device() != 0); |
| 21 | + //assert(B.b.get_current_device() != 0); |
| 22 | + auto device = ttg::device::current_device(); |
| 23 | + assert(device.is_device()); |
| 24 | +#if defined(TTG_ENABLE_CUDA) |
| 25 | + if constexpr (std::is_same_v<T,double>) { |
| 26 | + cublasDgemm(cublas_handle(), CUBLAS_OP_N, CUBLAS_OP_N, C.extent(0), C.extent(1), A.extent(1), |
| 27 | + &alpha, A.b.current_device_ptr(), A.extent(0), B.b.current_device_ptr(), B.extent(0), &beta, |
| 28 | + C.b.current_device_ptr(), C.extent(0)); |
| 29 | + } |
| 30 | + else if constexpr (std::is_same_v<T,float>) { |
| 31 | + cublasSgemm(cublas_handle(), CUBLAS_OP_N, CUBLAS_OP_N, C.extent(0), C.extent(1), A.extent(1), |
| 32 | + &alpha, A.b.current_device_ptr(), A.extent(0), B.b.current_device_ptr(), B.extent(0), &beta, |
| 33 | + C.b.current_device_ptr(), C.extent(0)); |
| 34 | + } |
| 35 | +#elif defined(TTG_ENABLE_HIP) |
| 36 | + if constexpr (std::is_same_v<T,double>) { |
| 37 | + hipblasDgemm(hipblas_handle(), |
| 38 | + HIPBLAS_OP_N, HIPBLAS_OP_N, |
| 39 | + C.extent(0), C.extent(1), A.extent(1), &alpha, |
| 40 | + A.b.current_device_ptr(), A.extent(0), |
| 41 | + B.b.current_device_ptr(), B.extent(0), &beta, |
| 42 | + C.b.current_device_ptr(), C.extent(0)); |
| 43 | + } else if constexpr (std::is_same_v<T,float>) { |
| 44 | + hipblasSgemm(hipblas_handle(), |
| 45 | + HIPBLAS_OP_N, HIPBLAS_OP_N, |
| 46 | + C.extent(0), C.extent(1), A.extent(1), &alpha, |
| 47 | + A.b.current_device_ptr(), A.extent(0), |
| 48 | + B.b.current_device_ptr(), B.extent(0), &beta, |
| 49 | + C.b.current_device_ptr(), C.extent(0)); |
| 50 | + } |
| 51 | +#elif defined(TTG_ENABLE_LEVEL_ZERO) |
| 52 | + |
| 53 | +#if defined(DEBUG_SYNCHRONOUS) |
| 54 | + try { |
| 55 | +#endif /* DEBUG_SYNCHRONOUS */ |
| 56 | + cl::sycl::event gemm_event; |
| 57 | + gemm_event = oneapi::mkl::blas::gemm(ttg::device::current_stream(), |
| 58 | + oneapi::mkl::transpose::N, oneapi::mkl::transpose::N, |
| 59 | + C.extent(0), C.extent(1), A.extent(1), |
| 60 | + alpha, A.b.current_device_ptr(), A.extent(0), |
| 61 | + B.b.current_device_ptr(), B.extent(0), |
| 62 | + beta, C.b.current_device_ptr(), C.extent(0)); |
| 63 | +#if defined(DEBUG_SYNCHRONOUS) |
| 64 | + gemm_event.wait(); |
| 65 | + } catch (const oneapi::mkl::invalid_argument &e) { |
| 66 | + std::cerr << "OneAPI MKL BLAS GEMM throws invalid argument exception" << std::endl; |
| 67 | + } catch (const oneapi::mkl::unsupported_device &e) { |
| 68 | + std::cerr << "OneAPI MKL BLAS GEMM throws unsuported device exception" << std::endl; |
| 69 | + } catch (const oneapi::mkl::host_bad_alloc &e) { |
| 70 | + std::cerr << "OneAPI MKL BLAS GEMM throws host bad allocation exception" << std::endl; |
| 71 | + } catch (const oneapi::mkl::device_bad_alloc &e) { |
| 72 | + std::cerr << "OneAPI MKL BLAS GEMM throws device bad allocation exception" << std::endl; |
| 73 | + } catch (const oneapi::mkl::unimplemented &e) { |
| 74 | + std::cerr << "OneAPI MKL BLAS GEMM throws unimplemented exception" << std::endl; |
| 75 | + } catch (const std::exception& e) { |
| 76 | + std::cerr << "OneAPI MKL BLAS GEMM throws unexpected exception" << std::endl; |
| 77 | + } catch (...) { |
| 78 | + std::cerr << "OneAPI MKL BLAS GEMM throws unexpected exception that is also badly formatted..." << std::endl; |
| 79 | + } |
| 80 | +#endif /* DEBUG_SYNCHRONOUS */ |
| 81 | +#endif |
| 82 | +} |
0 commit comments