From 836f6dd6bb43b361f2086598490ae581d8ce6825 Mon Sep 17 00:00:00 2001 From: Chloe Chia Date: Thu, 23 Oct 2025 19:37:52 +0000 Subject: [PATCH 1/4] add cufile.pxd --- cuda_bindings/cuda/bindings/cufile.pxd | 2 -- 1 file changed, 2 deletions(-) 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) From 90c2dfb0745c9fe4000a42491ef4b5c5d9ff4276 Mon Sep 17 00:00:00 2001 From: Chloe Chia Date: Thu, 23 Oct 2025 19:45:37 +0000 Subject: [PATCH 2/4] add cufile.pyx --- cuda_bindings/cuda/bindings/cufile.pyx | 60 +++++++++++--------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/cuda_bindings/cuda/bindings/cufile.pyx b/cuda_bindings/cuda/bindings/cufile.pyx index 8f4ab7b11..35b3b5fae 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,29 @@ 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. + """ + 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. + + .. seealso:: `cuFileWrite` + """ + with nogil: + status = cuFileWrite(fh, buf_ptr_base, size, file_offset, buf_ptr_offset) + check_status(status) + return status From 3a89bc424efdfc72b415c7c26fa326f255504efd Mon Sep 17 00:00:00 2001 From: Chloe Chia Date: Thu, 23 Oct 2025 20:36:37 +0000 Subject: [PATCH 3/4] Add to docstring for read --- cuda_bindings/cuda/bindings/cufile.pyx | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cuda_bindings/cuda/bindings/cufile.pyx b/cuda_bindings/cuda/bindings/cufile.pyx index 35b3b5fae..f8b3e360a 100644 --- a/cuda_bindings/cuda/bindings/cufile.pyx +++ b/cuda_bindings/cuda/bindings/cufile.pyx @@ -2658,6 +2658,18 @@ cpdef driver_close(): 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) @@ -2675,6 +2687,9 @@ cpdef write(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, 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: From 34e9190ef9af293faf134d4f74fe0af3c701a0a2 Mon Sep 17 00:00:00 2001 From: Chloe Chia Date: Sun, 26 Oct 2025 18:04:22 +0000 Subject: [PATCH 4/4] Check for read and write bytes equal each other --- cuda_bindings/tests/test_cufile.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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)