|
2 | 2 |
|
3 | 3 | libcufile = ctypes.CDLL("libcufile.so") |
4 | 4 |
|
| 5 | + |
5 | 6 | class CUfileError(ctypes.Structure): |
6 | 7 | _fields_ = [("err", ctypes.c_int), ("cu_err", ctypes.c_int)] |
7 | 8 |
|
| 9 | + |
8 | 10 | CUfileHandle_t = ctypes.c_void_p |
9 | 11 |
|
| 12 | + |
10 | 13 | class DescrUnion(ctypes.Union): |
11 | 14 | _fields_ = [("fd", ctypes.c_int), ("handle", ctypes.c_void_p)] |
12 | 15 |
|
| 16 | + |
13 | 17 | 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 | + |
31 | 54 |
|
32 | 55 | # convenience |
33 | 56 | def _ck(status: CUfileError, name: str): |
34 | 57 | 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 | + |
36 | 62 |
|
37 | 63 | def cuFileDriverOpen() -> None: |
38 | 64 | _ck(libcufile.cuFileDriverOpen(), "cuFileDriverOpen") |
39 | 65 |
|
| 66 | + |
40 | 67 | def cuFileDriverClose() -> None: |
41 | 68 | _ck(libcufile.cuFileDriverClose(), "cuFileDriverClose") |
42 | 69 |
|
| 70 | + |
43 | 71 | def cuFileHandleRegister(fd: int) -> CUfileHandle_t: |
44 | 72 | descr = CUfileDescr(type=1, handle=DescrUnion(fd=fd)) |
45 | 73 | handle = CUfileHandle_t() |
46 | 74 | _ck(libcufile.cuFileHandleRegister(handle, descr), "cuFileHandleRegister") |
47 | 75 | return handle |
48 | 76 |
|
| 77 | + |
49 | 78 | def cuFileHandleDeregister(handle: CUfileHandle_t) -> None: |
50 | 79 | libcufile.cuFileHandleDeregister(handle), "cuFileHandleDeregister" |
51 | 80 |
|
| 81 | + |
52 | 82 | def cuFileBufRegister(buf: ctypes.c_void_p, size: int, flags: int) -> None: |
53 | 83 | _ck(libcufile.cuFileBufRegister(buf, size, flags), "cuFileBufRegister") |
54 | 84 |
|
| 85 | + |
55 | 86 | def cuFileBufDeregister(buf: ctypes.c_void_p) -> None: |
56 | 87 | _ck(libcufile.cuFileBufDeregister(buf), "cuFileBufDeregister") |
57 | 88 |
|
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: |
59 | 97 | return libcufile.cuFileRead(handle, buf, size, file_offset, dev_offset) |
60 | 98 |
|
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: |
62 | 107 | return libcufile.cuFileWrite(handle, buf, size, file_offset, dev_offset) |
0 commit comments