Actual source code: inherit.c

petsc-master 2017-04-29
Report Typos and Errors

  2: /*
  3:      Provides utility routines for manipulating any type of PETSc object.
  4: */
  5:  #include <petsc/private/petscimpl.h>
  6:  #include <petscviewer.h>

  8: #if defined(PETSC_USE_LOG)
  9: PetscObject *PetscObjects      = 0;
 10: PetscInt    PetscObjectsCounts = 0, PetscObjectsMaxCounts = 0;
 11: PetscBool   PetscObjectsLog    = PETSC_FALSE;
 12: #endif

 14: extern PetscErrorCode PetscObjectGetComm_Petsc(PetscObject,MPI_Comm*);
 15: extern PetscErrorCode PetscObjectCompose_Petsc(PetscObject,const char[],PetscObject);
 16: extern PetscErrorCode PetscObjectQuery_Petsc(PetscObject,const char[],PetscObject*);
 17: extern PetscErrorCode PetscObjectComposeFunction_Petsc(PetscObject,const char[],void (*)(void));
 18: extern PetscErrorCode PetscObjectQueryFunction_Petsc(PetscObject,const char[],void (**)(void));

 20: /*
 21:    PetscHeaderCreate_Private - Creates a base PETSc object header and fills
 22:    in the default values.  Called by the macro PetscHeaderCreate().
 23: */
 24: PetscErrorCode  PetscHeaderCreate_Private(PetscObject h,PetscClassId classid,const char class_name[],const char descr[],const char mansec[],
 25:                                           MPI_Comm comm,PetscObjectDestroyFunction destroy,PetscObjectViewFunction view)
 26: {
 27:   static PetscInt idcnt = 1;
 28:   PetscErrorCode  ierr;
 29: #if defined(PETSC_USE_LOG)
 30:   PetscObject     *newPetscObjects;
 31:   PetscInt         newPetscObjectsMaxCounts,i;
 32: #endif

 35:   h->classid               = classid;
 36:   h->type                  = 0;
 37:   h->class_name            = (char*)class_name;
 38:   h->description           = (char*)descr;
 39:   h->mansec                = (char*)mansec;
 40:   h->prefix                = 0;
 41:   h->refct                 = 1;
 42: #if defined(PETSC_HAVE_SAWS)
 43:   h->amsmem                = PETSC_FALSE;
 44: #endif
 45:   h->id                    = idcnt++;
 46:   h->parentid              = 0;
 47:   h->qlist                 = 0;
 48:   h->olist                 = 0;
 49:   h->bops->destroy         = destroy;
 50:   h->bops->view            = view;
 51:   h->bops->getcomm         = PetscObjectGetComm_Petsc;
 52:   h->bops->compose         = PetscObjectCompose_Petsc;
 53:   h->bops->query           = PetscObjectQuery_Petsc;
 54:   h->bops->composefunction = PetscObjectComposeFunction_Petsc;
 55:   h->bops->queryfunction   = PetscObjectQueryFunction_Petsc;

 57:   PetscCommDuplicate(comm,&h->comm,&h->tag);

 59: #if defined(PETSC_USE_LOG)
 60:   /* Keep a record of object created */
 61:   if (PetscObjectsLog) {
 62:     PetscObjectsCounts++;
 63:     for (i=0; i<PetscObjectsMaxCounts; i++) {
 64:       if (!PetscObjects[i]) {
 65:         PetscObjects[i] = h;
 66:         return(0);
 67:       }
 68:     }
 69:     /* Need to increase the space for storing PETSc objects */
 70:     if (!PetscObjectsMaxCounts) newPetscObjectsMaxCounts = 100;
 71:     else                        newPetscObjectsMaxCounts = 2*PetscObjectsMaxCounts;
 72:     PetscMalloc1(newPetscObjectsMaxCounts,&newPetscObjects);
 73:     PetscMemcpy(newPetscObjects,PetscObjects,PetscObjectsMaxCounts*sizeof(PetscObject));
 74:     PetscMemzero(newPetscObjects+PetscObjectsMaxCounts,(newPetscObjectsMaxCounts - PetscObjectsMaxCounts)*sizeof(PetscObject));
 75:     PetscFree(PetscObjects);

 77:     PetscObjects                        = newPetscObjects;
 78:     PetscObjects[PetscObjectsMaxCounts] = h;
 79:     PetscObjectsMaxCounts               = newPetscObjectsMaxCounts;
 80:   }
 81: #endif
 82:   return(0);
 83: }

 85: extern PetscBool      PetscMemoryCollectMaximumUsage;
 86: extern PetscLogDouble PetscMemoryMaximumUsage;

 88: /*
 89:     PetscHeaderDestroy_Private - Destroys a base PETSc object header. Called by
 90:     the macro PetscHeaderDestroy().
 91: */
 92: PetscErrorCode  PetscHeaderDestroy_Private(PetscObject h)
 93: {

 98:   PetscLogObjectDestroy(h);
 99:   PetscComposedQuantitiesDestroy(h);
100:   if (PetscMemoryCollectMaximumUsage) {
101:     PetscLogDouble usage;
102:     PetscMemoryGetCurrentUsage(&usage);
103:     if (usage > PetscMemoryMaximumUsage) PetscMemoryMaximumUsage = usage;
104:   }
105:   /* first destroy things that could execute arbitrary code */
106:   if (h->python_destroy) {
107:     void           *python_context = h->python_context;
108:     PetscErrorCode (*python_destroy)(void*) = h->python_destroy;
109:     h->python_context = 0;
110:     h->python_destroy = 0;

112:     (*python_destroy)(python_context);
113:   }
114:   PetscObjectDestroyOptionsHandlers(h);
115:   PetscObjectListDestroy(&h->olist);
116:   PetscCommDestroy(&h->comm);
117:   /* next destroy other things */
118:   h->classid = PETSCFREEDHEADER;

120:   PetscFunctionListDestroy(&h->qlist);
121:   PetscFree(h->type_name);
122:   PetscFree(h->name);
123:   PetscFree(h->prefix);
124:   PetscFree(h->fortran_func_pointers);
125:   PetscFree(h->fortrancallback[PETSC_FORTRAN_CALLBACK_CLASS]);
126:   PetscFree(h->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]);

