Skip to content

Implement cudax::cufile#6122

Merged
davebayer merged 2 commits intoNVIDIA:mainfrom
davebayer:cudax_cufile_cufile
Nov 18, 2025
Merged

Implement cudax::cufile#6122
davebayer merged 2 commits intoNVIDIA:mainfrom
davebayer:cudax_cufile_cufile

Conversation

@davebayer
Copy link
Contributor

@davebayer davebayer commented Oct 3, 2025

This PR implements wrappers of CUfileHandle_t and related APIs.

It introduces:

  1. cudax::cufile type which handles opening/closing the native file handle and registering/deregistering of the cuFile file handle. It owns both of these resources.
  2. cudax::cufile_ref type that is a lightweight non-owning wrapper for the cuFile file handle. It doesn't own the resources (with exception below).
  3. cuda::cufile_driver.[de]register_native_handle(...) -> cuda::cufile_ref that can be used for manual registration of the native file handle. It has basically the same semantics as new/delete.

So, right now these are the scenarios are covered:

  1. the user has some legacy code from where he gets the CUfileHandle_t -> he can use cuda::cufile_ref with our APIs.
  2. the user has some code that gives him the native file handle, but he doesn't own the native file handle -> he can use cuda::cufile_driver.register_native_handle(...).
  3. the user wants us to open the native file handle for him as well -> he can use cuda::cufile

We might introduce a third option: cuda::scoped_cufile or something similar, that would be constructible from cuda::cufile_ref and would deregister the handle when going out of scope, but I would wait with it.

@copy-pr-bot
Copy link
Contributor

copy-pr-bot bot commented Oct 3, 2025

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@cccl-authenticator-app cccl-authenticator-app bot moved this from Todo to In Progress in CCCL Oct 3, 2025
@davebayer davebayer mentioned this pull request Oct 8, 2025
6 tasks
@davebayer davebayer self-assigned this Oct 8, 2025
@davebayer davebayer force-pushed the cudax_cufile_cufile branch 2 times, most recently from ff5d648 to 123f2f5 Compare October 16, 2025 13:19
@davebayer davebayer marked this pull request as ready for review October 16, 2025 13:20
@davebayer davebayer requested a review from a team as a code owner October 16, 2025 13:20
@davebayer davebayer requested a review from pciolkosz October 16, 2025 13:20
@cccl-authenticator-app cccl-authenticator-app bot moved this from In Progress to In Review in CCCL Oct 16, 2025
@davebayer davebayer force-pushed the cudax_cufile_cufile branch 2 times, most recently from e94e43d to d61c94c Compare October 16, 2025 13:52
@github-actions

This comment has been minimized.

//! @brief Wrapper for retrieving the open mode.
[[nodiscard]] static _CCCL_HOST_API cufile_open_mode __open_mode(native_handle_type __native_handle)
{
int __oflags = ::fcntl(__native_handle, F_GETFL);

Choose a reason for hiding this comment

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

do we really need to retrieve the opening mode?
I'm a bit worried on non-standard things in the bindings for when eventually cufile is windows supported.

Comment on lines +269 to +287
_CCCL_HOST_API void open(const char* __filename, cufile_open_mode __open_mode)
{
if (is_open())
{
::cuda::std::__throw_runtime_error("File is already opened.");
}

__native_handle_ = __open_file(__filename, __make_oflags(__open_mode));

try
{
__cufile_handle_ = __register_cufile_handle(__native_handle_);
}
catch (...)
{
__close_file(::cuda::std::exchange(__native_handle_, __invalid_native_handle));
throw;
}
}

Choose a reason for hiding this comment

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

I'm not sure we need this... its two ways of doing the same (this vs constructor) and this actually is forbidden if the constructor was already called. I'd vote to remove this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But for example std::fstream also implements the .open(...) method. I'd like to stay as close as what is common in the standard as possible

Choose a reason for hiding this comment

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

but we can always implement it later if needed... I don't see why this is needed; we are not trying to stay close to fstream either, are we?

Copy link
Contributor Author

@davebayer davebayer Oct 16, 2025

Choose a reason for hiding this comment

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

That's true, but I'd like to provide tools users are used to have. Consider this:

cuda::cufile file;
// ...
file = cuda::cufile{filename, open_mode}; // open the file
// ...
file = cuda::cufile{}; // close the file

You have a situation like this, when you want to have the object in not opened state. Then, to open the file, you must create another object and move assign it to the original object. And if you want to close the file with detection whether it was closed or not, you must (again) move assign a new default constructed object.

In my opinion this is not a good design. I'd like to do:

cuda::cufile file;
// ...
file.open(filename, open_mode); // open the file
// ...
file.close(); // close the file

@jvera-nvidia
Copy link

jvera-nvidia commented Oct 16, 2025 via email

@github-actions

This comment has been minimized.


void test_check_fd_is_valid(int fd)
{
CUDAX_REQUIRE(fcntl(fd, F_GETFD) == 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could correct flags be non-zero here? Internet says to do:
fcntl(fd, F_GETFD) != -1 || errno != EBADF;
For this use case probably just the first part is ok

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, you are right

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

This was referenced Oct 17, 2025
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@davebayer davebayer force-pushed the cudax_cufile_cufile branch from acc2cf1 to f683170 Compare October 20, 2025 20:15
@github-actions

This comment has been minimized.

@davebayer
Copy link
Contributor Author

@davebayer davebayer requested review from a team as code owners November 14, 2025 09:19
@davebayer davebayer requested a review from alliepiper November 14, 2025 09:19
@davebayer
Copy link
Contributor Author

Waiting for https://github.com/nv-gha-runners/roadmap/issues/318

As discussed internally, we will just build the tests for now, we will run those tests once the support is added. I tested it locally with CUDA 12.9 and 13.0 and both compiled & ran fine.

@github-actions

This comment has been minimized.

@github-project-automation github-project-automation bot moved this from In Review to In Progress in CCCL Nov 17, 2025
@davebayer davebayer requested a review from alliepiper November 17, 2025 19:50
@github-project-automation github-project-automation bot moved this from In Progress to In Review in CCCL Nov 17, 2025
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@davebayer davebayer enabled auto-merge (squash) November 18, 2025 07:41
@github-actions
Copy link
Contributor

🥳 CI Workflow Results

🟩 Finished in 12h 12m: Pass: 100%/261 | Total: 3d 16h | Max: 1h 41m | Hits: 99%/379106

See results here.

@davebayer davebayer merged commit 006fa90 into NVIDIA:main Nov 18, 2025
807 of 818 checks passed
@github-project-automation github-project-automation bot moved this from In Review to Done in CCCL Nov 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants