Skip to content

Commit

Permalink
Remove non-blocking functions and add opencl_copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Pencilcaseman committed Mar 27, 2024
1 parent 8bfe2a4 commit f669489
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ blas_blis = ["blas"] # BLIS/Flame Framework
blas_openblas = ["blas"] # OpenBLAS
blas_mkl = ["blas"] # Intel MKL

blas_clblast = ["blas", "opencl"] # CLBlast (https://github.com/CNugteren/CLBlast)
blas_cublas = ["blas", "cuda"] # cuBLAS

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
num-traits = "0.2"
Expand Down
6 changes: 4 additions & 2 deletions hasty_impl/include/opencl/opencl_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ OpenCLErrorCode opencl_allocate(size_t bytes, OpenCLMemoryType mem_type, cl::Buf

void opencl_free(cl::Buffer *buf);

OpenCLErrorCode opencl_write(const cl::Buffer &dst, const void *src, size_t bytes, bool blocking);
OpenCLErrorCode opencl_write(const cl::Buffer &dst, const void *src, size_t bytes);

OpenCLErrorCode opencl_read(void *dst, const cl::Buffer &src, size_t bytes, bool blocking);
OpenCLErrorCode opencl_read(void *dst, const cl::Buffer &src, size_t bytes);

OpenCLErrorCode opencl_copy(cl::Buffer &dst, const cl::Buffer &src, size_t bytes);

#endif // HASTY_IMPL_HAS_OPENCL
#endif //HASTY_IMPL_OPENCL_MEMORY_HPP
8 changes: 4 additions & 4 deletions hasty_impl/include/opencl/opencl_memory_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ enum OpenCLErrorCode opencl_allocate_voidptr(uint64_t bytes, enum OpenCLMemoryTy

void opencl_free_voidptr(void *buf);

enum OpenCLErrorCode
opencl_write_voidptr(void *dst, const void *src, uint64_t bytes, bool blocking);
enum OpenCLErrorCode opencl_write_voidptr(void *dst, const void *src, uint64_t bytes);

enum OpenCLErrorCode
opencl_read_voidptr(void *dst, const void *src, uint64_t bytes, bool blocking);
enum OpenCLErrorCode opencl_read_voidptr(void *dst, const void *src, uint64_t bytes);

enum OpenCLErrorCode opencl_copy_voidptr(void *dst, const void *src, uint64_t bytes);

#ifdef __cplusplus
}
Expand Down
28 changes: 20 additions & 8 deletions hasty_impl/src/opencl/opencl_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ void opencl_free(cl::Buffer *buf) {
delete buf;
}

OpenCLErrorCode opencl_write(const cl::Buffer &dst, const void *src, uint64_t bytes, bool blocking) {
OpenCLErrorCode opencl_write(const cl::Buffer &dst, const void *src, uint64_t bytes) {
// We always block here, since we need the data to be available. At some point we might want to add the option
// for a non-blocking write, but that's pretty complicated to handle correctly.
return get_opencl_error_code(
global::openCLQueue.enqueueWriteBuffer(dst, blocking ? CL_TRUE : CL_FALSE, 0, bytes, src));
global::openCLQueue.enqueueWriteBuffer(dst, CL_TRUE, 0, bytes, src));
}

OpenCLErrorCode opencl_read(void *dst, const cl::Buffer &src, uint64_t bytes, bool blocking) {
OpenCLErrorCode opencl_read(void *dst, const cl::Buffer &src, uint64_t bytes) {
// We always block here, since we need the data to be available. At some point we might want to add the option
// for a non-blocking read, but that's pretty complicated to handle correctly.
return get_opencl_error_code(
global::openCLQueue.enqueueReadBuffer(src, blocking ? CL_TRUE : CL_FALSE, 0, bytes, dst));
global::openCLQueue.enqueueReadBuffer(src, CL_TRUE, 0, bytes, dst));
}

OpenCLErrorCode opencl_copy(cl::Buffer &dst, const cl::Buffer &src, size_t bytes) {
return get_opencl_error_code(global::openCLQueue.enqueueCopyBuffer(src, dst, 0, 0, bytes));
}

#ifdef __cplusplus
Expand All @@ -52,12 +60,16 @@ void opencl_free_voidptr(void *buf) {
delete (cl::Buffer *) buf;
}

OpenCLErrorCode opencl_write_voidptr(void *dst, const void *src, uint64_t bytes, bool blocking) {
return opencl_write(*(cl::Buffer *) dst, src, bytes, blocking);
OpenCLErrorCode opencl_write_voidptr(void *dst, const void *src, uint64_t bytes) {
return opencl_write(*(cl::Buffer *) dst, src, bytes);
}

OpenCLErrorCode opencl_read_voidptr(void *dst, const void *src, uint64_t bytes) {
return opencl_read(dst, *(cl::Buffer *) src, bytes);
}

OpenCLErrorCode opencl_read_voidptr(void *dst, const void *src, uint64_t bytes, bool blocking) {
return opencl_read(dst, *(cl::Buffer *) src, bytes, blocking);
OpenCLErrorCode opencl_copy_voidptr(void *dst, const void *src, uint64_t bytes) {
return opencl_copy(*(cl::Buffer *) dst, *(cl::Buffer *) src, bytes);
}

#ifdef __cplusplus
Expand Down
23 changes: 19 additions & 4 deletions src/opencl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,12 @@ pub unsafe fn opencl_free(buffer: *mut ::std::os::raw::c_void) {
hasty_impl::opencl_free_voidptr(buffer);
}

/// Write data to the OpenCL device
/// Write data to OpenCL device memory from host memory
///
/// **Note**: There are no checks on the pointers, nor the size of the data. The caller
/// must ensure that everything is valid.
pub unsafe fn opencl_write(dst: *mut ::std::os::raw::c_void, src: *const ::std::os::raw::c_void, bytes: usize) -> Result<(), OpenCLErrorCode> {
let ret = hasty_impl::opencl_write_voidptr(dst, src, bytes as u64, true);
let ret = hasty_impl::opencl_write_voidptr(dst, src, bytes as u64);

if ret == hasty_impl::OpenCLErrorCode_Success {
Ok(())
Expand All @@ -379,16 +379,31 @@ pub unsafe fn opencl_write(dst: *mut ::std::os::raw::c_void, src: *const ::std::
}
}

/// Read data from the OpenCL device
/// Read data from the OpenCL device into host memory
///
/// **Note**: There are no checks on the pointers, nor the size of the data. The caller
/// must ensure that everything is valid.
pub unsafe fn opencl_read(dst: *mut ::std::os::raw::c_void, src: *const ::std::os::raw::c_void, bytes: usize) -> Result<(), OpenCLErrorCode> {
let ret = hasty_impl::opencl_read_voidptr(dst, src, bytes as u64, true);
let ret = hasty_impl::opencl_read_voidptr(dst, src, bytes as u64);

if ret == hasty_impl::OpenCLErrorCode_Success {
Ok(())
} else {
Err(OpenCLErrorCode::from_ffi(ret))
}
}

/// Copy data from one OpenCL buffer to another
///
/// **Note**: There are no checks on the pointers, nor the size of the data. The caller
/// must ensure that everything is valid.
pub unsafe fn opencl_copy(dst: *mut ::std::os::raw::c_void, src: *const ::std::os::raw::c_void, bytes: usize) -> Result<(), OpenCLErrorCode> {
let ret = hasty_impl::opencl_copy_voidptr(dst, src, bytes as u64);

if ret == hasty_impl::OpenCLErrorCode_Success {
Ok(())
} else {
Err(OpenCLErrorCode::from_ffi(ret))
}
}

0 comments on commit f669489

Please sign in to comment.