128: #if defined(PETSC_USE_LOG)
129:   if (PetscObjectsLog) {
130:     PetscInt i;
131:     /* Record object removal from list of all objects */
132:     for (i=0; i<PetscObjectsMaxCounts; i++) {
133:       if (PetscObjects[i] == h) {
134:         PetscObjects[i] = 0;
135:         PetscObjectsCounts--;
136:         break;
137:       }
138:     }
139:     if (!PetscObjectsCounts) {
140:       PetscFree(PetscObjects);
141:       PetscObjectsMaxCounts = 0;
142:     }
143:   }
144: #endif
145:   return(0);
146: }

148: /*@C
149:    PetscObjectCopyFortranFunctionPointers - Copy function pointers to another object

151:    Logically Collective on PetscObject

153:    Input Parameter:
154: +  src - source object
155: -  dest - destination object

157:    Level: developer

159:    Note:
160:    Both objects must have the same class.
161: @*/
162: PetscErrorCode PetscObjectCopyFortranFunctionPointers(PetscObject src,PetscObject dest)
163: {
165:   PetscInt       cbtype,numcb[PETSC_FORTRAN_CALLBACK_MAXTYPE];

170:   if (src->classid != dest->classid) SETERRQ(src->comm,PETSC_ERR_ARG_INCOMP,"Objects must be of the same class");

172:   PetscFree(dest->fortran_func_pointers);
173:   PetscMalloc(src->num_fortran_func_pointers*sizeof(void(*)(void)),&dest->fortran_func_pointers);
174:   PetscMemcpy(dest->fortran_func_pointers,src->fortran_func_pointers,src->num_fortran_func_pointers*sizeof(void(*)(void)));

176:   dest->num_fortran_func_pointers = src->num_fortran_func_pointers;

178:   PetscFortranCallbackGetSizes(src->classid,&numcb[PETSC_FORTRAN_CALLBACK_CLASS],&numcb[PETSC_FORTRAN_CALLBACK_SUBTYPE]);
179:   for (cbtype=PETSC_FORTRAN_CALLBACK_CLASS; cbtype<PETSC_FORTRAN_CALLBACK_MAXTYPE; cbtype++) {
180:     PetscFree(dest->fortrancallback[cbtype]);
181:     PetscCalloc1(numcb[cbtype],&dest->fortrancallback[cbtype]);
182:     PetscMemcpy(dest->fortrancallback[cbtype],src->fortrancallback[cbtype],src->num_fortrancallback[cbtype]*sizeof(PetscFortranCallback));
183:   }
184:   return(0);
185: }

