/******************************************************************************* * * MIT License * * Copyright (C) 2021-2025 Advanced Micro Devices, Inc. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * *******************************************************************************/ #ifndef ROCWMMA_GEMM_RESOURCE_IMPL_HPP #define ROCWMMA_GEMM_RESOURCE_IMPL_HPP #include "gemm_resource.hpp" namespace rocwmma { template GemmResource::GemmResource() : HipResource() , mDeviceA(Base::template allocDevice(0)) , mDeviceB(Base::template allocDevice(0)) , mDeviceC(Base::template allocDevice(0)) , mDeviceD(Base::template allocDevice(0)) , mHostA(Base::template allocHost(0)) , mHostB(Base::template allocHost(0)) , mHostC(Base::template allocHost(0)) , mHostD(Base::template allocHost(0)) , mCurrentMatrixElements({0, 0, 0, 0}) , mCurrentAllocElements({0, 0, 0, 0}) { } template GemmResource::GemmResource(GemmResource&& rhs) : HipResource() , mDeviceA(std::move(rhs.mDeviceA)) , mDeviceB(std::move(rhs.mDeviceB)) , mDeviceC(std::move(rhs.mDeviceC)) , mDeviceD(std::move(rhs.mDeviceD)) , mHostA(std::move(rhs.mHostA)) , mHostB(std::move(rhs.mHostB)) , mHostC(std::move(rhs.mHostC)) , mHostD(std::move(rhs.mHostD)) , mCurrentMatrixElements(rhs.mCurrentMatrixElements) , mCurrentAllocElements(rhs.mCurrentAllocElements) { } template void GemmResource::copyHostToDeviceAll() { Base::copyData(mDeviceA, mHostA, std::get(mCurrentMatrixElements)); Base::copyData(mDeviceB, mHostB, std::get(mCurrentMatrixElements)); Base::copyData(mDeviceC, mHostC, std::get(mCurrentMatrixElements)); Base::copyData(mDeviceD, mHostD, std::get(mCurrentMatrixElements)); } template void GemmResource::copyDeviceToHostAll() { Base::copyData(mHostA, mDeviceA, std::get(mCurrentMatrixElements)); Base::copyData(mHostB, mDeviceB, std::get(mCurrentMatrixElements)); Base::copyData(mHostC, mDeviceC, std::get(mCurrentMatrixElements)); Base::copyData(mHostD, mDeviceD, std::get(mCurrentMatrixElements)); } template void GemmResource::resizeStorage(ProblemDims const& size) { resizeStorage( std::make_tuple(std::get(size) * std::get(size), // elements MatrixA = M * K std::get(size) * std::get(size), // elements MatrixB = K * N std::get(size) * std::get(size), // elements MatrixC = M * N std::get(size) * std::get(size))); // elements MatrixD = M * N) } template void GemmResource::resizeStorage(MatrixElements const& newMatrixElements) { auto conditionalReallocDeviceHostPair = [](auto& devicePtr, auto& hostPtr, int64_t& currentAllocElements, int64_t newAllocElements) { // Only realloc if required (e.g. current allocation won't fit new sizes) if(currentAllocElements < newAllocElements) { Base::reallocDeviceHostPair(devicePtr, hostPtr, newAllocElements); currentAllocElements = newAllocElements; } }; conditionalReallocDeviceHostPair(mDeviceA, mHostA, std::get(mCurrentAllocElements), std::get(newMatrixElements)); conditionalReallocDeviceHostPair(mDeviceB, mHostB, std::get(mCurrentAllocElements), std::get(newMatrixElements)); conditionalReallocDeviceHostPair(mDeviceC, mHostC, std::get(mCurrentAllocElements), std::get(newMatrixElements)); conditionalReallocDeviceHostPair(mDeviceD, mHostD, std::get(mCurrentAllocElements), std::get(newMatrixElements)); // Always update the current matrix element count mCurrentMatrixElements = newMatrixElements; } template void GemmResource::reset() { Base::reallocDeviceHostPair(mDeviceA, mHostA, 0); Base::reallocDeviceHostPair(mDeviceB, mHostB, 0); Base::reallocDeviceHostPair(mDeviceC, mHostC, 0); Base::reallocDeviceHostPair(mDeviceD, mHostD, 0); mCurrentAllocElements = {0, 0, 0, 0}; mCurrentMatrixElements = {0, 0, 0, 0}; } template auto GemmResource::hostA() -> HostPtrT& { return mHostA; } template auto GemmResource::hostB() -> HostPtrT& { return mHostB; } template auto GemmResource::hostC() -> HostPtrT& { return mHostC; } template auto GemmResource::hostD() -> HostPtrT& { return mHostD; } template auto GemmResource::deviceA() -> DevicePtrT& { return mDeviceA; } template auto GemmResource::deviceB() -> DevicePtrT& { return mDeviceB; } template auto GemmResource::deviceC() -> DevicePtrT& { return mDeviceC; } template auto GemmResource::deviceD() -> DevicePtrT& { return mDeviceD; } } // namespace rocwmma #endif // ROCWMMA_GEMM_RESOURCE_IMPL_HPP