Actual source code: dcontext.cxx
1: #include <petsc/private/deviceimpl.h>
2: #include "objpool.hpp"
4: /* Define the allocator */
5: struct PetscDeviceContextAllocator : Petsc::Allocator<PetscDeviceContext>
6: {
7: static PetscInt PetscDeviceContextID;
9: PETSC_NODISCARD PetscErrorCode create(PetscDeviceContext *dctx) noexcept
10: {
11: PetscDeviceContext dc;
12: PetscErrorCode ierr;
15: PetscNew(&dc);
16: dc->id = PetscDeviceContextID++;
17: dc->idle = PETSC_TRUE;
18: dc->streamType = PETSC_STREAM_DEFAULT_BLOCKING;
19: *dctx = dc;
20: return(0);
21: }
23: PETSC_NODISCARD PetscErrorCode destroy(PetscDeviceContext &dctx) const noexcept
24: {
28: if (PetscUnlikelyDebug(dctx->numChildren)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Device context still has %D un-restored children, must call PetscDeviceContextRestore() on all children before destroying",dctx->numChildren);
29: if (dctx->ops->destroy) {(*dctx->ops->destroy)(dctx);}
30: PetscDeviceDestroy(&dctx->device);
31: PetscFree(dctx->childIDs);
32: PetscFree(dctx);
33: return(0);
34: }
36: PETSC_NODISCARD PetscErrorCode reset(PetscDeviceContext &dctx) const noexcept
37: {
41: /* don't deallocate the child array, rather just zero it out */
42: PetscArrayzero(dctx->childIDs,dctx->maxNumChildren);
43: dctx->setup = PETSC_FALSE;
44: dctx->numChildren = 0;
45: dctx->idle = PETSC_TRUE;
46: dctx->streamType = PETSC_STREAM_DEFAULT_BLOCKING;
47: return(0);
48: }
50: PETSC_NODISCARD PetscErrorCode finalize(void) noexcept
51: {
53: PetscDeviceContextID = 0;
54: return(0);
55: }
56: };
57: PetscInt PetscDeviceContextAllocator::PetscDeviceContextID = 0;
59: static Petsc::ObjectPool<PetscDeviceContext,PetscDeviceContextAllocator> contextPool;
61: /*@C
62: PetscDeviceContextCreate - Creates a PetscDeviceContext
64: Not Collective, Asynchronous
66: Output Paramemter:
67: . dctx - The PetscDeviceContext
69: Notes:
70: Unlike almost every other PETSc class it is advised that most users use
71: PetscDeviceContextDuplicate() rather than this routine to create new contexts. Contexts
72: of different types are incompatible with one another; using
73: PetscDeviceContextDuplicate() ensures compatible types.
75: Level: beginner
77: .seealso: PetscDeviceContextDuplicate(), PetscDeviceContextSetDevice(),
78: PetscDeviceContextSetStreamType(), PetscDeviceContextSetUp(),
79: PetscDeviceContextSetFromOptions(), PetscDeviceContextDestroy()
80: @*/
81: PetscErrorCode PetscDeviceContextCreate(PetscDeviceContext *dctx)
82: {
87: PetscDeviceInitializePackage();
88: contextPool.get(*dctx);
89: return(0);
90: }
92: /*@C
93: PetscDeviceContextDestroy - Frees a PetscDeviceContext
95: Not Collective, Asynchronous
97: Input Parameters:
98: . dctx - The PetscDeviceContext
100: Notes:
101: No implicit synchronization occurs due to this routine, all resources are released completely asynchronously
102: w.r.t. the host. If one needs to guarantee access to the data produced on this contexts stream one should perform the
103: appropriate synchronization before calling this routine.
105: Developer Notes:
106: The context is never actually "destroyed", only returned to an ever growing pool of
107: contexts. There are currently no safeguards on the size of the pool, this should perhaps
108: be implemented.
110: Level: beginner
112: .seealso: PetscDeviceContextCreate(), PetscDeviceContextSetDevice(), PetscDeviceContextSetUp(), PetscDeviceContextSynchronize()
113: @*/
114: PetscErrorCode PetscDeviceContextDestroy(PetscDeviceContext *dctx)
115: {
119: if (!*dctx) return(0);
120: /* use move assignment whenever possible */
121: contextPool.reclaim(std::move(*dctx));
122: return(0);
123: }
125: /*@C
126: PetscDeviceContextSetStreamType - Set the implementation type of the underlying stream for a PetscDeviceContext
128: Not Collective, Asynchronous
130: Input Parameters:
131: + dctx - The PetscDeviceContext
132: - type - The PetscStreamType
134: Notes:
135: See PetscStreamType in include/petscdevicetypes.h for more information on the available
136: types and their interactions. If the PetscDeviceContext was previously set up and stream
137: type was changed, you must call PetscDeviceContextSetUp() again after this routine.
139: Level: intermediate
141: .seealso: PetscDeviceContextGetStreamType(), PetscDeviceContextCreate(), PetscDeviceContextSetUp(), PetscDeviceContextSetFromOptions()
142: @*/
143: PetscErrorCode PetscDeviceContextSetStreamType(PetscDeviceContext dctx, PetscStreamType type)
144: {
148: /* only need to do complex swapping if the object has already been setup */
149: if (dctx->setup && (dctx->streamType != type)) {
152: (*dctx->ops->changestreamtype)(dctx,type);
153: dctx->setup = PETSC_FALSE;
154: }
155: dctx->streamType = type;
156: return(0);
157: }
159: /*@C
160: PetscDeviceContextGetStreamType - Get the implementation type of the underlying stream for a PetscDeviceContext
162: Not Collective, Asynchronous
164: Input Parameter:
165: . dctx - The PetscDeviceContext
167: Output Parameter:
168: . type - The PetscStreamType
170: Notes:
171: See PetscStreamType in include/petscdevicetypes.h for more information on the available types and their interactions
173: Level: intermediate
175: .seealso: PetscDeviceContextSetStreamType(), PetscDeviceContextCreate(), PetscDeviceContextSetFromOptions()
176: @*/
177: PetscErrorCode PetscDeviceContextGetStreamType(PetscDeviceContext dctx, PetscStreamType *type)
178: {
182: *type = dctx->streamType;
183: return(0);
184: }
186: /*@C
187: PetscDeviceContextSetDevice - Set the underlying device for the PetscDeviceContext
189: Not Collective, Possibly Synchronous
191: Input Parameters:
192: + dctx - The PetscDeviceContext
193: - device - The PetscDevice
195: Notes:
196: This routine is effectively PetscDeviceContext's "set-type" (so every PetscDeviceContext
197: must also have an attached PetscDevice). Unlike the usual set-type semantics, it is
198: not stricly necessary to set a contexts device to enable usage, any created device
199: contexts will always come equipped with the "default" device.
201: This routine may initialize the backend device and incur synchronization.
203: Level: intermediate
205: .seealso: PetscDeviceCreate(), PetscDeviceConfigure(), PetscDeviceContextGetDevice()
206: @*/
207: PetscErrorCode PetscDeviceContextSetDevice(PetscDeviceContext dctx, PetscDevice device)
208: {
214: if (dctx->device == device) return(0);
215: PetscDeviceDestroy(&dctx->device);
216: PetscMemzero(dctx->ops,sizeof(*dctx->ops));
217: (*device->ops->createcontext)(dctx);
218: dctx->device = PetscDeviceReference(device);
219: dctx->setup = PETSC_FALSE;
220: return(0);
221: }
223: /*@C
224: PetscDeviceContextGetDevice - Get the underlying PetscDevice for a PetscDeviceContext
226: Not Collective, Asynchronous
228: Input Parameter:
229: . dctx - the PetscDeviceContext
231: Output Parameter:
232: . device - The PetscDevice
234: Notes:
235: This is a borrowed reference, the user should not destroy the device.
237: .seealso: PetscDeviceContextSetDevice(), PetscDevice
238: @*/
239: PetscErrorCode PetscDeviceContextGetDevice(PetscDeviceContext dctx, PetscDevice *device)
240: {
244: *device = dctx->device;
245: return(0);
246: }
248: /*@C
249: PetscDeviceContextSetUp - Prepares a PetscDeviceContext for use
251: Not Collective, Asynchronous
253: Input Parameter:
254: . dctx - The PetscDeviceContext
256: Developer Notes:
257: This routine is usually the stage where a PetscDeviceContext acquires device-side data structures such as streams,
258: events, and (possibly) handles.
260: Level: beginner
262: .seealso: PetscDeviceContextCreate(), PetscDeviceContextSetDevice(), PetscDeviceContextDestroy(), PetscDeviceContextSetFromOptions()
263: @*/
264: PetscErrorCode PetscDeviceContextSetUp(PetscDeviceContext dctx)
265: {
270: if (!dctx->device) {
271: PetscInfo2(NULL,"PetscDeviceContext %d did not have an explicitly attached PetscDevice, using default with type %s\n",dctx->id,PetscDeviceKinds[PETSC_DEVICE_DEFAULT]);
272: PetscDeviceContextSetDevice(dctx,PetscDeviceDefault_Internal());
273: }
274: if (dctx->setup) return(0);
275: (*dctx->ops->setup)(dctx);
276: dctx->setup = PETSC_TRUE;
277: return(0);
278: }
280: /*@C
281: PetscDeviceContextDuplicate - Duplicates a PetscDeviceContext object
283: Not Collective, Asynchronous
285: Input Parameter:
286: . dctx - The PetscDeviceContext to duplicate
288: Output Paramter:
289: . strmdup - The duplicated PetscDeviceContext
291: Notes:
292: This is a shorthand method for creating a PetscDeviceContext with the exact same
293: settings as another. Note however that the duplicated PetscDeviceContext does not "share"
294: any of the underlying data with the original, (including its current stream-state) they
295: are completely separate objects.
297: Level: beginner
299: .seealso: PetscDeviceContextCreate(), PetscDeviceContextSetDevice(), PetscDeviceContextSetStreamType()
300: @*/
301: PetscErrorCode PetscDeviceContextDuplicate(PetscDeviceContext dctx, PetscDeviceContext *dctxdup)
302: {
308: PetscDeviceContextCreate(dctxdup);
309: PetscDeviceContextSetDevice(*dctxdup,dctx->device);
310: PetscDeviceContextSetStreamType(*dctxdup,dctx->streamType);
311: PetscDeviceContextSetUp(*dctxdup);
312: return(0);
313: }
315: /*@C
316: PetscDeviceContextQueryIdle - Returns whether or not a PetscDeviceContext is idle
318: Not Collective, Asynchronous
320: Input Parameter:
321: . dctx - The PetscDeviceContext object
323: Output Parameter:
324: . idle - PETSC_TRUE if PetscDeviceContext has NO work, PETSC_FALSE if it has work
326: Notes:
327: This routine only refers a singular context and does NOT take any of its children into account. That is, if dctx is
328: idle but has dependents who do have work, this routine still returns PETSC_TRUE.
330: Results of PetscDeviceContextQueryIdle() are cached on return, allowing this function to be called repeatedly in an
331: efficient manner. When debug mode is enabled this cache is verified on every call to
332: this routine, but is blindly believed when debugging is disabled.
334: Level: intermediate
336: .seealso: PetscDeviceContextCreate(), PetscDeviceContextWaitForContext(), PetscDeviceContextFork()
337: @*/
338: PetscErrorCode PetscDeviceContextQueryIdle(PetscDeviceContext dctx, PetscBool *idle)
339: {
345: if (dctx->idle) {
346: *idle = PETSC_TRUE;
347: PetscDeviceContextValidateIdle_Internal(dctx);
348: } else {
349: (*dctx->ops->query)(dctx,idle);
350: dctx->idle = *idle;
351: }
352: return(0);
353: }
355: /*@C
356: PetscDeviceContextWaitForContext - Make one context wait for another context to finish
358: Not Collective, Asynchronous
360: Input Parameters:
361: + dctxa - The PetscDeviceContext object that is waiting
362: - dctxb - The PetscDeviceContext object that is being waited on
364: Notes:
365: Serializes two PetscDeviceContexts. This routine uses only the state of dctxb at the moment this routine was
366: called, so any future work queued will not affect dctxa. It is safe to pass the same context to both arguments.
368: Level: beginner
370: .seealso: PetscDeviceContextCreate(), PetscDeviceContextQueryIdle(), PetscDeviceContextJoin()
371: @*/
372: PetscErrorCode PetscDeviceContextWaitForContext(PetscDeviceContext dctxa, PetscDeviceContext dctxb)
373: {
378: if (dctxa == dctxb) return(0);
379: if (dctxb->idle) {
380: /* No need to do the extra function lookup and event record if the stream were waiting on isn't doing anything */
381: PetscDeviceContextValidateIdle_Internal(dctxb);
382: } else {
383: (*dctxa->ops->waitforctx)(dctxa,dctxb);
384: }
385: return(0);
386: }
388: /*@C
389: PetscDeviceContextFork - Create a set of dependent child contexts from a parent context
391: Not Collective, Asynchronous
393: Input Parameters:
394: + dctx - The parent PetscDeviceContext
395: - n - The number of children to create
397: Output Parameter:
398: . dsub - The created child context(s)
400: Notes:
401: This routine creates n edges of a DAG from a source node which are causally dependent on the source node, meaning
402: that work queued on child contexts will not start until the parent context finishes its work. This accounts for work
403: queued on the parent up until calling this function, any subsequent work enqueued on the parent has no effect on the children.
405: Any children created with this routine have their lifetimes bounded by the parent. That is, the parent context expects
406: to free all of it's children (and ONLY its children) before itself is freed.
408: DAG representation:
409: .vb
410: time ->
412: -> dctx \----> dctx ------>
413: \---> dsub[0] --->
414: \--> ... ------->
415: \-> dsub[n-1] ->
416: .ve
418: Level: intermediate
420: .seealso: PetscDeviceContextJoin(), PetscDeviceContextSynchronize(), PetscDeviceContextQueryIdle()
421: @*/
422: PetscErrorCode PetscDeviceContextFork(PetscDeviceContext dctx, PetscInt n, PetscDeviceContext **dsub)
423: {
424: #if defined(PETSC_USE_DEBUG) && defined(PETSC_USE_INFO)
425: const PetscInt nBefore = n;
426: static std::string idList;
427: #endif
428: PetscDeviceContext *dsubTmp = nullptr;
429: PetscInt i = 0;
430: PetscErrorCode ierr;
435: if (PetscUnlikelyDebug(n < 0)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of contexts requested %D < 0",n);
436: #if defined(PETSC_USE_DEBUG) && defined(PETSC_USE_INFO)
437: /* reserve 4 chars per id, 2 for number and 2 for ', ' separator */
438: idList.reserve(4*n);
439: #endif
440: /* update child totals */
441: dctx->numChildren += n;
442: /* now to find out if we have room */
443: if (dctx->numChildren > dctx->maxNumChildren) {
444: /* no room, either from having too many kids or not having any */
445: if (dctx->childIDs) {
446: /* have existing children, must reallocate them */
447: PetscRealloc(dctx->numChildren*sizeof(*dctx->childIDs),&dctx->childIDs);
448: /* clear the extra memory since realloc doesn't do it for us */
449: PetscArrayzero((dctx->childIDs)+(dctx->maxNumChildren),(dctx->numChildren)-(dctx->maxNumChildren));
450: } else {
451: /* have no children */
452: PetscCalloc1(dctx->numChildren,&dctx->childIDs);
453: }
454: /* update total number of children */
455: dctx->maxNumChildren = dctx->numChildren;
456: }
457: PetscMalloc1(n,&dsubTmp);
458: while (n) {
459: /* empty child slot */
460: if (!(dctx->childIDs[i])) {
461: /* create the child context in the image of its parent */
462: PetscDeviceContextDuplicate(dctx,dsubTmp+i);
463: PetscDeviceContextWaitForContext(dsubTmp[i],dctx);
464: /* register the child with its parent */
465: dctx->childIDs[i] = dsubTmp[i]->id;
466: #if defined(PETSC_USE_DEBUG) && defined(PETSC_USE_INFO)
467: idList += std::to_string(dsubTmp[i]->id);
468: if (n != 1) idList += ", ";
469: #endif
470: --n;
471: }
472: ++i;
473: }
474: #if defined(PETSC_USE_DEBUG) && defined(PETSC_USE_INFO)
475: PetscInfo3(NULL,"Forked %D children from parent %D with IDs: %s\n",nBefore,dctx->id,idList.c_str());
476: /* resets the size but doesn't deallocate the memory */
477: idList.clear();
478: #endif
479: /* pass the children back to caller */
480: *dsub = dsubTmp;
481: return(0);
482: }
484: /*@C
485: PetscDeviceContextJoin - Converge a set of child contexts
487: Not Collective, Asynchronous
489: Input Parameters:
490: + dctx - A PetscDeviceContext to converge on
491: . n - The number of sub contexts to converge
492: . joinMode - The type of join to perform
493: - dsub - The sub contexts to converge
495: Notes:
496: If PetscDeviceContextFork() creates n edges from a source node which all depend on the
497: source node, then this routine is the exact mirror. That is, it creates a node
498: (represented in dctx) which recieves n edges (and optionally destroys them) which is
499: dependent on the completion of all incoming edges.
501: If joinMode is PETSC_DEVICE_CONTEXT_JOIN_DESTROY all contexts in dsub will be destroyed
502: by this routine. Thus all sub contexts must have been created with the dctx passed to
503: this routine.
505: if joinMode is PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC dctx waits for all sub contexts but the
506: sub contexts do not wait for one another afterwards.
508: If joinMode is PETSC_DEVICE_CONTEXT_JOIN_SYNC all sub contexts will additionally
509: wait on dctx after converging. This has the effect of "synchronizing" the outgoing
510: edges.
512: DAG representations:
513: If joinMode is PETSC_DEVICE_CONTEXT_JOIN_DESTROY
514: .vb
515: time ->
517: -> dctx ---------/- dctx ->
518: -> dsub[0] -----/
519: -> ... -------/
520: -> dsub[n-1] -/
521: .ve
522: If joinMode is PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC
523: .vb
524: time ->
526: -> dctx ---------/- dctx ->
527: -> dsub[0] -----/--------->
528: -> ... -------/---------->
529: -> dsub[n-1] -/----------->
530: .ve
531: If joinMode is PETSC_DEVICE_CONTEXT_JOIN_SYNC
532: .vb
533: time ->
535: -> dctx ---------/- dctx -\----> dctx ------>
536: -> dsub[0] -----/ \---> dsub[0] --->
537: -> ... -------/ \--> ... ------->
538: -> dsub[n-1] -/ \-> dsub[n-1] ->
539: .ve
541: Level: intermediate
543: .seealso: PetscDeviceContextFork(), PetscDeviceContextSynchronize(), PetscDeviceContextJoinMode
544: @*/
545: PetscErrorCode PetscDeviceContextJoin(PetscDeviceContext dctx, PetscInt n, PetscDeviceContextJoinMode joinMode, PetscDeviceContext **dsub)
546: {
547: #if defined(PETSC_USE_DEBUG) && defined(PETSC_USE_INFO)
548: static std::string idList;
549: #endif
550: PetscErrorCode ierr;
553: /* validity of dctx is checked in the wait-for loop */
555: if (PetscUnlikelyDebug(n < 0)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of contexts merged %D < 0",n);
556: #if defined(PETSC_USE_DEBUG) && defined(PETSC_USE_INFO)
557: /* reserve 4 chars per id, 2 for number and 2 for ', ' separator */
558: idList.reserve(4*n);
559: #endif
560: /* first dctx waits on all the incoming edges */
561: for (PetscInt i = 0; i < n; ++i) {
563: PetscDeviceContextWaitForContext(dctx,(*dsub)[i]);
564: #if defined(PETSC_USE_DEBUG) && defined(PETSC_USE_INFO)
565: idList += std::to_string((*dsub)[i]->id);
566: if (i+1 < n) idList += ", ";
567: #endif
568: }
570: /* now we handle the aftermath */
571: switch (joinMode) {
572: case PETSC_DEVICE_CONTEXT_JOIN_DESTROY:
573: {
574: PetscInt j = 0;
576: if (PetscUnlikelyDebug(n > dctx->numChildren)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy %D children of a parent context that only has %D children, likely trying to restore to wrong parent",n,dctx->numChildren);
577: /* update child count while it's still fresh in memory */
578: dctx->numChildren -= n;
579: for (PetscInt i = 0; i < dctx->maxNumChildren; ++i) {
580: if (dctx->childIDs[i] && (dctx->childIDs[i] == (*dsub)[j]->id)) {
581: /* child is one of ours, can destroy it */
582: PetscDeviceContextDestroy((*dsub)+j);
583: /* reset the child slot */
584: dctx->childIDs[i] = 0;
585: if (++j == n) break;
586: }
587: }
588: /* gone through the loop but did not find every child, if this triggers (or well, doesn't) on perf-builds we leak the remaining contexts memory */
589: if (PetscUnlikelyDebug(j != n)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"%D contexts still remain after destroy, this may be because you are trying to restore to the wrong parent context, or the device contexts are not in the same order as they were checked out out in.",n-j);
590: PetscFree(*dsub);
591: }
592: break;
593: case PETSC_DEVICE_CONTEXT_JOIN_SYNC:
594: for (PetscInt i = 0; i < n; ++i) {
595: PetscDeviceContextWaitForContext((*dsub)[i],dctx);
596: }
597: case PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC:
598: break;
599: default:
600: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown PetscDeviceContextJoinMode given");
601: }
603: #if defined(PETSC_USE_DEBUG) && defined(PETSC_USE_INFO)
604: PetscInfo4(NULL,"Joined %D ctxs to ctx %D, mode %s with IDs: %s\n",n,dctx->id,PetscDeviceContextJoinModes[joinMode],idList.c_str());
605: idList.clear();
606: #endif
607: return(0);
608: }
610: /*@C
611: PetscDeviceContextSynchronize - Block the host until all work queued on or associated with a PetscDeviceContext has finished
613: Not Collective, Synchronous
615: Input Parameters:
616: . dctx - The PetscDeviceContext to synchronize
618: Level: beginner
620: .seealso: PetscDeviceContextFork(), PetscDeviceContextJoin(), PetscDeviceContextQueryIdle()
621: @*/
622: PetscErrorCode PetscDeviceContextSynchronize(PetscDeviceContext dctx)
623: {
628: /* if it isn't setup there is nothing to sync on */
629: if (dctx->setup) {(*dctx->ops->synchronize)(dctx);}
630: dctx->idle = PETSC_TRUE;
631: return(0);
632: }
634: static PetscDeviceContext globalContext = nullptr;
635: static PetscBool globalContextSetup = PETSC_FALSE;
636: static PetscStreamType defaultStreamType = PETSC_STREAM_DEFAULT_BLOCKING;
638: /* automatically registered to PetscFinalize() when first context is instantiated, do not
639: call */
640: static PetscErrorCode PetscDeviceContextDestroyGlobalContext_Private(void)
641: {
645: PetscDeviceContextSynchronize(globalContext);
646: PetscDeviceContextDestroy(&globalContext);
647: /* reset everything to defaults */
648: defaultStreamType = PETSC_STREAM_DEFAULT_BLOCKING;
649: globalContextSetup = PETSC_FALSE;
650: return(0);
651: }
653: /* creates and initializes the root context in PetscInitialize() but does not call
654: SetUp() as the user may wish to change types after PetscInitialize() */
655: PetscErrorCode PetscDeviceContextInitializeRootContext_Internal(MPI_Comm comm, const char prefix[])
656: {
660: PetscInfo1(NULL,"Initializing root PetscDeviceContext with PetscDeviceKind %s\n",PetscDeviceKinds[PETSC_DEVICE_DEFAULT]);
661: PetscDeviceContextCreate(&globalContext);
662: if (PetscUnlikelyDebug(globalContext->id != 0)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"The root current PetscDeviceContext should have id = 0, however it has id = %D",globalContext->id);
663: PetscDeviceContextSetDevice(globalContext,PetscDeviceDefault_Internal());
664: PetscDeviceContextSetStreamType(globalContext,defaultStreamType);
665: PetscDeviceContextSetFromOptions(comm,prefix,globalContext);
666: PetscRegisterFinalize(PetscDeviceContextDestroyGlobalContext_Private);
667: return(0);
668: }
670: /*@C
671: PetscDeviceContextGetCurrentContext - Get the current active PetscDeviceContext
673: Not Collective, Asynchronous
675: Output Parameter:
676: . dctx - The PetscDeviceContext
678: Notes:
679: The user generally should not destroy contexts retrieved with this routine unless they themselves have created
680: them. There exists no protection against destroying the root context.
682: Developer Notes:
683: This routine creates the "root" context the first time it is called, registering its
684: destructor to PetscFinalize(). The root context is synchronized before being destroyed.
686: Level: beginner
688: .seealso: PetscDeviceContextSetCurrentContext(), PetscDeviceContextFork(),
689: PetscDeviceContextJoin(), PetscDeviceContextCreate()
690: @*/
691: PetscErrorCode PetscDeviceContextGetCurrentContext(PetscDeviceContext *dctx)
692: {
695: if (PetscUnlikely(!globalContextSetup)) {
698: /* if there is no available device backend, PetscDeviceInitializePackage() will fire a
699: PETSC_ERR_SUP_SYS error. */
700: PetscDeviceInitializePackage();
701: PetscDeviceContextSetUp(globalContext);
702: globalContextSetup = PETSC_TRUE;
703: }
704: *dctx = globalContext;
705: return(0);
706: }
708: /*@C
709: PetscDeviceContextSetCurrentContext - Set the current active PetscDeviceContext
711: Not Collective, Asynchronous
713: Input Parameter:
714: . dctx - The PetscDeviceContext
716: Notes:
717: The old context is not stored in any way by this routine; if one is overriding a context that they themselves do not
718: control, one should take care to temporarily store it by calling PetscDeviceContextGetCurrentContext() before calling
719: this routine.
721: Level: beginner
723: .seealso: PetscDeviceContextGetCurrentContext(), PetscDeviceContextFork(),
724: PetscDeviceContextJoin(), PetscDeviceContextCreate()
725: @*/
726: PetscErrorCode PetscDeviceContextSetCurrentContext(PetscDeviceContext dctx)
727: {
732: globalContext = dctx;
733: PetscInfo1(NULL,"Set global device context id %D\n",dctx->id);
734: return(0);
735: }
737: /*@C
738: PetscDeviceContextSetFromOptions - Configure a PetscDeviceContext from the options database
740: Collective on comm, Asynchronous
742: Input Parameters:
743: + comm - MPI communicator on which to query the options database
744: . prefix - prefix to prepend to all options database queries, NULL if not needed
745: - dctx - The PetscDeviceContext to configure
747: Output Parameter:
748: . dctx - The PetscDeviceContext
750: Options Database:
751: + -device_context_device_kind - the kind of PetscDevice to attach by default - PetscDeviceKind
752: - -device_context_stream_type - type of stream to create inside the PetscDeviceContext -
753: PetscDeviceContextSetStreamType()
755: Level: beginner
757: .seealso: PetscDeviceContextSetStreamType(), PetscDeviceContextSetDevice()
758: @*/
759: PetscErrorCode PetscDeviceContextSetFromOptions(MPI_Comm comm, const char prefix[], PetscDeviceContext dctx)
760: {
761: PetscBool flag;
762: PetscInt stype,dkind;
768: PetscOptionsBegin(comm,prefix,"PetscDeviceContext Options","Sys");
769: PetscOptionsEList("-device_context_device_kind","Underlying PetscDevice","PetscDeviceContextSetDevice",PetscDeviceKinds+1,PETSC_DEVICE_MAX-1,dctx->device ? PetscDeviceKinds[dctx->device->kind] : PetscDeviceKinds[PETSC_DEVICE_DEFAULT],&dkind,&flag);
770: if (flag) {
771: PetscDeviceContextSetDevice(dctx,PetscDeviceDefaultKind_Internal(static_cast<PetscDeviceKind>(dkind+1)));
772: }
773: PetscOptionsEList("-device_context_stream_type","PetscDeviceContext PetscStreamType","PetscDeviceContextSetStreamType",PetscStreamTypes,3,PetscStreamTypes[dctx->streamType],&stype,&flag);
774: if (flag) {
775: PetscDeviceContextSetStreamType(dctx,static_cast<PetscStreamType>(stype));
776: }
777: PetscOptionsEnd();
778: return(0);
779: }