187: /*@C
188:    PetscObjectSetFortranCallback - set fortran callback function pointer and context

190:    Logically Collective

192:    Input Arguments:
193: +  obj - object on which to set callback
194: .  cbtype - callback type (class or subtype)
195: .  cid - address of callback Id, updated if not yet initialized (zero)
196: .  func - Fortran function
197: -  ctx - Fortran context

199:    Level: developer

201: .seealso: PetscObjectGetFortranCallback()
202: @*/
203: PetscErrorCode PetscObjectSetFortranCallback(PetscObject obj,PetscFortranCallbackType cbtype,PetscFortranCallbackId *cid,void (*func)(void),void *ctx)
204: {
206:   const char     *subtype = NULL;

210:   if (cbtype == PETSC_FORTRAN_CALLBACK_SUBTYPE) subtype = obj->type_name;
211:   if (!*cid) {PetscFortranCallbackRegister(obj->classid,subtype,cid);}
212:   if (*cid >= PETSC_SMALLEST_FORTRAN_CALLBACK+obj->num_fortrancallback[cbtype]) {
213:     PetscInt             oldnum = obj->num_fortrancallback[cbtype],newnum = PetscMax(1,2*oldnum);
214:     PetscFortranCallback *callback;
215:     PetscMalloc1(newnum,&callback);
216:     PetscMemcpy(callback,obj->fortrancallback[cbtype],oldnum*sizeof(*obj->fortrancallback[cbtype]));
217:     PetscFree(obj->fortrancallback[cbtype]);

219:     obj->fortrancallback[cbtype] = callback;
220:     obj->num_fortrancallback[cbtype] = newnum;
221:   }
222:   obj->fortrancallback[cbtype][*cid-PETSC_SMALLEST_FORTRAN_CALLBACK].func = func;
223:   obj->fortrancallback[cbtype][*cid-PETSC_SMALLEST_FORTRAN_CALLBACK].ctx = ctx;
224:   return(0);
225: }

227: /*@C
228:    PetscObjectGetFortranCallback - get fortran callback function pointer and context

230:    Logically Collective

232:    Input Arguments:
233: +  obj - object on which to get callback
234: .  cbtype - callback type
235: -  cid - address of callback Id

237:    Output Arguments:
238: +  func - Fortran function (or NULL if not needed)
239: -  ctx - Fortran context (or NULL if not needed)

241:    Level: developer

243: .seealso: PetscObjectSetFortranCallback()
244: @*/
245: PetscErrorCode PetscObjectGetFortranCallback(PetscObject obj,PetscFortranCallbackType cbtype,PetscFortranCallbackId cid,void (**func)(void),void **ctx)
246: {
247:   PetscFortranCallback *cb;

251:   if (PetscUnlikely(cid < PETSC_SMALLEST_FORTRAN_CALLBACK)) SETERRQ(obj->comm,PETSC_ERR_ARG_CORRUPT,"Fortran callback Id invalid");
252:   if (PetscUnlikely(cid >= PETSC_SMALLEST_FORTRAN_CALLBACK+obj->num_fortrancallback[cbtype])) SETERRQ(obj->comm,PETSC_ERR_ARG_CORRUPT,"Fortran callback not set on this object");
253:   cb = &obj->fortrancallback[cbtype][cid-PETSC_SMALLEST_FORTRAN_CALLBACK];
254:   if (func) *func = cb->func;
255:   if (ctx) *ctx = cb->ctx;
256:   return(0);
257: }

