Skip to content

Commit cfdb51d

Browse files
committed
clean up formatting issues
1 parent d6834bd commit cfdb51d

File tree

6 files changed

+167
-72
lines changed

6 files changed

+167
-72
lines changed

cufile/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
from .cufile import CuFile, CuFileDriver
88

9-
__all__ = ["CuFile", "CuFileDriver"]
9+
__all__ = ["CuFile", "CuFileDriver"]

cufile/bindings.py

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,106 @@
22

33
libcufile = ctypes.CDLL("libcufile.so")
44

5+
56
class CUfileError(ctypes.Structure):
67
_fields_ = [("err", ctypes.c_int), ("cu_err", ctypes.c_int)]
78

9+
810
CUfileHandle_t = ctypes.c_void_p
911

12+
1013
class DescrUnion(ctypes.Union):
1114
_fields_ = [("fd", ctypes.c_int), ("handle", ctypes.c_void_p)]
1215

16+
1317
class CUfileDescr(ctypes.Structure):
14-
_fields_ = [("type", ctypes.c_int), ("handle", DescrUnion), ("fs_ops", ctypes.c_void_p)]
15-
16-
libcufile.cuFileDriverOpen.restype = CUfileError
17-
libcufile.cuFileDriverClose.restype = CUfileError
18-
libcufile.cuFileHandleRegister.restype = CUfileError
19-
libcufile.cuFileBufRegister.restype = CUfileError
20-
libcufile.cuFileBufDeregister.restype = CUfileError
21-
libcufile.cuFileRead.restype = ctypes.c_size_t
22-
libcufile.cuFileWrite.restype = ctypes.c_size_t
23-
libcufile.cuFileHandleRegister.argtypes = [ctypes.POINTER(CUfileHandle_t), ctypes.POINTER(CUfileDescr)]
24-
libcufile.cuFileHandleDeregister.argtypes = [CUfileHandle_t]
25-
libcufile.cuFileBufRegister.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int]
26-
libcufile.cuFileBufDeregister.argtypes = [ctypes.c_void_p]
27-
libcufile.cuFileRead.argtypes = [CUfileHandle_t, ctypes.c_void_p, ctypes.c_size_t,
28-
ctypes.c_longlong, ctypes.c_longlong]
29-
libcufile.cuFileWrite.argtypes = [CUfileHandle_t, ctypes.c_void_p, ctypes.c_size_t,
30-
ctypes.c_longlong, ctypes.c_longlong]
18+
_fields_ = [
19+
("type", ctypes.c_int),
20+
("handle", DescrUnion),
21+
("fs_ops", ctypes.c_void_p),
22+
]
23+
24+
25+
libcufile.cuFileDriverOpen.restype = CUfileError
26+
libcufile.cuFileDriverClose.restype = CUfileError
27+
libcufile.cuFileHandleRegister.restype = CUfileError
28+
libcufile.cuFileBufRegister.restype = CUfileError
29+
libcufile.cuFileBufDeregister.restype = CUfileError
30+
libcufile.cuFileRead.restype = ctypes.c_size_t
31+
libcufile.cuFileWrite.restype = ctypes.c_size_t
32+
libcufile.cuFileHandleRegister.argtypes = [
33+
ctypes.POINTER(CUfileHandle_t),
34+
ctypes.POINTER(CUfileDescr),
35+
]
36+
libcufile.cuFileHandleDeregister.argtypes = [CUfileHandle_t]
37+
libcufile.cuFileBufRegister.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int]
38+
libcufile.cuFileBufDeregister.argtypes = [ctypes.c_void_p]
39+
libcufile.cuFileRead.argtypes = [
40+
CUfileHandle_t,
41+
ctypes.c_void_p,
42+
ctypes.c_size_t,
43+
ctypes.c_longlong,
44+
ctypes.c_longlong,
45+
]
46+
libcufile.cuFileWrite.argtypes = [
47+
CUfileHandle_t,
48+
ctypes.c_void_p,
49+
ctypes.c_size_t,
50+
ctypes.c_longlong,
51+
ctypes.c_longlong,
52+
]
53+
3154

