diff --git a/cuda_bindings/cuda/bindings/cufile.pxd b/cuda_bindings/cuda/bindings/cufile.pxd index 13fbb68cb..9fa2361cc 100644 --- a/cuda_bindings/cuda/bindings/cufile.pxd +++ b/cuda_bindings/cuda/bindings/cufile.pxd @@ -47,8 +47,6 @@ cpdef intptr_t handle_register(intptr_t descr) except? 0 cpdef void handle_deregister(intptr_t fh) except* cpdef buf_register(intptr_t buf_ptr_base, size_t length, int flags) cpdef buf_deregister(intptr_t buf_ptr_base) -cpdef read(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, off_t buf_ptr_offset) -cpdef write(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, off_t buf_ptr_offset) cpdef driver_open() cpdef use_count() cpdef driver_get_properties(intptr_t props) diff --git a/cuda_bindings/cuda/bindings/cufile.pyx b/cuda_bindings/cuda/bindings/cufile.pyx index 8f4ab7b11..f8b3e360a 100644 --- a/cuda_bindings/cuda/bindings/cufile.pyx +++ b/cuda_bindings/cuda/bindings/cufile.pyx @@ -2281,40 +2281,6 @@ cpdef buf_deregister(intptr_t buf_ptr_base): check_status(__status__) -cpdef read(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, off_t buf_ptr_offset): - """read data from a registered file handle to a specified device or host memory. - - Args: - fh (intptr_t): ``CUfileHandle_t`` opaque file handle. - buf_ptr_base (intptr_t): base address of buffer in device or host memory. - size (size_t): size bytes to read. - file_offset (off_t): file-offset from begining of the file. - buf_ptr_offset (off_t): offset relative to the buf_ptr_base pointer to read into. - - .. seealso:: `cuFileRead` - """ - with nogil: - __status__ = cuFileRead(fh, buf_ptr_base, size, file_offset, buf_ptr_offset) - check_status(__status__) - - -cpdef write(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, off_t buf_ptr_offset): - """write data from a specified device or host memory to a registered file handle. - - Args: - fh (intptr_t): ``CUfileHandle_t`` opaque file handle. - buf_ptr_base (intptr_t): base address of buffer in device or host memory. - size (size_t): size bytes to write. - file_offset (off_t): file-offset from begining of the file. - buf_ptr_offset (off_t): offset relative to the buf_ptr_base pointer to write from. - - .. seealso:: `cuFileWrite` - """ - with nogil: - __status__ = cuFileWrite(fh, buf_ptr_base, size, file_offset, buf_ptr_offset) - check_status(__status__) - - cpdef driver_open(): """Initialize the cuFile library and open the nvidia-fs driver. @@ -2689,3 +2655,44 @@ cpdef driver_close(): with nogil: status = cuFileDriverClose_v2() check_status(status) + +cpdef read(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, off_t buf_ptr_offset): + """read data from a registered file handle to a specified device or host memory. + + Args: + fh (intptr_t): ``CUfileHandle_t`` opaque file handle. + buf_ptr_base (intptr_t): base address of buffer in device or host memory. + size (size_t): size bytes to read. + file_offset (off_t): file-offset from begining of the file. + buf_ptr_offset (off_t): offset relative to the buf_ptr_base pointer to read into. + + Returns: + ssize_t: number of bytes read on success. + + .. seealso:: `cuFileRead` + """ + with nogil: + status = cuFileRead(fh, buf_ptr_base, size, file_offset, buf_ptr_offset) + check_status(status) + return status + + +cpdef write(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, off_t buf_ptr_offset): + """write data from a specified device or host memory to a registered file handle. + + Args: + fh (intptr_t): ``CUfileHandle_t`` opaque file handle. + buf_ptr_base (intptr_t): base address of buffer in device or host memory. + size (size_t): size bytes to write. + file_offset (off_t): file-offset from begining of the file. + buf_ptr_offset (off_t): offset relative to the buf_ptr_base pointer to write from. + + Returns: + ssize_t: number of bytes written on success. + + .. seealso:: `cuFileWrite` + """ + with nogil: + status = cuFileWrite(fh, buf_ptr_base, size, file_offset, buf_ptr_offset) + check_status(status) + return status diff --git a/cuda_bindings/tests/test_cufile.py b/cuda_bindings/tests/test_cufile.py index 6e7d9883a..3716e2bec 100644 --- a/cuda_bindings/tests/test_cufile.py +++ b/cuda_bindings/tests/test_cufile.py @@ -535,6 +535,11 @@ def test_cufile_read_write(): # Read data back using cuFile bytes_read = cufile.read(handle, read_buf_int, write_size, 0, 0) + # Verify bytes written equals bytes read + assert bytes_written == write_size, f"Expected to write {write_size} bytes, but wrote {bytes_written}" + assert bytes_read == write_size, f"Expected to read {write_size} bytes, but read {bytes_read}" + assert bytes_written == bytes_read, f"Bytes written ({bytes_written}) doesn't match bytes read ({bytes_read})" + # Copy read data back to host cuda.cuMemcpyDtoHAsync(host_buf, read_buf, write_size, 0) cuda.cuStreamSynchronize(0) @@ -636,6 +641,11 @@ def test_cufile_read_write_host_memory(): # Read data back using cuFile bytes_read = cufile.read(handle, read_buf_int, write_size, 0, 0) + # Verify bytes written equals bytes read + assert bytes_written == write_size, f"Expected to write {write_size} bytes, but wrote {bytes_written}" + assert bytes_read == write_size, f"Expected to read {write_size} bytes, but read {bytes_read}" + assert bytes_written == bytes_read, f"Bytes written ({bytes_written}) doesn't match bytes read ({bytes_read})" + # Verify the data read_data = ctypes.string_at(read_buf, write_size) expected_data = write_buf_content @@ -738,6 +748,11 @@ def test_cufile_read_write_large(): # Read data back using cuFile bytes_read = cufile.read(handle, read_buf_int, write_size, 0, 0) + # Verify bytes written equals bytes read + assert bytes_written == write_size, f"Expected to write {write_size} bytes, but wrote {bytes_written}" + assert bytes_read == write_size, f"Expected to read {write_size} bytes, but read {bytes_read}" + assert bytes_written == bytes_read, f"Bytes written ({bytes_written}) doesn't match bytes read ({bytes_read})" + # Copy read data back to host cuda.cuMemcpyDtoHAsync(host_buf, read_buf, write_size, 0) cuda.cuStreamSynchronize(0)