Improve device initialization for eventual CMSSW use
We've never had a completely satisfactory way of handling device initialization:
celeritas::deviceis a global variable that's allowed to be initialized exactly once- The CUDA runtime API device context is stored with
threadlocal - Disabling the device (basically) has to be done with an environment variable
Important features we want to retain from the current implementation:
- Manage a "stream" abstraction (so that the state's stream ID can be converted to a CUDA/HIP stream pointer without requiring CUDA API inclusion from host code)
- Let params objects determine whether to allocate on device or not
- Save diagnostic data about the device being used
It's really unclear to me how in practice we'd want a single process to share multiple GPUs.
I think the option to enable/disable device use is also required to cleanly implement https://github.com/drbenmorgan/celer-adept/issues/10. I don't know how tightly coupled this is to the other items here, but perhaps we could split it out into a subitem? At least in terms of the celer-adept use case it probably only needs a parameter in SetupOptions and some way to pass that other than an env var into Device?
Yeah, we can split it into a separate option. For what it's worth, it is possible to set Celeritas "environment variables" programmatically—celeritas::environment().insert({"CELER_DISABLE_DEVICE", "1"}). But there should be a better way than that to disable the device when integrating externally.
The biggest problem is that lots of the code assumes this setting is set exactly once. If we allow it to change then it could result in an inconsistent state. The best option I can think of is to define a more general Config class (perhaps a wrapper to std::map<std::string, std::any>), pass it into the highest level classes (perhaps sharing it with a global config as a default?) and replace static data with class data.