259: #if defined(PETSC_USE_LOG)
260: /*@C
261:    PetscObjectsDump - Prints the currently existing objects.

263:    Logically Collective on PetscViewer

265:    Input Parameter:
266: +  fd - file pointer
267: -  all - by default only tries to display objects created explicitly by the user, if all is PETSC_TRUE then lists all outstanding objects

269:    Options Database:
270: .  -objects_dump <all>

272:    Level: advanced

274:    Concepts: options database^printing

276: @*/
277: PetscErrorCode  PetscObjectsDump(FILE *fd,PetscBool all)
278: {
280:   PetscInt       i;
281: #if defined(PETSC_USE_DEBUG)
282:   PetscInt       j,k=0;
283: #endif
284:   PetscObject    h;

287:   if (PetscObjectsCounts) {
288:     PetscFPrintf(PETSC_COMM_WORLD,fd,"The following objects were never freed\n");
289:     PetscFPrintf(PETSC_COMM_WORLD,fd,"-----------------------------------------\n");
290:     for (i=0; i<PetscObjectsMaxCounts; i++) {
291:       if ((h = PetscObjects[i])) {
292:         PetscObjectName(h);
293:         {
294: #if defined(PETSC_USE_DEBUG)
295:         PetscStack *stack = 0;
296:         char       *create,*rclass;

298:         /* if the PETSc function the user calls is not a create then this object was NOT directly created by them */
299:         PetscMallocGetStack(h,&stack);
300:         if (stack) {
301:           k = stack->currentsize-2;
302:           if (!all) {
303:             k = 0;
304:             while (!stack->petscroutine[k]) k++;
305:             PetscStrstr(stack->function[k],"Create",&create);
306:             if (!create) {
307:               PetscStrstr(stack->function[k],"Get",&create);
308:             }
309:             PetscStrstr(stack->function[k],h->class_name,&rclass);
310:             if (!create) continue;
311:             if (!rclass) continue;
312:           }
313:         }
314: #endif

316:         PetscFPrintf(PETSC_COMM_WORLD,fd,"[%d] %s %s %s\n",PetscGlobalRank,h->class_name,h->type_name,h->name);

318: #if defined(PETSC_USE_DEBUG)
319:         PetscMallocGetStack(h,&stack);
320:         if (stack) {
321:           for (j=k; j>=0; j--) {
322:             fprintf(fd,"      [%d]  %s() in %s\n",PetscGlobalRank,stack->function[j],stack->file[j]);
323:           }
324:         }
325: #endif
326:         }
327:       }
328:     }
329:   }
330:   return(0);
331: }
332: #endif

334: #if defined(PETSC_USE_LOG)

