Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEA] Add method to copy device_buffer back to host memory #219

Merged
merged 37 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
76a18c4
Add `copy_to_host` method to `rmm::device_buffer`
jakirkham Dec 20, 2019
695aabf
Add `copy_to_host` to Cython binding interface
jakirkham Dec 20, 2019
88051e4
Define `tobytes` method in `DeviceBuffer`
jakirkham Dec 20, 2019
3d29c34
Implement `tobytes` using `copy_to_host`
jakirkham Dec 20, 2019
4d0b5fc
Fix incorrect variable name
jakirkham Dec 20, 2019
105b6c0
Use attributes directly in `copy_to_host`
jakirkham Dec 20, 2019
aaf38a3
Check that `host_buffer` is not `nullptr`
jakirkham Dec 20, 2019
edbd381
Add CUDA stream and use `cudaMemcpyAsync`
jakirkham Dec 20, 2019
f30a301
Fix incorrect variable name
jakirkham Dec 20, 2019
8edffa1
Convert `copy_to_host` to a function
jakirkham Dec 20, 2019
6098038
Use stream provided and don't change device_buffer
jakirkham Jan 6, 2020
93eda08
Add Mark's doc suggestion
jakirkham Jan 6, 2020
327bcad
Include changelog entry for this feature
jakirkham Jan 6, 2020
8a6aaad
Merge rapidsai/branch-0.12 into jakirkham/add_devbuf_tobytes
jakirkham Jan 6, 2020
b886251
Add `const` in `copy_to_host` forward declaration
jakirkham Jan 6, 2020
4640772
Add `stream` with default value
jakirkham Jan 6, 2020
1d1af06
Declare `copy_to_host` w/ & w/o `cudaStream_t` arg
jakirkham Jan 7, 2020
8f9662f
Test copying to raw host pointer in C++
jakirkham Jan 7, 2020
1a2989d
Define `p` as a `char*` and cast later
jakirkham Jan 7, 2020
b4f786d
Assign `const device_buffer*` beforehand
jakirkham Jan 7, 2020
7fe5c1e
Release GIL when copying to host
jakirkham Jan 7, 2020
2f550ab
Synchronize on CUDA stream before returning
jakirkham Jan 7, 2020
87fbc07
Merge rapidsai/branch-0.12 into jakirkham/add_devbuf_tobytes
jakirkham Jan 7, 2020
eec351a
Add Python test to convert `DeviceBuffer` to bytes
jakirkham Jan 7, 2020
65bd2d6
Allow passing `stream` into `tobytes`
jakirkham Jan 7, 2020
1634ef2
Drop stream synchronization from `copy_to_host`
jakirkham Jan 7, 2020
28e9116
Extern `cudaError_t` in Cython
jakirkham Jan 7, 2020
b098935
Extern `cudaStreamSynchronize`
jakirkham Jan 7, 2020
5278146
Sync stream in Cython
jakirkham Jan 7, 2020
8559ecb
Take `uintptr_t` in `tobytes` call
jakirkham Jan 7, 2020
10601c9
Import `cudaSuccess` in `device_buffer`
jakirkham Jan 7, 2020
bcf3592
Drop unneeded `ctypedef`
jakirkham Jan 7, 2020
9e43baf
Document C++ parameters of `copy_to_host`
jakirkham Jan 7, 2020
8261367
Document exceptions thrown by `copy_to_host`
jakirkham Jan 7, 2020
37b48a2
Also import `uintptr_t` in `device_buffer.pxd`
jakirkham Jan 7, 2020
48b7158
Update include/rmm/device_buffer.hpp
jakirkham Jan 7, 2020
f8ea21a
Add the CUDA error code in the Python exception
jakirkham Jan 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions include/rmm/device_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,24 +383,6 @@ class device_buffer {
*-------------------------------------------------------------------------**/
mr::device_memory_resource* memory_resource() const noexcept { return _mr; }

/**--------------------------------------------------------------------------*
* @brief Copies rmm::device_buffer to a preallocated host buffer.
*-------------------------------------------------------------------------**/
void copy_to_host(void* host_buffer, cudaStream_t stream = 0) const {
if (host_buffer == nullptr) {
throw std::runtime_error{"Cannot copy to `nullptr`."};
}
set_stream(stream);
cudaError_t err = cudaMemcpyAsync(host_buffer,
_data,
_size,
cudaMemcpyDeviceToHost,
this->stream());
if (err != cudaSuccess) {
throw std::runtime_error{"Failed to copy to host."};
}
}

private:
void* _data{nullptr}; ///< Pointer to device memory allocation
std::size_t _size{}; ///< Requested size of the device memory allocation
Expand All @@ -410,4 +392,22 @@ class device_buffer {
mr::get_default_resource()}; ///< The memory resource used to
///< allocate/deallocate device memory
};

/**--------------------------------------------------------------------------*
* @brief Copies rmm::device_buffer to a preallocated host buffer.
*-------------------------------------------------------------------------**/
jakirkham marked this conversation as resolved.
Show resolved Hide resolved
void copy_to_host(device_buffer& db, void* hb, cudaStream_t stream = 0) {
jakirkham marked this conversation as resolved.
Show resolved Hide resolved
if (hb == nullptr) {
throw std::runtime_error{"Cannot copy to `nullptr`."};
}
db.set_stream(stream);
jakirkham marked this conversation as resolved.
Show resolved Hide resolved
cudaError_t err = cudaMemcpyAsync(hb,
db.data(),
db.size(),
cudaMemcpyDeviceToHost,
db.stream());
if (err != cudaSuccess) {
throw std::runtime_error{"Failed to copy to host."};
}
}
} // namespace rmm
3 changes: 2 additions & 1 deletion python/rmm/_lib/device_buffer.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ cdef extern from "rmm/device_buffer.hpp" namespace "rmm" nogil:
void* data()
size_t size()
size_t capacity()
void copy_to_host(void* host_buffer) except *

void copy_to_host(device_buffer& db, void* hb) except *

cdef class DeviceBuffer:
cdef unique_ptr[device_buffer] c_obj
Expand Down
2 changes: 1 addition & 1 deletion python/rmm/_lib/device_buffer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ cdef class DeviceBuffer:
cdef bytes b = PyBytes_FromStringAndSize(NULL, self.c_size())
cdef void* p = <void*>PyBytes_AS_STRING(b)
kkraus14 marked this conversation as resolved.
Show resolved Hide resolved

self.c_obj.get()[0].copy_to_host(p)
copy_to_host(self.c_obj.get()[0], p)

return b

Expand Down