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

Handle file descriptor ownership and update documents for GDS #234

Merged
merged 2 commits into from
Mar 24, 2022

Conversation

gigony
Copy link
Contributor

@gigony gigony commented Mar 9, 2022

  • Close the file descriptor if the file descriptor is owned by the object.
  • Update GDS example in the Jupyter notebook.
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

Signed-off-by: Gigon Bae <gbae@nvidia.com>
@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 requested a review from a team as a code owner March 9, 2022 02:05
@gigony gigony self-assigned this Mar 9, 2022
@gigony gigony added the non-breaking Introduces a non-breaking change label Mar 9, 2022
@@ -70,6 +74,13 @@ struct EXPORT_VISIBLE CuCIMFileHandle : public std::enable_shared_from_this<CuCI
deleter(this);
deleter = nullptr;
}

if (own_fd)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check fd too? Is there any risk it is undefined?

Copy link
Contributor Author

@gigony gigony Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jakirkham for the feedback!
Will update to make sure to check fd (fd >= 0. It's default value is -1)

Comment on lines 253 to +257
"fno = os.open(\"output.raw\", os.O_RDWR | os.O_CREAT | os.O_TRUNC)\n",
"fd = CuFileDriver(fno)\n",
"write_count = fd.pwrite(np_arr, 10, 5) # write 10 bytes from np_array to file starting from offset 5\n",
"fd.close()\n",
"os.close(fno)\n",
Copy link
Member

@jakirkham jakirkham Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth writing this to use a context manager (with statement). Just a thought 🙂

Edit: Though I guess we are illustrating this below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

This is to showcase the different ways to call the methods.
Here, I couldn't use with statement for os.close(fno) because Python's built-in os.open() returns int type and there is no context manager available.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can always roll our own. Though maybe that's unnecessary for an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if we want to provide such a wrapper in Python for the file descriptor(int).

with is supported by our API. the recommended way for using it (without close method) is described in the notebook and a description of the GDS reader page so don't need to change here?

https://github.com/NVIDIA/MagnumIO/tree/main/gds/readers/cucim-gds

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)

@jakirkham
Copy link
Member

Thanks Gigon! 😄 Generally looks good. Had a couple questions above 🙂

Signed-off-by: Gigon Bae <gbae@nvidia.com>
@jakirkham
Copy link
Member

@gpucibot merge

@rapids-bot rapids-bot bot merged commit 4f79b07 into rapidsai:branch-22.04 Mar 24, 2022
@jakirkham
Copy link
Member

Thanks Gigon! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working non-breaking Introduces a non-breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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