336: /*@C
337:    PetscObjectsView - Prints the currently existing objects.

339:    Logically Collective on PetscViewer

341:    Input Parameter:
342: .  viewer - must be an PETSCVIEWERASCII viewer

344:    Level: advanced

346:    Concepts: options database^printing

348: @*/
349: PetscErrorCode  PetscObjectsView(PetscViewer viewer)
350: {
352:   PetscBool      isascii;
353:   FILE           *fd;

356:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_WORLD;
357:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
358:   if (!isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Only supports ASCII viewer");
359:   PetscViewerASCIIGetPointer(viewer,&fd);
360:   PetscObjectsDump(fd,PETSC_TRUE);
361:   return(0);
362: }

364: /*@C
365:    PetscObjectsGetObject - Get a pointer to a named object

367:    Not collective

369:    Input Parameter:
370: .  name - the name of an object

372:    Output Parameter:
373: .   obj - the object or null if there is no object

375:    Level: advanced

377:    Concepts: options database^printing

379: @*/
380: PetscErrorCode  PetscObjectsGetObject(const char *name,PetscObject *obj,char **classname)
381: {
383:   PetscInt       i;
384:   PetscObject    h;
385:   PetscBool      flg;

388:   *obj = NULL;
389:   for (i=0; i<PetscObjectsMaxCounts; i++) {
390:     if ((h = PetscObjects[i])) {
391:       PetscObjectName(h);
392:       PetscStrcmp(h->name,name,&flg);
393:       if (flg) {
394:         *obj = h;
395:         if (classname) *classname = h->class_name;
396:         return(0);
397:       }
398:     }
399:   }
400:   return(0);
401: }
402: #endif

404: /*@C
405:     PetscObjectAddOptionsHandler - Adds an additional function to check for options when XXXSetFromOptions() is called.

407:     Not Collective

409:     Input Parameter:
410: +   obj - the PETSc object
411: .   handle - function that checks for options
412: .   destroy - function to destroy context if provided
413: -   ctx - optional context for check function

415:     Level: developer


418: .seealso: KSPSetFromOptions(), PCSetFromOptions(), SNESSetFromOptions(), PetscObjectProcessOptionsHandlers(), PetscObjectDestroyOptionsHandlers()

420: @*/
421: PetscErrorCode PetscObjectAddOptionsHandler(PetscObject obj,PetscErrorCode (*handle)(PetscOptionItems*,PetscObject,void*),PetscErrorCode (*destroy)(PetscObject,void*),void *ctx)
422: {
425:   if (obj->noptionhandler >= PETSC_MAX_OPTIONS_HANDLER) SETERRQ(obj->comm,PETSC_ERR_ARG_OUTOFRANGE,"To many options handlers added");
426:   obj->optionhandler[obj->noptionhandler] = handle;
427:   obj->optiondestroy[obj->noptionhandler] = destroy;
428:   obj->optionctx[obj->noptionhandler++]   = ctx;
429:   return(0);
430: }

432: /*@C
433:     PetscObjectProcessOptionsHandlers - Calls all the options handlers attached to an object

435:     Not Collective

437:     Input Parameter:
438: .   obj - the PETSc object

440:     Level: developer


443: .seealso: KSPSetFromOptions(), PCSetFromOptions(), SNESSetFromOptions(), PetscObjectAddOptionsHandler(), PetscObjectDestroyOptionsHandlers()

445: @*/
446: PetscErrorCode  PetscObjectProcessOptionsHandlers(PetscOptionItems *PetscOptionsObject,PetscObject obj)
447: {
448:   PetscInt       i;

453:   for (i=0; i<obj->noptionhandler; i++) {
454:     (*obj->optionhandler[i])(PetscOptionsObject,obj,obj->optionctx[i]);
455:   }
456:   return(0);
457: }

459: /*@C
460:     PetscObjectDestroyOptionsHandlers - Destroys all the option handlers attached to an object

462:     Not Collective

464:     Input Parameter:
465: .   obj - the PETSc object

467:     Level: developer


470: .seealso: KSPSetFromOptions(), PCSetFromOptions(), SNESSetFromOptions(), PetscObjectAddOptionsHandler(), PetscObjectProcessOptionsHandlers()

472: @*/
473: PetscErrorCode  PetscObjectDestroyOptionsHandlers(PetscObject obj)
474: {
475:   PetscInt       i;

480:   for (i=0; i<obj->noptionhandler; i++) {
481:     if (obj->optiondestroy[i]) {
482:       (*obj->optiondestroy[i])(obj,obj->optionctx[i]);
483:     }
484:   }
485:   obj->noptionhandler = 0;
486:   return(0);
487: }


490: /*@
491:    PetscObjectReference - Indicates to any PetscObject that it is being
492:    referenced by another PetscObject. This increases the reference
493:    count for that object by one.

495:    Logically Collective on PetscObject

497:    Input Parameter:
498: .  obj - the PETSc object. This must be cast with (PetscObject), for example,
499:          PetscObjectReference((PetscObject)mat);

501:    Level: advanced

503: .seealso: PetscObjectCompose(), PetscObjectDereference()
504: @*/
505: PetscErrorCode  PetscObjectReference(PetscObject obj)
506: {
508:   if (!obj) return(0);
510:   obj->refct++;
511:   return(0);
512: }

514: /*@
515:    PetscObjectGetReference - Gets the current reference count for
516:    any PETSc object.

518:    Not Collective

520:    Input Parameter:
521: .  obj - the PETSc object; this must be cast with (PetscObject), for example,
522:          PetscObjectGetReference((PetscObject)mat,&cnt);

524:    Output Parameter:
525: .  cnt - the reference count

527:    Level: advanced

529: .seealso: PetscObjectCompose(), PetscObjectDereference(), PetscObjectReference()
530: @*/
531: PetscErrorCode  PetscObjectGetReference(PetscObject obj,PetscInt *cnt)
532: {
536:   *cnt = obj->refct;
537:   return(0);
538: }

540: /*@
541:    PetscObjectDereference - Indicates to any PetscObject that it is being
542:    referenced by one less PetscObject. This decreases the reference
543:    count for that object by one.

545:    Collective on PetscObject if reference reaches 0 otherwise Logically Collective

547:    Input Parameter:
548: .  obj - the PETSc object; this must be cast with (PetscObject), for example,
549:          PetscObjectDereference((PetscObject)mat);

551:    Notes: PetscObjectDestroy(PetscObject *obj)  sets the obj pointer to null after the call, this routine does not.

553:    Level: advanced

555: .seealso: PetscObjectCompose(), PetscObjectReference()
556: @*/
557: PetscErrorCode  PetscObjectDereference(PetscObject obj)
558: {

562:   if (!obj) return(0);
564:   if (obj->bops->destroy) {
565:     (*obj->bops->destroy)(&obj);
566:   } else if (!--obj->refct) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"This PETSc object does not have a generic destroy routine");
567:   return(0);
568: }

