Actual source code: global_dcontext.cxx

  1: #include "petscdevice_interface_internal.hpp" /*I <petscdevice.h> I*/

  3: /* rootContext holds the default context created by PETSc, for cleanup during PetscFinalize().
  4:    globalContext selects the current context, which may instead be supplied by the application.
  5:    PetscDeviceContextSetCurrentContext() changes this selection without transferring ownership,
  6:    so rootContext must be kept separately for PETSc to clean up the context it created. */
  7: static auto               rootDeviceType = PETSC_DEVICE_CONTEXT_DEFAULT_DEVICE_TYPE;
  8: static auto               rootStreamType = PETSC_DEVICE_CONTEXT_DEFAULT_STREAM_TYPE;
  9: static PetscDeviceContext globalContext  = nullptr;
 10: static PetscDeviceContext rootContext    = nullptr;

 12: /* when PetscDevice initializes PetscDeviceContext eagerly the type of device created should
 13:  * match whatever device is eagerly initialized */
 14: PetscErrorCode PetscDeviceContextSetRootDeviceType_Internal(PetscDeviceType type)
 15: {
 16:   PetscFunctionBegin;
 18:   rootDeviceType = type;
 19:   PetscFunctionReturn(PETSC_SUCCESS);
 20: }

 22: PetscErrorCode PetscDeviceContextSetRootStreamType_Internal(PetscStreamType type)
 23: {
 24:   PetscFunctionBegin;
 26:   rootStreamType = type;
 27:   PetscFunctionReturn(PETSC_SUCCESS);
 28: }

 30: static PetscErrorCode PetscDeviceContextSetupGlobalContext_Private() noexcept
 31: {
 32:   PetscFunctionBegin;
 33:   if (PetscUnlikely(!globalContext)) {
 34:     PetscObject pobj;
 35:     const auto  dtype     = rootDeviceType;
 36:     const auto  finalizer = [] {
 37:       PetscDeviceType dtype;

 39:       PetscFunctionBegin;
 40:       PetscCall(PetscDeviceContextGetDeviceType(rootContext, &dtype));
 41:       PetscCall(PetscInfo(rootContext, "Destroying global PetscDeviceContext with device type %s\n", PetscDeviceTypes[dtype]));
 42:       PetscCall(PetscDeviceContextDestroy(&rootContext));
 43:       globalContext  = nullptr;
 44:       rootDeviceType = PETSC_DEVICE_CONTEXT_DEFAULT_DEVICE_TYPE;
 45:       rootStreamType = PETSC_DEVICE_CONTEXT_DEFAULT_STREAM_TYPE;
 46:       PetscFunctionReturn(PETSC_SUCCESS);
 47:     };

 49:     /* this exists purely as a valid device check. */
 50:     PetscCall(PetscDeviceInitializePackage());
 51:     PetscCall(PetscRegisterFinalize(std::move(finalizer)));
 52:     PetscCall(PetscDeviceContextCreate(&globalContext));
 53:     rootContext = globalContext;
 54:     PetscCall(PetscInfo(globalContext, "Initializing global PetscDeviceContext with device type %s\n", PetscDeviceTypes[dtype]));
 55:     pobj = PetscObjectCast(globalContext);
 56:     PetscCall(PetscObjectSetName(pobj, "global root"));
 57:     PetscCall(PetscObjectSetOptionsPrefix(pobj, "root_"));
 58:     PetscCall(PetscDeviceContextSetStreamType(globalContext, rootStreamType));
 59:     PetscCall(PetscDeviceContextSetDefaultDeviceForType_Internal(globalContext, dtype));
 60:     PetscCall(PetscDeviceContextSetUp(globalContext));
 61:   }
 62:   PetscFunctionReturn(PETSC_SUCCESS);
 63: }

 65: /*@
 66:   PetscDeviceContextGetCurrentContext - Get the current active `PetscDeviceContext`

 68:   Not Collective

 70:   Output Parameter:
 71: . dctx - The `PetscDeviceContext`

 73:   Notes:
 74:   The user generally should not destroy contexts retrieved with this routine unless they
 75:   themselves have created them. There exists no protection against destroying the root
 76:   context.

 78:   Developer Notes:
 79:   Unless the user has set their own, this routine creates the "root" context the first time it
 80:   is called, registering its destructor to `PetscFinalize()`.

 82:   Level: beginner

 84: .seealso: `PetscDeviceContextSetCurrentContext()`, `PetscDeviceContextFork()`,
 85:           `PetscDeviceContextJoin()`, `PetscDeviceContextCreate()`
 86: @*/
 87: PetscErrorCode PetscDeviceContextGetCurrentContext(PetscDeviceContext *dctx)
 88: {
 89:   PetscFunctionBegin;
 90:   PetscAssertPointer(dctx, 1);
 91:   PetscCall(PetscDeviceContextSetupGlobalContext_Private());
 92:   /* while the static analyzer can find global variables, it will throw a warning about not
 93:    * being able to connect this back to the function arguments */
 95:   *dctx = globalContext;
 96:   PetscFunctionReturn(PETSC_SUCCESS);
 97: }

 99: /*@
100:   PetscDeviceContextSetCurrentContext - Set the current active `PetscDeviceContext`

102:   Not Collective

104:   Input Parameter:
105: . dctx - The `PetscDeviceContext`

107:   Level: beginner

109:   Notes:
110:   This routine selects the context used by subsequent operations of PETSc's built-in CUDA and HIP
111:   implementations. Kokkos-based types keep the execution space created when PETSc initialized
112:   Kokkos, and external packages may use their own streams. Selecting another context does not
113:   replace PETSc's root context.

115:   The supplied context must be set up, or be `PetscDeviceContextDefault`, which is resolved before
116:   being installed as current. To restore a previous context, pass the handle
117:   saved from `PetscDeviceContextGetCurrentContext()`.

119:   This routine does not take ownership of a user-created context; keep it alive while it is current
120:   and destroy it before `PetscFinalize()`. PETSc retains ownership of its root and cached default contexts.

122: .seealso: `PetscDeviceContextGetCurrentContext()`, `PetscDeviceContextFork()`,
123:           `PetscDeviceContextJoin()`, `PetscDeviceContextCreate()`
124: @*/
125: PetscErrorCode PetscDeviceContextSetCurrentContext(PetscDeviceContext dctx)
126: {
127:   PetscDeviceType dtype;

129:   PetscFunctionBegin;
130:   PetscCall(PetscDeviceContextResolveDefault_Private(&dctx));
132:   PetscAssert(dctx->setup, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "PetscDeviceContext %" PetscInt64_FMT " must be set up before being set as global context", PetscObjectCast(dctx)->id);
133:   PetscCall(PetscDeviceContextGetDeviceType(dctx, &dtype));
134:   PetscCall(PetscDeviceSetDefaultDeviceType(dtype));
135:   globalContext = dctx;
136:   PetscCall(PetscInfo(dctx, "Set global PetscDeviceContext id %" PetscInt64_FMT "\n", PetscObjectCast(dctx)->id));
137:   PetscFunctionReturn(PETSC_SUCCESS);
138: }