3255
# convenience
3356
def _ck(status: CUfileError, name: str):
3457
if status.err != 0:
35-
raise RuntimeError(f"{name} failed (cuFile err={status.err}, cuda_err={status.cu_err})")
58+
raise RuntimeError(
59+
f"{name} failed (cuFile err={status.err}, cuda_err={status.cu_err})"
60+
)
61+
3662

3763
def cuFileDriverOpen() -> None:
3864
_ck(libcufile.cuFileDriverOpen(), "cuFileDriverOpen")
3965

66+
4067
def cuFileDriverClose() -> None:
4168
_ck(libcufile.cuFileDriverClose(), "cuFileDriverClose")
4269

70+
4371
def cuFileHandleRegister(fd: int) -> CUfileHandle_t:
4472
descr = CUfileDescr(type=1, handle=DescrUnion(fd=fd))
4573
handle = CUfileHandle_t()
4674
_ck(libcufile.cuFileHandleRegister(handle, descr), "cuFileHandleRegister")
4775
return handle
4876

77+
4978
def cuFileHandleDeregister(handle: CUfileHandle_t) -> None:
5079
libcufile.cuFileHandleDeregister(handle), "cuFileHandleDeregister"
5180

81+
5282
def cuFileBufRegister(buf: ctypes.c_void_p, size: int, flags: int) -> None:
5383
_ck(libcufile.cuFileBufRegister(buf, size, flags), "cuFileBufRegister")
5484

85+
5586
def cuFileBufDeregister(buf: ctypes.c_void_p) -> None:
5687
_ck(libcufile.cuFileBufDeregister(buf), "cuFileBufDeregister")
5788

58-
def cuFileRead(handle: CUfileHandle_t, buf: ctypes.c_void_p, size: int, file_offset: int, dev_offset: int) -> int:
89+
90+
def cuFileRead(
91+
handle: CUfileHandle_t,
92+
buf: ctypes.c_void_p,
93+
size: int,
94+
file_offset: int,
95+
dev_offset: int,
96+
) -> int:
5997
return libcufile.cuFileRead(handle, buf, size, file_offset, dev_offset)
6098

61-
def cuFileWrite(handle: CUfileHandle_t, buf: ctypes.c_void_p, size: int, file_offset: int, dev_offset: int) -> int:
99+
100+
def cuFileWrite(
101+
handle: CUfileHandle_t,
102+
buf: ctypes.c_void_p,
103+
size: int,
104+
file_offset: int,
105+
dev_offset: int,
106+
) -> int:
62107
return libcufile.cuFileWrite(handle, buf, size, file_offset, dev_offset)

cufile/cufile.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,27 @@
66
import ctypes
77
from .bindings import *
88

9+
910
def _singleton(cls):
1011
_instances = {}
12+
1113
def wrapper(*args, **kwargs):
1214
if cls not in _instances:
1315
_instances[cls] = cls(*args, **kwargs)
1416
return _instances[cls]
17+
1518
return wrapper
1619

20+
1721
@_singleton
1822
class CuFileDriver:
1923
def __init__(self):
2024
cuFileDriverOpen()
21-
25+
2226
def __del__(self):
2327
cuFileDriverClose()
2428

29+
2530
def _os_mode(mode: str):
2631
modes = {
2732
"r": os.O_RDONLY,
@@ -33,11 +38,12 @@ def _os_mode(mode: str):
3338
}
3439
return modes[mode]
3540