570: /* ----------------------------------------------------------------------- */
571: /*
572:      The following routines are the versions private to the PETSc object
573:      data structures.
574: */
575: PetscErrorCode PetscObjectGetComm_Petsc(PetscObject obj,MPI_Comm *comm)
576: {
579:   *comm = obj->comm;
580:   return(0);
581: }

583: PetscErrorCode PetscObjectRemoveReference(PetscObject obj,const char name[])
584: {

589:   PetscObjectListRemoveReference(&obj->olist,name);
590:   return(0);
591: }

593: PetscErrorCode PetscObjectCompose_Petsc(PetscObject obj,const char name[],PetscObject ptr)
594: {
596:   char           *tname;
597:   PetscBool      skipreference;

600:   if (ptr) {
601:     PetscObjectListReverseFind(ptr->olist,obj,&tname,&skipreference);
602:     if (tname && !skipreference) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"An object cannot be composed with an object that was composed with it");
603:   }
604:   PetscObjectListAdd(&obj->olist,name,ptr);
605:   return(0);
606: }

608: PetscErrorCode PetscObjectQuery_Petsc(PetscObject obj,const char name[],PetscObject *ptr)
609: {

614:   PetscObjectListFind(obj->olist,name,ptr);
615:   return(0);
616: }

618: PetscErrorCode PetscObjectComposeFunction_Petsc(PetscObject obj,const char name[],void (*ptr)(void))
619: {

624:   PetscFunctionListAdd(&obj->qlist,name,ptr);
625:   return(0);
626: }

628: PetscErrorCode PetscObjectQueryFunction_Petsc(PetscObject obj,const char name[],void (**ptr)(void))
629: {

634:   PetscFunctionListFind(obj->qlist,name,ptr);
635:   return(0);
636: }

638: /*@C
639:    PetscObjectCompose - Associates another PETSc object with a given PETSc object.

641:    Not Collective

643:    Input Parameters:
644: +  obj - the PETSc object; this must be cast with (PetscObject), for example,
645:          PetscObjectCompose((PetscObject)mat,...);
646: .  name - name associated with the child object
647: -  ptr - the other PETSc object to associate with the PETSc object; this must also be
648:          cast with (PetscObject)

650:    Level: advanced

652:    Notes:
653:    The second objects reference count is automatically increased by one when it is
654:    composed.

656:    Replaces any previous object that had the same name.

658:    If ptr is null and name has previously been composed using an object, then that
659:    entry is removed from the obj.

661:    PetscObjectCompose() can be used with any PETSc object (such as
662:    Mat, Vec, KSP, SNES, etc.) or any user-provided object.  See
663:    PetscContainerCreate() for info on how to create an object from a
664:    user-provided pointer that may then be composed with PETSc objects.

666:    Concepts: objects^composing
667:    Concepts: composing objects

669: .seealso: PetscObjectQuery(), PetscContainerCreate()
670: @*/
671: PetscErrorCode  PetscObjectCompose(PetscObject obj,const char name[],PetscObject ptr)
672: {

679:   (*obj->bops->compose)(obj,name,ptr);
680:   return(0);
681: }

