Commit a7a08134 authored by Paul's avatar Paul
Browse files

Move hip functions to cpp file

parent 33298b0b
...@@ -7,6 +7,7 @@ if(NOT TARGET MIOpen) ...@@ -7,6 +7,7 @@ if(NOT TARGET MIOpen)
endif() endif()
add_library(rtg_miopen add_library(rtg_miopen
hip.cpp
miopen_target.cpp miopen_target.cpp
) )
rocm_clang_tidy_check(rtg_miopen) rocm_clang_tidy_check(rtg_miopen)
......
#include <rtg/miopen/hip.hpp>
namespace rtg {
namespace miopen {
hip_ptr gpu_allocate(std::size_t sz)
{
void* result;
// TODO: Check status
hipMalloc(&result, sz);
return hip_ptr{result};
}
hip_ptr write_to_gpu(const void* x, std::size_t sz)
{
auto result = gpu_allocate(sz);
// TODO: Check status
hipMemcpy(result.get(), x, sz, hipMemcpyHostToDevice);
return result;
}
rtg::argument to_gpu(rtg::argument arg)
{
auto p = share(write_to_gpu(arg.data(), arg.get_shape().bytes()));
return {arg.get_shape(), [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}
rtg::argument from_gpu(rtg::argument arg)
{
rtg::argument result;
arg.visit([&](auto x) {
using type = typename decltype(x)::value_type;
auto v = read_from_gpu<type>(arg.data(), x.get_shape().bytes() / sizeof(type));
result = {x.get_shape(), [v]() mutable { return reinterpret_cast<char*>(v.data()); }};
});
return result;
}
} // namespace miopen
} // namespace rtg
...@@ -2,29 +2,24 @@ ...@@ -2,29 +2,24 @@
#define RTG_GUARD_RTGLIB_HIP_HPP #define RTG_GUARD_RTGLIB_HIP_HPP
#include <rtg/manage_ptr.hpp> #include <rtg/manage_ptr.hpp>
#include <rtg/argument.hpp>
#include <rtg/operators.hpp>
#include <miopen/miopen.h> #include <miopen/miopen.h>
#include <vector>
namespace rtg { namespace rtg {
namespace miopen { namespace miopen {
using hip_ptr = RTG_MANAGE_PTR(void, hipFree); using hip_ptr = RTG_MANAGE_PTR(void, hipFree);
inline hip_ptr gpu_allocate(std::size_t sz) hip_ptr gpu_allocate(std::size_t sz);
{
void* result;
// TODO: Check status
hipMalloc(&result, sz);
return hip_ptr{result};
}
inline hip_ptr write_to_gpu(const void* x, std::size_t sz) hip_ptr write_to_gpu(const void* x, std::size_t sz);
{
auto result = gpu_allocate(sz); rtg::argument to_gpu(rtg::argument arg);
// TODO: Check status
hipMemcpy(result.get(), x, sz, hipMemcpyHostToDevice); rtg::argument from_gpu(rtg::argument arg);
return result;
}
template <class T> template <class T>
hip_ptr write_to_gpu(const T& x) hip_ptr write_to_gpu(const T& x)
...@@ -43,23 +38,6 @@ std::vector<T> read_from_gpu(const void* x, std::size_t sz) ...@@ -43,23 +38,6 @@ std::vector<T> read_from_gpu(const void* x, std::size_t sz)
return result; return result;
} }
inline rtg::argument to_gpu(rtg::argument arg)
{
auto p = share(write_to_gpu(arg.data(), arg.get_shape().bytes()));
return {arg.get_shape(), [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}
inline rtg::argument from_gpu(rtg::argument arg)
{
rtg::argument result;
arg.visit([&](auto x) {
using type = typename decltype(x)::value_type;
auto v = read_from_gpu<type>(arg.data(), x.get_shape().bytes() / sizeof(type));
result = {x.get_shape(), [v]() mutable { return reinterpret_cast<char*>(v.data()); }};
});
return result;
}
struct hip_allocate struct hip_allocate
{ {
std::string name() const { return "hip::allocate"; } std::string name() const { return "hip::allocate"; }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment