Skip to content

Adds cleanup functions for resources - #224

Open
bechols97 wants to merge 5 commits into
mainfrom
bechols97/stream_cleanup
Open

Adds cleanup functions for resources#224
bechols97 wants to merge 5 commits into
mainfrom
bechols97/stream_cleanup

Conversation

@bechols97

@bechols97 bechols97 commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

This is a bugfix to add cleanup functions for resources.

The Hip and Cuda resources are non-owning and never destroy streams, which show up as leaks in memory checkers/sanitizers. In these cases, the streams would be destroyed in the new cleanup function. If there is no cleanup needed for a resource (such as Host, Omp_target, Sycl), then the cleanup function will be a no-op.

An open question for this PR is whether the cleanup functions should be thread-safe? Currently, these are not thread safe to be consistent with RAJA::release_unused_internal_memory().

Note: These changes were created by AI. I have reviewed the changes and they seemed reasonable.

Related to: #223

@artv3

artv3 commented Aug 21, 2026

Copy link
Copy Markdown
Member

Will we need a patch release ? We use the wait_for functions in my application, wait_for(default_res) type of calls.

@bechols97

Copy link
Copy Markdown
Collaborator Author

No, we probably don't need a patch release for these changes. With these changes, applications will realistically be calling the cleanup function only once prior to exiting since the number of streams created within the lifetime of the program is constant and will only be reported as leak by memcheckers/sanitizers during exit. In the worse case, this includes camp's default stream and 16 additional streams if applications use more than the default stream.

Regarding the wait_for question, I believe events were made owning in #206, which was in the release.

rhornung67
rhornung67 previously approved these changes Aug 21, 2026

@rhornung67 rhornung67 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems reasonable to me. However, someone else should review and approve to make sure.

Comment thread docs/sphinx/user_guide/feature/resource.rst Outdated
Comment thread include/camp/resource/cuda.hpp

static stream_state& get_stream_state()
{
static stream_state state;

@MrBurmark MrBurmark Aug 27, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given that we're manually handling the thread safety now, we should probably make this a class static member variable so it doesn't get another layer of thread safe initialization that comes from being a function local static. Given that we have a library we should probably put the variable definition in the camp sources, as opposed to an inline static member var.

* The caller must ensure no other thread is using CUDA resources while
* cleanup runs.
*/
static void cleanup()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does it make more sense to put most of this code in a member function of the state?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I can move most of the cleanup logic to be a state member function. However, I think we will still want a static cleanup function on the resource to be consistent across resources.

if (num < 0) {
std::lock_guard<std::mutex> lock(s_mtx);
s_previous = (s_previous + 1) % num_streams;
return s_streams[s_previous];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This still needs to be locked.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In fact does it make sense to make this whole function a member function of the state now?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It probably makes sense to make getting a stream and getting the default stream member functions at this point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants