Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] The file descriptor for cuFile (GDS) is not closed after use #233

Closed
gigony opened this issue Mar 9, 2022 · 0 comments · Fixed by #234
Closed

[BUG] The file descriptor for cuFile (GDS) is not closed after use #233

gigony opened this issue Mar 9, 2022 · 0 comments · Fixed by #234
Assignees
Labels
bug Something isn't working
Milestone

Comments

@gigony
Copy link
Contributor

gigony commented Mar 9, 2022

Describe the bug

If a CuFileDriver instance is created by the file descriptor (fd), the file descriptor needs to be closed manually.
However, if the CuFileDriver instance is created by cucim.clara.filesystem.open() method, its file descriptor needs to be closed automatically when the reference count is zero.

Steps/Code to reproduce bug

from cucim.clara.filesystem import CuFileDriver
import cucim.clara.filesystem as fs
import os, cupy as cp, torch

# Create a CuPy array with size 10 (in bytes)
cp_arr = cp.ones(10, dtype=cp.uint8)
# Create a PyTorch array with size 10 (in bytes)
cuda0 = torch.device('cuda:0')
torch_arr = torch.ones(10, dtype=torch.uint8, device=cuda0)

# Using CuFileDriver
# (Opening a file with O_DIRECT flag is required for GDS)
fno = os.open("input.raw", os.O_RDONLY | os.O_DIRECT)
with CuFileDriver(fno) as fd:
  # Read 8 bytes starting from file offset 0 into buffer offset 2
  read_count = fd.pread(cp_arr, 8, 0, 2)
  # Read 10 bytes starting from file offset 3
  read_count = fd.pread(torch_arr, 10, 3)
os.close(fno)

# Another way of opening file with cuFile
with fs.open("output.raw", "w") as fd:
  # Write 10 bytes from cp_array to file starting from offset 5
  write_count = fd.pwrite(cp_arr, 10, 5)
  #############################################
  # <=== file descriptor created by fs.open() is not closed when exiting the scope
  #############################################

Expected behavior
It should close the file descriptor.

Environment details (please complete the following information):

  • Environment location: Bare-metal
  • Method of cuCIM install: pip, from source
@gigony gigony added the bug Something isn't working label Mar 9, 2022
@gigony gigony added this to the v22.02.01 milestone Mar 9, 2022
@gigony gigony self-assigned this Mar 9, 2022
@rapids-bot rapids-bot bot closed this as completed in #234 Mar 24, 2022
rapids-bot bot pushed a commit that referenced this issue Mar 24, 2022
- Close the file descriptor if the file descriptor is owned by the object.
- Update GDS example in the Jupyter notebook.

```python
from cucim.clara.filesystem import CuFileDriver
import cucim.clara.filesystem as fs
import os, cupy as cp, torch

# Create a CuPy array with size 10 (in bytes)
cp_arr = cp.ones(10, dtype=cp.uint8)
# Create a PyTorch array with size 10 (in bytes)
cuda0 = torch.device('cuda:0')
torch_arr = torch.ones(10, dtype=torch.uint8, device=cuda0)

# Using CuFileDriver
# (Opening a file with O_DIRECT flag is required for GDS)
fno = os.open("input.raw", os.O_RDONLY | os.O_DIRECT)
with CuFileDriver(fno) as fd:
  # Read 8 bytes starting from file offset 0 into buffer offset 2
  read_count = fd.pread(cp_arr, 8, 0, 2)
  # Read 10 bytes starting from file offset 3
  read_count = fd.pread(torch_arr, 10, 3)
os.close(fno)

# Another way of opening file with cuFile
with fs.open("output.raw", "w") as fd:
  # Write 10 bytes from cp_array to file starting from offset 5
  write_count = fd.pwrite(cp_arr, 10, 5)
  #############################################
  # <=== file descriptor created by fs.open() would be closed when exiting the scope
  #############################################
```


Fixes #233

Authors:
  - Gigon Bae (https://github.com/gigony)

Approvers:
  - https://github.com/jakirkham

URL: #234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant