NVIDIA / NVIDIA/cuda-python

[FEA]: Add context managers to objects where resource cleanup is required

Open
#1,719 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

cuda.bindings enhancement example P1
Dominant language
Cython
Stars
3.4k
Forks
329
Avg merge
1d 23h
Merged PRs (30d)
116

Description

Is this a duplicate?
Area

cuda.core

Is your feature request related to a problem? Please describe.

#1711 illustrates a number of places where users are required to perform explicit cleanup of objects, by updating the examples to use try/finally and performing cleanup within the finally.

#1586 is related and probably a part of this work, but not as broad as this.

Describe the solution you'd like

We should implement context managers on any objects that need an explicit close or other resource revocation.

This includes at least Graph, GraphBuilder, Stream, possibly others.

Once these context managers are added, we should update any examples/docs/tests to use them.

Describe alternatives you've considered

I have read of resistance to this idea elsewhere based on the performance issues with contextlib.contextmanager, and if we provide them, users will use them over manual resource deallocation and pay the performance penalty everywhere.

However, context managers as a general concept (implemented with __enter__ and __exit__) do not have a major performance problem relative to correctly written code with a try/finally, as far as I know. I'm happy to be corrected on that, if there is in fact a performance problem vs. /correct/ code.

I think supporting context managers would be a major ergonomic improvement over manual tracking of context or even try/finally (which doesn't compose as well as with).

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the cuda.core objects named in the request: Graph, GraphBuilder, and Stream, along with the cleanup examples from #1711. Identify which objects require explicit close or resource revocation, then update the relevant examples, documentation, and tests to use context managers and verify cleanup.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
hpc
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.