Commit 8bbdc2db authored by Paul's avatar Paul
Browse files

Remove get_argument_gpu function

parent 4e44a1d8
...@@ -18,15 +18,20 @@ inline hip_ptr gpu_allocate(std::size_t sz) ...@@ -18,15 +18,20 @@ inline hip_ptr gpu_allocate(std::size_t sz)
return hip_ptr{result}; return hip_ptr{result};
} }
inline 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;
}
template <class T> template <class T>
hip_ptr write_to_gpu(const T& x) hip_ptr write_to_gpu(const T& x)
{ {
using type = typename T::value_type; using type = typename T::value_type;
auto size = x.size() * sizeof(type); auto size = x.size() * sizeof(type);
auto result = gpu_allocate(size); return write_to_gpu(x.data(), size);
// TODO: Check status
hipMemcpy(result.get(), x.data(), size, hipMemcpyHostToDevice);
return result;
} }
template <class T> template <class T>
...@@ -38,6 +43,23 @@ std::vector<T> read_from_gpu(const void* x, std::size_t sz) ...@@ -38,6 +43,23 @@ 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"; }
......
...@@ -23,14 +23,6 @@ rtg::program create_program() ...@@ -23,14 +23,6 @@ rtg::program create_program()
return p; return p;
} }
// TODO: Move to header
rtg::argument get_tensor_argument_gpu(rtg::shape s)
{
auto v = rtg::generate_tensor_data<float>(s);
auto p = rtg::share(rtg::miopen::write_to_gpu(v));
return {s, [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}
std::vector<float> cpu() std::vector<float> cpu()
{ {
std::vector<float> result; std::vector<float> result;
...@@ -48,10 +40,10 @@ std::vector<float> gpu() ...@@ -48,10 +40,10 @@ std::vector<float> gpu()
{ {
std::vector<float> result; std::vector<float> result;
auto p = create_program(); auto p = create_program();
auto x = get_tensor_argument_gpu({rtg::shape::float_type, {4, 3, 3, 3}}); auto x = rtg::miopen::to_gpu(rtg::generate_argument({rtg::shape::float_type, {4, 3, 3, 3}}));
auto w = get_tensor_argument_gpu({rtg::shape::float_type, {4, 3, 3, 3}}); auto w = rtg::miopen::to_gpu(rtg::generate_argument({rtg::shape::float_type, {4, 3, 3, 3}}));
p.compile(rtg::miopen::miopen_target{}); p.compile(rtg::miopen::miopen_target{});
auto y = get_tensor_argument_gpu(p.get_parameter_shape("output")); auto y = rtg::miopen::to_gpu(rtg::generate_argument(p.get_parameter_shape("output")));
auto handle = rtg::miopen::make_obj<rtg::miopen::miopen_handle>(&miopenCreate); auto handle = rtg::miopen::make_obj<rtg::miopen::miopen_handle>(&miopenCreate);
auto r = p.eval( auto r = p.eval(
{{"x", x}, {"w", w}, {"output", y}, {"handle", {rtg::shape::any_type, handle.get()}}}); {{"x", x}, {"w", w}, {"output", y}, {"handle", {rtg::shape::any_type, handle.get()}}});
......
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