683: /*@C
684:    PetscObjectQuery  - Gets a PETSc object associated with a given object.

686:    Not Collective

688:    Input Parameters:
689: +  obj - the PETSc object
690:          Thus must be cast with a (PetscObject), for example,
691:          PetscObjectCompose((PetscObject)mat,...);
692: .  name - name associated with child object
693: -  ptr - the other PETSc object associated with the PETSc object, this must be
694:          cast with (PetscObject*)

696:    Level: advanced

698:    The reference count of neither object is increased in this call

700:    Concepts: objects^composing
701:    Concepts: composing objects
702:    Concepts: objects^querying
703:    Concepts: querying objects

705: .seealso: PetscObjectCompose()
706: @*/
707: PetscErrorCode  PetscObjectQuery(PetscObject obj,const char name[],PetscObject *ptr)
708: {

715:   (*obj->bops->query)(obj,name,ptr);
716:   return(0);
717: }

719: /*MC
720:    PetscObjectComposeFunction - Associates a function with a given PETSc object.

722:     Synopsis:
723:     #include <petscsys.h>
724:     PetscErrorCode PetscObjectComposeFunction(PetscObject obj,const char name[],void (*fptr)(void))

726:    Logically Collective on PetscObject

728:    Input Parameters:
729: +  obj - the PETSc object; this must be cast with a (PetscObject), for example,
730:          PetscObjectCompose((PetscObject)mat,...);
731: .  name - name associated with the child function
732: .  fname - name of the function
733: -  fptr - function pointer

735:    Level: advanced

737:    Notes:
738:    To remove a registered routine, pass in NULL for fptr().

740:    PetscObjectComposeFunction() can be used with any PETSc object (such as
741:    Mat, Vec, KSP, SNES, etc.) or any user-provided object.

743:    Concepts: objects^composing functions
744:    Concepts: composing functions
745:    Concepts: functions^querying
746:    Concepts: objects^querying
747:    Concepts: querying objects

749: .seealso: PetscObjectQueryFunction(), PetscContainerCreate()
750: M*/

752: PetscErrorCode  PetscObjectComposeFunction_Private(PetscObject obj,const char name[],void (*fptr)(void))
753: {

759:   (*obj->bops->composefunction)(obj,name,fptr);
760:   return(0);
761: }

763: /*MC
764:    PetscObjectQueryFunction - Gets a function associated with a given object.

766:     Synopsis:
767:     #include <petscsys.h>
768:     PetscErrorCode PetscObjectQueryFunction(PetscObject obj,const char name[],void (**fptr)(void))

770:    Logically Collective on PetscObject

772:    Input Parameters:
773: +  obj - the PETSc object; this must be cast with (PetscObject), for example,
774:          PetscObjectQueryFunction((PetscObject)ksp,...);
775: -  name - name associated with the child function

777:    Output Parameter:
778: .  fptr - function pointer

780:    Level: advanced

782:    Concepts: objects^composing functions
783:    Concepts: composing functions
784:    Concepts: functions^querying
785:    Concepts: objects^querying
786:    Concepts: querying objects

788: .seealso: PetscObjectComposeFunction(), PetscFunctionListFind()
789: M*/
790: PETSC_EXTERN PetscErrorCode PetscObjectQueryFunction_Private(PetscObject obj,const char name[],void (**ptr)(void))
791: {

797:   (*obj->bops->queryfunction)(obj,name,ptr);
798:   return(0);
799: }

801: struct _p_PetscContainer {
802:   PETSCHEADER(int);
803:   void           *ptr;
804:   PetscErrorCode (*userdestroy)(void*);
805: };

807: /*@C
808:    PetscContainerGetPointer - Gets the pointer value contained in the container.

810:    Not Collective

812:    Input Parameter:
813: .  obj - the object created with PetscContainerCreate()

815:    Output Parameter:
816: .  ptr - the pointer value

818:    Level: advanced

820: .seealso: PetscContainerCreate(), PetscContainerDestroy(),
821:           PetscContainerSetPointer()
822: @*/
823: PetscErrorCode  PetscContainerGetPointer(PetscContainer obj,void **ptr)
824: {
828:   *ptr = obj->ptr;
829:   return(0);
830: }


