device_memory.cpp 924 Bytes
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

4
5
6
#include "ck/host_utility/hip_check_error.hpp"

#include "ck/library/utility/device_memory.hpp"
Chao Liu's avatar
Chao Liu committed
7
8
9
10
11
12

DeviceMem::DeviceMem(std::size_t mem_size) : mMemSize(mem_size)
{
    hip_check_error(hipMalloc(static_cast<void**>(&mpDeviceBuf), mMemSize));
}

13
void* DeviceMem::GetDeviceBuffer() const { return mpDeviceBuf; }
Chao Liu's avatar
Chao Liu committed
14

15
std::size_t DeviceMem::GetBufferSize() const { return mMemSize; }
Chao Liu's avatar
Chao Liu committed
16

17
void DeviceMem::ToDevice(const void* p) const
Chao Liu's avatar
Chao Liu committed
18
19
20
21
{
    hip_check_error(hipMemcpy(mpDeviceBuf, const_cast<void*>(p), mMemSize, hipMemcpyHostToDevice));
}

22
void DeviceMem::FromDevice(void* p) const
Chao Liu's avatar
Chao Liu committed
23
24
25
26
{
    hip_check_error(hipMemcpy(p, mpDeviceBuf, mMemSize, hipMemcpyDeviceToHost));
}

27
void DeviceMem::SetZero() const { hip_check_error(hipMemset(mpDeviceBuf, 0, mMemSize)); }
Chao Liu's avatar
Chao Liu committed
28
29

DeviceMem::~DeviceMem() { hip_check_error(hipFree(mpDeviceBuf)); }