Skip to content

Commit 54ea6e8

Browse files
authored
Add Handwritten Bindings for CuFileRead and CuFileWrite (#1182)
* add cufile.pxd * add cufile.pyx * Add to docstring for read * Check for read and write bytes equal each other
1 parent 53f5680 commit 54ea6e8

File tree

3 files changed

+56
-36
lines changed

3 files changed

+56
-36
lines changed

cuda_bindings/cuda/bindings/cufile.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ cpdef intptr_t handle_register(intptr_t descr) except? 0
4747
cpdef void handle_deregister(intptr_t fh) except*
4848
cpdef buf_register(intptr_t buf_ptr_base, size_t length, int flags)
4949
cpdef buf_deregister(intptr_t buf_ptr_base)
50-
cpdef read(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, off_t buf_ptr_offset)
51-
cpdef write(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, off_t buf_ptr_offset)
5250
cpdef driver_open()
5351
cpdef use_count()
5452
cpdef driver_get_properties(intptr_t props)

cuda_bindings/cuda/bindings/cufile.pyx

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,40 +2281,6 @@ cpdef buf_deregister(intptr_t buf_ptr_base):
22812281
check_status(__status__)
22822282

22832283

2284-
cpdef read(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, off_t buf_ptr_offset):
2285-
"""read data from a registered file handle to a specified device or host memory.
2286-
2287-
Args:
2288-
fh (intptr_t): ``CUfileHandle_t`` opaque file handle.
2289-
buf_ptr_base (intptr_t): base address of buffer in device or host memory.
2290-
size (size_t): size bytes to read.
2291-
file_offset (off_t): file-offset from begining of the file.
2292-
buf_ptr_offset (off_t): offset relative to the buf_ptr_base pointer to read into.
2293-
2294-
.. seealso:: `cuFileRead`
2295-
"""
2296-
with nogil:
2297-
__status__ = cuFileRead(<Handle>fh, <void*>buf_ptr_base, size, file_offset, buf_ptr_offset)
2298-
check_status(__status__)
2299-
2300-
2301-
cpdef write(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, off_t buf_ptr_offset):
2302-
"""write data from a specified device or host memory to a registered file handle.
2303-
2304-
Args:
2305-
fh (intptr_t): ``CUfileHandle_t`` opaque file handle.
2306-
buf_ptr_base (intptr_t): base address of buffer in device or host memory.
2307-
size (size_t): size bytes to write.
2308-
file_offset (off_t): file-offset from begining of the file.
2309-
buf_ptr_offset (off_t): offset relative to the buf_ptr_base pointer to write from.
2310-
2311-
.. seealso:: `cuFileWrite`
2312-
"""
2313-
with nogil:
2314-
__status__ = cuFileWrite(<Handle>fh, <const void*>buf_ptr_base, size, file_offset, buf_ptr_offset)
2315-
check_status(__status__)
2316-
2317-
23182284
cpdef driver_open():
23192285
"""Initialize the cuFile library and open the nvidia-fs driver.
23202286
@@ -2689,3 +2655,44 @@ cpdef driver_close():
26892655
with nogil:
26902656
status = cuFileDriverClose_v2()
26912657
check_status(status)
2658+
2659+
cpdef read(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, off_t buf_ptr_offset):
2660+
"""read data from a registered file handle to a specified device or host memory.
2661+
2662+
Args:
2663+
fh (intptr_t): ``CUfileHandle_t`` opaque file handle.
2664+
buf_ptr_base (intptr_t): base address of buffer in device or host memory.
2665+
size (size_t): size bytes to read.
2666+
file_offset (off_t): file-offset from begining of the file.
2667+
buf_ptr_offset (off_t): offset relative to the buf_ptr_base pointer to read into.
2668+
2669+
Returns:
2670+
ssize_t: number of bytes read on success.
2671+
2672+
.. seealso:: `cuFileRead`
2673+
"""
2674+
with nogil:
2675+
status = cuFileRead(<Handle>fh, <void*>buf_ptr_base, size, file_offset, buf_ptr_offset)
2676+
check_status(status)
2677+
return status
2678+
2679+
2680+
cpdef write(intptr_t fh, intptr_t buf_ptr_base, size_t size, off_t file_offset, off_t buf_ptr_offset):
2681+
"""write data from a specified device or host memory to a registered file handle.
2682+
2683+
Args:
2684+
fh (intptr_t): ``CUfileHandle_t`` opaque file handle.
2685+
buf_ptr_base (intptr_t): base address of buffer in device or host memory.
2686+
size (size_t): size bytes to write.
2687+
file_offset (off_t): file-offset from begining of the file.
2688+
buf_ptr_offset (off_t): offset relative to the buf_ptr_base pointer to write from.
2689+
2690+
Returns:
2691+
ssize_t: number of bytes written on success.
2692+
2693+
.. seealso:: `cuFileWrite`
2694+
"""
2695+
with nogil:
2696+
status = cuFileWrite(<Handle>fh, <const void*>buf_ptr_base, size, file_offset, buf_ptr_offset)
2697+
check_status(status)
2698+
return status

cuda_bindings/tests/test_cufile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,11 @@ def test_cufile_read_write():
535535
# Read data back using cuFile
536536
bytes_read = cufile.read(handle, read_buf_int, write_size, 0, 0)
537537

538+
# Verify bytes written equals bytes read
539+
assert bytes_written == write_size, f"Expected to write {write_size} bytes, but wrote {bytes_written}"
540+
assert bytes_read == write_size, f"Expected to read {write_size} bytes, but read {bytes_read}"
541+
assert bytes_written == bytes_read, f"Bytes written ({bytes_written}) doesn't match bytes read ({bytes_read})"
542+
538543
# Copy read data back to host
539544
cuda.cuMemcpyDtoHAsync(host_buf, read_buf, write_size, 0)
540545
cuda.cuStreamSynchronize(0)
@@ -636,6 +641,11 @@ def test_cufile_read_write_host_memory():
636641
# Read data back using cuFile
637642
bytes_read = cufile.read(handle, read_buf_int, write_size, 0, 0)
638643

644+
# Verify bytes written equals bytes read
645+
assert bytes_written == write_size, f"Expected to write {write_size} bytes, but wrote {bytes_written}"
646+
assert bytes_read == write_size, f"Expected to read {write_size} bytes, but read {bytes_read}"
647+
assert bytes_written == bytes_read, f"Bytes written ({bytes_written}) doesn't match bytes read ({bytes_read})"
648+
639649
# Verify the data
640650
read_data = ctypes.string_at(read_buf, write_size)
641651
expected_data = write_buf_content
@@ -738,6 +748,11 @@ def test_cufile_read_write_large():
738748
# Read data back using cuFile
739749
bytes_read = cufile.read(handle, read_buf_int, write_size, 0, 0)
740750

751+
# Verify bytes written equals bytes read
752+
assert bytes_written == write_size, f"Expected to write {write_size} bytes, but wrote {bytes_written}"
753+
assert bytes_read == write_size, f"Expected to read {write_size} bytes, but read {bytes_read}"
754+
assert bytes_written == bytes_read, f"Bytes written ({bytes_written}) doesn't match bytes read ({bytes_read})"
755+
741756
# Copy read data back to host
742757
cuda.cuMemcpyDtoHAsync(host_buf, read_buf, write_size, 0)
743758
cuda.cuStreamSynchronize(0)

0 commit comments

Comments
 (0)