833: /*@C
834:    PetscContainerSetPointer - Sets the pointer value contained in the container.

836:    Logically Collective on PetscContainer

838:    Input Parameters:
839: +  obj - the object created with PetscContainerCreate()
840: -  ptr - the pointer value

842:    Level: advanced

844: .seealso: PetscContainerCreate(), PetscContainerDestroy(),
845:           PetscContainerGetPointer()
846: @*/
847: PetscErrorCode  PetscContainerSetPointer(PetscContainer obj,void *ptr)
848: {
852:   obj->ptr = ptr;
853:   return(0);
854: }

856: /*@C
857:    PetscContainerDestroy - Destroys a PETSc container object.

859:    Collective on PetscContainer

861:    Input Parameter:
862: .  obj - an object that was created with PetscContainerCreate()

864:    Level: advanced

866: .seealso: PetscContainerCreate(), PetscContainerSetUserDestroy()
867: @*/
868: PetscErrorCode  PetscContainerDestroy(PetscContainer *obj)
869: {

873:   if (!*obj) return(0);
875:   if (--((PetscObject)(*obj))->refct > 0) {*obj = 0; return(0);}
876:   if ((*obj)->userdestroy) (*(*obj)->userdestroy)((*obj)->ptr);
877:   PetscHeaderDestroy(obj);
878:   return(0);
879: }

881: /*@C
882:    PetscContainerSetUserDestroy - Sets name of the user destroy function.

884:    Logically Collective on PetscContainer

886:    Input Parameter:
887: +  obj - an object that was created with PetscContainerCreate()
888: -  des - name of the user destroy function

890:    Level: advanced

892: .seealso: PetscContainerDestroy()
893: @*/
894: PetscErrorCode  PetscContainerSetUserDestroy(PetscContainer obj, PetscErrorCode (*des)(void*))
895: {
898:   obj->userdestroy = des;
899:   return(0);
900: }

902: PetscClassId PETSC_CONTAINER_CLASSID;

904: /*@C
905:    PetscContainerCreate - Creates a PETSc object that has room to hold
906:    a single pointer. This allows one to attach any type of data (accessible
907:    through a pointer) with the PetscObjectCompose() function to a PetscObject.
908:    The data item itself is attached by a call to PetscContainerSetPointer().

910:    Collective on MPI_Comm

912:    Input Parameters:
913: .  comm - MPI communicator that shares the object

915:    Output Parameters:
916: .  container - the container created

918:    Level: advanced

920: .seealso: PetscContainerDestroy(), PetscContainerSetPointer(), PetscContainerGetPointer()
921: @*/
922: PetscErrorCode  PetscContainerCreate(MPI_Comm comm,PetscContainer *container)
923: {
925:   PetscContainer contain;

929:   PetscSysInitializePackage();
930:   PetscHeaderCreate(contain,PETSC_CONTAINER_CLASSID,"PetscContainer","Container","Sys",comm,PetscContainerDestroy,NULL);
931:   *container = contain;
932:   return(0);
933: }

935: /*@
936:    PetscObjectSetFromOptions - Sets generic parameters from user options.

938:    Collective on obj

940:    Input Parameter:
941: .  obj - the PetscObjcet

943:    Options Database Keys:

945:    Notes:
946:    We have no generic options at present, so this does nothing

948:    Level: beginner

950: .keywords: set, options, database
951: .seealso: PetscObjectSetOptionsPrefix(), PetscObjectGetOptionsPrefix()
952: @*/
953: PetscErrorCode  PetscObjectSetFromOptions(PetscObject obj)
954: {
957:   return(0);
958: }

960: /*@
961:    PetscObjectSetUp - Sets up the internal data structures for the later use.

963:    Collective on PetscObject

965:    Input Parameters:
966: .  obj - the PetscObject

968:    Notes:
969:    This does nothing at present.

971:    Level: advanced

973: .keywords: setup
974: .seealso: PetscObjectDestroy()
975: @*/
976: PetscErrorCode  PetscObjectSetUp(PetscObject obj)
977: {
980:   return(0);
981: }