Use reference-counting of user SYCL objects to manage runtime lifetime #749
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously, the runtime was a singleton that lived within
libhipSYCL-rt
. This is occasionally caused issues with the shutdown behavior: Because the unload order of shared libraries is not well defined, it could happen that backend libraries (e.g. the CUDA runtime) unload before the hipSYCL runtime. This can prevent a clean release of allocated resources.This PR fixes this at a fundamental level:
std::weak_ptr<runtime>
.std::shared_ptr<runtime>
from the weak pointer. If theweak_ptr
is not currently valid, a new runtime object is constructed. This is abstracted using aruntime_keep_alive_token
, which obtains theshared_ptr
in its constructor.runtime*
pointers to all its classes that need it - here we cannot useshared_ptr
to avoid cyclic references or deadlocks during startup or shutdown.With this approach, the runtime will automatically shutdown when there are no SYCL classes used in the main application anymore. If there are such classes again, the runtime starts up again.
Generally SYCL applications cannot really assume a global SYCL state in other implementations either, however hipSYCL previously exhibited this behavior. This can have performance impact, since launching the runtime is not free. The old behavior can be retrieved by setting the environment variable
HIPSYCL_PERSISTENT_RUNTIME=1
.