41+
3642
class CuFile:
3743
"""
3844
Main class for CUDA file operations.
3945
"""
40-
46+
4147
def __init__(self, path: str, mode: str = "r", use_direct_io: bool = False):
4248
"""
4349
Initialize the CuFile instance.
@@ -87,14 +93,22 @@ def __enter__(self):
8793
def __exit__(self, exc_type, exc_val, exc_tb):
8894
"""Context manager exit."""
8995
self.close()
90-
91-
def read(self, dest: ctypes.c_void_p, size: int, file_offset: int = 0, dev_offset: int = 0):
96+
97+
def read(
98+
self,
99+
dest: ctypes.c_void_p,
100+
size: int,
101+
file_offset: int = 0,
102+
dev_offset: int = 0,
103+
):
92104
"""Read from the file."""
93105
if not self.is_open:
94106
raise IOError("File is not open.")
95107
return cuFileRead(self._cu_file_handle, dest, size, file_offset, dev_offset)
96-
97-
def write(self, src: ctypes.c_void_p, size: int, file_offset: int = 0, dev_offset: int = 0):
108+
109+
def write(
110+
self, src: ctypes.c_void_p, size: int, file_offset: int = 0, dev_offset: int = 0
111+
):
98112
"""Write to the file."""
99113
if not self.is_open:
100114
raise IOError("File is not open.")
@@ -103,5 +117,3 @@ def write(self, src: ctypes.c_void_p, size: int, file_offset: int = 0, dev_offse
103117
def get_handle(self):
104118
"""Get the file handle."""
105119
return self._handle
106-
107-

tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""
22
Test package for cufile-python.
3-
"""
3+
"""

tests/test_cufile.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
file_path = os.path.join(WORK_DIR, "test.bin")
1818

19-
err, = cuda.cuInit(0)
19+
(err,) = cuda.cuInit(0)
2020
assert err == 0, f"cuInit failed: {err}"
2121
err, device = cuda.cuDeviceGet(CUDA_DEVICE)
2222
assert err == 0, f"cuDeviceGet failed: {err}"
@@ -30,19 +30,22 @@
3030
assert err == 0, f"cuMemAlloc failed: {err}"
3131
err, hptr = cuda.cuMemAllocHost(BUF_SIZE)
3232
assert err == 0, f"cuMemAllocHost failed: {err}"
33-
err, = cuda.cuMemsetD8(dptr_w, PATTERN_BYTE, BUF_SIZE)
33+
(err,) = cuda.cuMemsetD8(dptr_w, PATTERN_BYTE, BUF_SIZE)
3434
assert err == 0, f"cuMemsetD8 failed: {err}"
3535

36+
3637
def test_cufile_initialization():
3738
"""Test that CuFile can be initialized."""
3839
cufile = CuFile(file_path, "w")
3940
assert isinstance(cufile, CuFile)
4041

42+
4143
def test_cufile_context_manager():
4244
"""Test that CuFile works as a context manager."""
4345
with CuFile(file_path, "w") as cufile:
4446
assert isinstance(cufile, CuFile)
4547

48+
4649
def test_cufile_read_write_with_context_manager():
4750
"""Test that CuFile can read and write to a file."""
4851
begin = time.perf_counter()
@@ -52,8 +55,12 @@ def test_cufile_read_write_with_context_manager():
5255
write_time = time.perf_counter() - begin_write
5356
assert ret == BUF_SIZE
5457
dt = time.perf_counter() - begin
55-
print(f"WRITE (w/o open/register) {ret/1024/1024:.2f}MB in {write_time*1e3:.2f}ms ({ret/write_time/1024/1024/1024:.2f}GB/s)")
56-
print(f"FULL WRITE {ret/1024/1024:.2f}MB in {dt*1e3:.2f}ms ({ret/dt/1024/1024/1024:.2f}GB/s)")
58+
print(
59+
f"WRITE (w/o open/register) {ret / 1024 / 1024:.2f}MB in {write_time * 1e3:.2f}ms ({ret / write_time / 1024 / 1024 / 1024:.2f}GB/s)"
60+
)
61+
print(
62+
f"FULL WRITE {ret / 1024 / 1024:.2f}MB in {dt * 1e3:.2f}ms ({ret / dt / 1024 / 1024 / 1024:.2f}GB/s)"
63+
)
5764

5865
begin = time.perf_counter()
5966
with CuFile(file_path, "r") as cufile:
@@ -62,15 +69,20 @@ def test_cufile_read_write_with_context_manager():
6269
read_time = time.perf_counter() - begin_read
6370
assert ret == BUF_SIZE
6471
dt = time.perf_counter() - begin
65-
print(f"READ (w/o open/register) {ret/1024/1024:.2f}MB in {read_time*1e3:.2f}ms ({ret/read_time/1024/1024/1024:.2f}GB/s)")
66-
print(f"FULL READ {ret/1024/1024:.2f}MB in {dt*1e3:.2f}ms ({ret/dt/1024/1024/1024:.2f}GB/s)")
72+
print(
73+
f"READ (w/o open/register) {ret / 1024 / 1024:.2f}MB in {read_time * 1e3:.2f}ms ({ret / read_time / 1024 / 1024 / 1024:.2f}GB/s)"
74+
)
75+
print(
76+
f"FULL READ {ret / 1024 / 1024:.2f}MB in {dt * 1e3:.2f}ms ({ret / dt / 1024 / 1024 / 1024:.2f}GB/s)"
77+
)
6778

68-
err, = cuda.cuMemcpyDtoH(hptr, dptr_r, BUF_SIZE)
79+
(err,) = cuda.cuMemcpyDtoH(hptr, dptr_r, BUF_SIZE)
6980
assert err == 0, f"cuMemcpyDtoH failed: {err}"
7081
host_buf = (ctypes.c_ubyte * BUF_SIZE).from_address(hptr)
7182
for i in range(BUF_SIZE):
7283
assert host_buf[i] == PATTERN_BYTE
7384

85+
7486
def test_cufile_read_write():
7587
"""Test that CuFile can read and write to a file."""
7688

@@ -83,8 +95,12 @@ def test_cufile_read_write():
8395
assert ret == BUF_SIZE
8496
cufile.close()
8597
dt = time.perf_counter() - begin
86-
print(f"WRITE (w/o open/register) {ret/1024/1024:.2f}MB in {write_time*1e3:.2f}ms ({ret/write_time/1024/1024/1024:.2f}GB/s)")
87-
print(f"FULL WRITE {ret/1024/1024:.2f}MB in {dt*1e3:.2f}ms ({ret/dt/1024/1024/1024:.2f}GB/s)")
98+
print(
99+
f"WRITE (w/o open/register) {ret / 1024 / 1024:.2f}MB in {write_time * 1e3:.2f}ms ({ret / write_time / 1024 / 1024 / 1024:.2f}GB/s)"
100+
)
101+
print(
102+
f"FULL WRITE {ret / 1024 / 1024:.2f}MB in {dt * 1e3:.2f}ms ({ret / dt / 1024 / 1024 / 1024:.2f}GB/s)"
103+
)
88104

89105
begin = time.perf_counter()
90106
cufile = CuFile(file_path, "r")
@@ -95,15 +111,20 @@ def test_cufile_read_write():
95111
assert ret == BUF_SIZE
96112
cufile.close()
97113
dt = time.perf_counter() - begin
98-
print(f"READ (w/o open/register) {ret/1024/1024:.2f}MB in {read_time*1e3:.2f}ms ({ret/read_time/1024/1024/1024:.2f}GB/s)")
99-
print(f"FULL READ {ret/1024/1024:.2f}MB in {dt*1e3:.2f}ms ({ret/dt/1024/1024/1024:.2f}GB/s)")
114+
print(
115+
f"READ (w/o open/register) {ret / 1024 / 1024:.2f}MB in {read_time * 1e3:.2f}ms ({ret / read_time / 1024 / 1024 / 1024:.2f}GB/s)"
116+
)
117+
print(
118+
f"FULL READ {ret / 1024 / 1024:.2f}MB in {dt * 1e3:.2f}ms ({ret / dt / 1024 / 1024 / 1024:.2f}GB/s)"
119+
)
100120

101-
err, = cuda.cuMemcpyDtoH(hptr, dptr_r, BUF_SIZE)
121+
(err,) = cuda.cuMemcpyDtoH(hptr, dptr_r, BUF_SIZE)
102122
assert err == 0, f"cuMemcpyDtoH failed: {err}"
103123
host_buf = (ctypes.c_ubyte * BUF_SIZE).from_address(hptr)
104124
for i in range(BUF_SIZE):
105125
assert host_buf[i] == PATTERN_BYTE
106126

127+
107128
def test_read_write_without_open():
108129
cufile = CuFile(file_path, "r")
109130
try:

0 commit comments

Comments
 (0)