17d0a6c19SBarry Smith 2e5c89e4eSSatish Balay /* 3e5c89e4eSSatish Balay Provides utility routines for manulating any type of PETSc object. 4e5c89e4eSSatish Balay */ 5af0996ceSBarry Smith #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/ 6665c2dedSJed Brown #include <petscviewer.h> 7e5c89e4eSSatish Balay 89371c9d4SSatish Balay PetscErrorCode PetscComposedQuantitiesDestroy(PetscObject obj) { 9d42688cbSBarry Smith PetscInt i; 10d42688cbSBarry Smith 11b436f76fSVictor Eijkhout PetscFunctionBegin; 12b436f76fSVictor Eijkhout if (obj->intstar_idmax > 0) { 139566063dSJacob Faibussowitsch for (i = 0; i < obj->intstar_idmax; i++) PetscCall(PetscFree(obj->intstarcomposeddata[i])); 149566063dSJacob Faibussowitsch PetscCall(PetscFree2(obj->intstarcomposeddata, obj->intstarcomposedstate)); 15b436f76fSVictor Eijkhout } 16b436f76fSVictor Eijkhout if (obj->realstar_idmax > 0) { 179566063dSJacob Faibussowitsch for (i = 0; i < obj->realstar_idmax; i++) PetscCall(PetscFree(obj->realstarcomposeddata[i])); 189566063dSJacob Faibussowitsch PetscCall(PetscFree2(obj->realstarcomposeddata, obj->realstarcomposedstate)); 19b436f76fSVictor Eijkhout } 20b436f76fSVictor Eijkhout if (obj->scalarstar_idmax > 0) { 219566063dSJacob Faibussowitsch for (i = 0; i < obj->scalarstar_idmax; i++) PetscCall(PetscFree(obj->scalarstarcomposeddata[i])); 229566063dSJacob Faibussowitsch PetscCall(PetscFree2(obj->scalarstarcomposeddata, obj->scalarstarcomposedstate)); 23b436f76fSVictor Eijkhout } 249566063dSJacob Faibussowitsch PetscCall(PetscFree2(obj->intcomposeddata, obj->intcomposedstate)); 259566063dSJacob Faibussowitsch PetscCall(PetscFree2(obj->realcomposeddata, obj->realcomposedstate)); 269566063dSJacob Faibussowitsch PetscCall(PetscFree2(obj->scalarcomposeddata, obj->scalarcomposedstate)); 27b436f76fSVictor Eijkhout PetscFunctionReturn(0); 28b436f76fSVictor Eijkhout } 29b436f76fSVictor Eijkhout 30e30d2299SSatish Balay /*@ 31811af0c4SBarry Smith PetscObjectDestroy - Destroys any `PetscObject`, regardless of the type. 32e5c89e4eSSatish Balay 33811af0c4SBarry Smith Collective on obj 34e5c89e4eSSatish Balay 35e5c89e4eSSatish Balay Input Parameter: 36811af0c4SBarry Smith . obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. 37811af0c4SBarry Smith This must be cast with a (`PetscObject`*), for example, 38811af0c4SBarry Smith `PetscObjectDestroy`((`PetscObject`*)&mat); 39e5c89e4eSSatish Balay 40e5c89e4eSSatish Balay Level: beginner 41e5c89e4eSSatish Balay 42811af0c4SBarry Smith .seealso: `PetscObject` 43e5c89e4eSSatish Balay @*/ 449371c9d4SSatish Balay PetscErrorCode PetscObjectDestroy(PetscObject *obj) { 45e5c89e4eSSatish Balay PetscFunctionBegin; 465f80ce2aSJacob Faibussowitsch if (!obj || !*obj) PetscFunctionReturn(0); 476bf464f9SBarry Smith PetscValidHeader(*obj, 1); 485f80ce2aSJacob Faibussowitsch PetscCheck((*obj)->bops->destroy, PETSC_COMM_SELF, PETSC_ERR_PLIB, "This PETSc object of class %s does not have a generic destroy routine", (*obj)->class_name); 499566063dSJacob Faibussowitsch PetscCall((*(*obj)->bops->destroy)(obj)); 50e5c89e4eSSatish Balay PetscFunctionReturn(0); 51e5c89e4eSSatish Balay } 52e5c89e4eSSatish Balay 53e5c89e4eSSatish Balay /*@C 54811af0c4SBarry Smith PetscObjectView - Views any `PetscObject`, regardless of the type. 55e5c89e4eSSatish Balay 56811af0c4SBarry Smith Collective on obj 57e5c89e4eSSatish Balay 58e5c89e4eSSatish Balay Input Parameters: 59811af0c4SBarry Smith + obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. 60811af0c4SBarry Smith This must be cast with a (`PetscObject`), for example, 61811af0c4SBarry Smith `PetscObjectView`((`PetscObject`)mat,viewer); 62e5c89e4eSSatish Balay - viewer - any PETSc viewer 63e5c89e4eSSatish Balay 64e5c89e4eSSatish Balay Level: intermediate 65e5c89e4eSSatish Balay 66811af0c4SBarry Smith .seealso: `PetscObject`, `PetscObjectViewFromOptions()` 67e5c89e4eSSatish Balay @*/ 689371c9d4SSatish Balay PetscErrorCode PetscObjectView(PetscObject obj, PetscViewer viewer) { 69e5c89e4eSSatish Balay PetscFunctionBegin; 70e5c89e4eSSatish Balay PetscValidHeader(obj, 1); 715f80ce2aSJacob Faibussowitsch PetscCheck(obj->bops->view, PETSC_COMM_SELF, PETSC_ERR_SUP, "This PETSc object does not have a generic viewer routine"); 729566063dSJacob Faibussowitsch if (!viewer) PetscCall(PetscViewerASCIIGetStdout(obj->comm, &viewer)); 730700a824SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 74e5c89e4eSSatish Balay 759566063dSJacob Faibussowitsch PetscCall((*obj->bops->view)(obj, viewer)); 76e5c89e4eSSatish Balay PetscFunctionReturn(0); 77e5c89e4eSSatish Balay } 78e5c89e4eSSatish Balay 792d747510SLisandro Dalcin /*@C 80811af0c4SBarry Smith PetscObjectViewFromOptions - Processes command line options to determine if/how a `PetscObject` is to be viewed. 812d747510SLisandro Dalcin 82811af0c4SBarry Smith Collective on obj 832d747510SLisandro Dalcin 842d747510SLisandro Dalcin Input Parameters: 852d747510SLisandro Dalcin + obj - the object 862d747510SLisandro Dalcin . bobj - optional other object that provides prefix (if NULL then the prefix in obj is used) 87bb7acecfSBarry Smith - optionname - option string that is used to activate viewing 882d747510SLisandro Dalcin 8911a5261eSBarry Smith Options Database Key: 9011a5261eSBarry Smith . -optionname_view [viewertype]:... - option name and values. In actual usage this would be something like -mat_coarse_view 912d747510SLisandro Dalcin 9211a5261eSBarry Smith Notes: 9311a5261eSBarry Smith .vb 9411a5261eSBarry Smith If no value is provided ascii:stdout is used 9511a5261eSBarry Smith ascii[:[filename][:[format][:append]]] defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab, 9611a5261eSBarry Smith for example ascii::ascii_info prints just the information about the object not all details 9711a5261eSBarry Smith unless :append is given filename opens in write mode, overwriting what was already there 9811a5261eSBarry Smith binary[:[filename][:[format][:append]]] defaults to the file binaryoutput 9911a5261eSBarry Smith draw[:drawtype[:filename]] for example, draw:tikz, draw:tikz:figure.tex or draw:x 10011a5261eSBarry Smith socket[:port] defaults to the standard output port 10111a5261eSBarry Smith saws[:communicatorname] publishes object to the Scientific Application Webserver (SAWs) 10211a5261eSBarry Smith .ve 10311a5261eSBarry Smith 10411a5261eSBarry Smith This is not called directly but is called by, for example, `MatCoarseViewFromOptions()` 10511a5261eSBarry Smith 10611a5261eSBarry Smith Level: developer 10711a5261eSBarry Smith 10811a5261eSBarry Smith .seealso: `PetscObject`, `PetscObjectView()`, `PetscOptionsGetViewer()` 1092d747510SLisandro Dalcin @*/ 1109371c9d4SSatish Balay PetscErrorCode PetscObjectViewFromOptions(PetscObject obj, PetscObject bobj, const char optionname[]) { 1112d747510SLisandro Dalcin PetscViewer viewer; 1122d747510SLisandro Dalcin PetscBool flg; 1132d747510SLisandro Dalcin static PetscBool incall = PETSC_FALSE; 1142d747510SLisandro Dalcin PetscViewerFormat format; 1152d747510SLisandro Dalcin const char *prefix; 1162d747510SLisandro Dalcin 1172d747510SLisandro Dalcin PetscFunctionBegin; 1182d747510SLisandro Dalcin if (incall) PetscFunctionReturn(0); 1192d747510SLisandro Dalcin incall = PETSC_TRUE; 1202d747510SLisandro Dalcin prefix = bobj ? bobj->prefix : obj->prefix; 1219566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)obj), obj->options, prefix, optionname, &viewer, &format, &flg)); 1222d747510SLisandro Dalcin if (flg) { 1239566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 1249566063dSJacob Faibussowitsch PetscCall(PetscObjectView(obj, viewer)); 1259566063dSJacob Faibussowitsch PetscCall(PetscViewerFlush(viewer)); 1269566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 1279566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 1282d747510SLisandro Dalcin } 1292d747510SLisandro Dalcin incall = PETSC_FALSE; 1302d747510SLisandro Dalcin PetscFunctionReturn(0); 1312d747510SLisandro Dalcin } 1322d747510SLisandro Dalcin 133e5c89e4eSSatish Balay /*@C 134251f4c67SDmitry Karpeev PetscObjectTypeCompare - Determines whether a PETSc object is of a particular type. 135e5c89e4eSSatish Balay 136e5c89e4eSSatish Balay Not Collective 137e5c89e4eSSatish Balay 138e5c89e4eSSatish Balay Input Parameters: 139811af0c4SBarry Smith + obj - any PETSc object, for example a `Vec`, `Mat or `KSP`. 140811af0c4SBarry Smith This must be cast with a (`PetscObject`), for example, 141811af0c4SBarry Smith `PetscObjectTypeCompare`((`PetscObject`)mat); 142e5c89e4eSSatish Balay - type_name - string containing a type name 143e5c89e4eSSatish Balay 144e5c89e4eSSatish Balay Output Parameter: 145811af0c4SBarry Smith . same - `PETSC_TRUE` if they are the same, else `PETSC_FALSE` 146e5c89e4eSSatish Balay 147e5c89e4eSSatish Balay Level: intermediate 148e5c89e4eSSatish Balay 149*013e2dc7SBarry Smith .seealso: `PetscObject`, `VecGetType()`, `KSPGetType()`, `PCGetType()`, `SNESGetType()`, `PetscObjectBaseTypeCompare()`, `PetscObjectTypeCompareAny()`, `PetscObjectBaseTypeCompareAny()`, `PetscObjectObjectTypeCompare()` 150e5c89e4eSSatish Balay @*/ 1519371c9d4SSatish Balay PetscErrorCode PetscObjectTypeCompare(PetscObject obj, const char type_name[], PetscBool *same) { 152e5c89e4eSSatish Balay PetscFunctionBegin; 1535f80ce2aSJacob Faibussowitsch PetscValidBoolPointer(same, 3); 154a297a907SKarl Rupp if (!obj) *same = PETSC_FALSE; 155a297a907SKarl Rupp else if (!type_name && !obj->type_name) *same = PETSC_TRUE; 156a297a907SKarl Rupp else if (!type_name || !obj->type_name) *same = PETSC_FALSE; 157a297a907SKarl Rupp else { 158e5c89e4eSSatish Balay PetscValidHeader(obj, 1); 159e5c89e4eSSatish Balay PetscValidCharPointer(type_name, 2); 1609566063dSJacob Faibussowitsch PetscCall(PetscStrcmp((char *)(obj->type_name), type_name, same)); 161e5c89e4eSSatish Balay } 162e5c89e4eSSatish Balay PetscFunctionReturn(0); 163e5c89e4eSSatish Balay } 164e5c89e4eSSatish Balay 1652b12f010SJed Brown /*@C 166*013e2dc7SBarry Smith PetscObjectObjectTypeCompare - Determines whether two PETSc objects are of the same type 167*013e2dc7SBarry Smith 168*013e2dc7SBarry Smith Logically Collective 169*013e2dc7SBarry Smith 170*013e2dc7SBarry Smith Input Parameters: 171*013e2dc7SBarry Smith + obj1 - any PETSc object, for example a Vec, Mat or KSP. 172*013e2dc7SBarry Smith - obj2 - anther PETSc object 173*013e2dc7SBarry Smith 174*013e2dc7SBarry Smith Output Parameter: 175*013e2dc7SBarry Smith . same - PETSC_TRUE if they are the same, else PETSC_FALSE 176*013e2dc7SBarry Smith 177*013e2dc7SBarry Smith Level: intermediate 178*013e2dc7SBarry Smith 179*013e2dc7SBarry Smith .seealso: `PetscObjectTypeCompare()`, `VecGetType()`, `KSPGetType()`, `PCGetType()`, `SNESGetType()`, `PetscObjectBaseTypeCompare()`, `PetscObjectTypeCompareAny()`, `PetscObjectBaseTypeCompareAny()` 180*013e2dc7SBarry Smith 181*013e2dc7SBarry Smith @*/ 182*013e2dc7SBarry Smith PetscErrorCode PetscObjectObjectTypeCompare(PetscObject obj1, PetscObject obj2, PetscBool *same) { 183*013e2dc7SBarry Smith PetscFunctionBegin; 184*013e2dc7SBarry Smith PetscValidBoolPointer(same, 3); 185*013e2dc7SBarry Smith PetscValidHeader(obj1, 1); 186*013e2dc7SBarry Smith PetscValidHeader(obj2, 2); 187*013e2dc7SBarry Smith PetscCall(PetscStrcmp((char *)(obj1->type_name), (char *)(obj2->type_name), same)); 188*013e2dc7SBarry Smith PetscFunctionReturn(0); 189*013e2dc7SBarry Smith } 190*013e2dc7SBarry Smith 191*013e2dc7SBarry Smith /*@C 192811af0c4SBarry Smith PetscObjectBaseTypeCompare - Determines whether a `PetscObject` is of a given base type. For example the base type of `MATSEQAIJPERM` is `MATSEQAIJ` 1934099cc6bSBarry Smith 1944099cc6bSBarry Smith Not Collective 1954099cc6bSBarry Smith 1964099cc6bSBarry Smith Input Parameters: 1974099cc6bSBarry Smith + mat - the matrix 1984099cc6bSBarry Smith - type_name - string containing a type name 1994099cc6bSBarry Smith 2004099cc6bSBarry Smith Output Parameter: 201811af0c4SBarry Smith . same - `PETSC_TRUE` if it is of the same base type 2024099cc6bSBarry Smith 2034099cc6bSBarry Smith Level: intermediate 2044099cc6bSBarry Smith 205811af0c4SBarry Smith .seealso: `PetscObject`, `PetscObjectTypeCompare()`, `PetscObjectTypeCompareAny()`, `PetscObjectBaseTypeCompareAny()` 2064099cc6bSBarry Smith @*/ 2079371c9d4SSatish Balay PetscErrorCode PetscObjectBaseTypeCompare(PetscObject obj, const char type_name[], PetscBool *same) { 2084099cc6bSBarry Smith PetscFunctionBegin; 2095f80ce2aSJacob Faibussowitsch PetscValidBoolPointer(same, 3); 2104099cc6bSBarry Smith if (!obj) *same = PETSC_FALSE; 2114099cc6bSBarry Smith else if (!type_name && !obj->type_name) *same = PETSC_TRUE; 2124099cc6bSBarry Smith else if (!type_name || !obj->type_name) *same = PETSC_FALSE; 2134099cc6bSBarry Smith else { 2144099cc6bSBarry Smith PetscValidHeader(obj, 1); 2154099cc6bSBarry Smith PetscValidCharPointer(type_name, 2); 2169566063dSJacob Faibussowitsch PetscCall(PetscStrbeginswith((char *)(obj->type_name), type_name, same)); 2174099cc6bSBarry Smith } 2184099cc6bSBarry Smith PetscFunctionReturn(0); 2194099cc6bSBarry Smith } 2204099cc6bSBarry Smith 2214099cc6bSBarry Smith /*@C 222251f4c67SDmitry Karpeev PetscObjectTypeCompareAny - Determines whether a PETSc object is of any of a list of types. 2232b12f010SJed Brown 2242b12f010SJed Brown Not Collective 2252b12f010SJed Brown 2262b12f010SJed Brown Input Parameters: 227811af0c4SBarry Smith + obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. 228811af0c4SBarry Smith This must be cast with a (`PetscObjec`t), for example, `PetscObjectTypeCompareAny`((`PetscObject`)mat,...); 229811af0c4SBarry Smith - type_name - array of strings containing type names, pass the empty string "" to terminate the list 2302b12f010SJed Brown 2312b12f010SJed Brown Output Parameter: 232811af0c4SBarry Smith . match - `PETSC_TRUE` if the type of obj matches any in the list, else `PETSC_FALSE` 2332b12f010SJed Brown 2342b12f010SJed Brown Level: intermediate 2352b12f010SJed Brown 236dee2f2dbSPierre Jolivet .seealso: `VecGetType()`, `KSPGetType()`, `PCGetType()`, `SNESGetType()`, `PetscObjectTypeCompare()`, `PetscObjectBaseTypeCompare()` 2372b12f010SJed Brown @*/ 2389371c9d4SSatish Balay PetscErrorCode PetscObjectTypeCompareAny(PetscObject obj, PetscBool *match, const char type_name[], ...) { 2392b12f010SJed Brown va_list Argp; 2402b12f010SJed Brown 2412b12f010SJed Brown PetscFunctionBegin; 2425f80ce2aSJacob Faibussowitsch PetscValidBoolPointer(match, 2); 2432b12f010SJed Brown *match = PETSC_FALSE; 244f73a99e8SStefano Zampini if (!obj) PetscFunctionReturn(0); 2452b12f010SJed Brown va_start(Argp, type_name); 2462b12f010SJed Brown while (type_name && type_name[0]) { 2472b12f010SJed Brown PetscBool found; 2489566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare(obj, type_name, &found)); 2492b12f010SJed Brown if (found) { 2502b12f010SJed Brown *match = PETSC_TRUE; 2512b12f010SJed Brown break; 2522b12f010SJed Brown } 2532b12f010SJed Brown type_name = va_arg(Argp, const char *); 2542b12f010SJed Brown } 2552b12f010SJed Brown va_end(Argp); 2562b12f010SJed Brown PetscFunctionReturn(0); 2572b12f010SJed Brown } 2582b12f010SJed Brown 259b9e7e5c1SBarry Smith /*@C 260b9e7e5c1SBarry Smith PetscObjectBaseTypeCompareAny - Determines whether a PETSc object has the base type of any of a list of types. 261b9e7e5c1SBarry Smith 262b9e7e5c1SBarry Smith Not Collective 263b9e7e5c1SBarry Smith 264b9e7e5c1SBarry Smith Input Parameters: 265811af0c4SBarry Smith + obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. 266811af0c4SBarry Smith This must be cast with a (`PetscObject`), for example, `PetscObjectBaseTypeCompareAny`((`PetscObject`)mat,...); 267811af0c4SBarry Smith - type_name - array of strings containing type names, pass the empty string "" to terminate the list 268b9e7e5c1SBarry Smith 269b9e7e5c1SBarry Smith Output Parameter: 270811af0c4SBarry Smith . match - `PETSC_TRUE` if the type of obj matches any in the list, else `PETSC_FALSE` 271b9e7e5c1SBarry Smith 272b9e7e5c1SBarry Smith Level: intermediate 273b9e7e5c1SBarry Smith 274db781477SPatrick Sanan .seealso: `VecGetType()`, `KSPGetType()`, `PCGetType()`, `SNESGetType()`, `PetscObjectTypeCompare()`, `PetscObjectBaseTypeCompare()`, `PetscObjectTypeCompareAny()` 275b9e7e5c1SBarry Smith @*/ 2769371c9d4SSatish Balay PetscErrorCode PetscObjectBaseTypeCompareAny(PetscObject obj, PetscBool *match, const char type_name[], ...) { 277b9e7e5c1SBarry Smith va_list Argp; 278b9e7e5c1SBarry Smith 279b9e7e5c1SBarry Smith PetscFunctionBegin; 2805f80ce2aSJacob Faibussowitsch PetscValidBoolPointer(match, 2); 281b9e7e5c1SBarry Smith *match = PETSC_FALSE; 282b9e7e5c1SBarry Smith va_start(Argp, type_name); 283b9e7e5c1SBarry Smith while (type_name && type_name[0]) { 284b9e7e5c1SBarry Smith PetscBool found; 2859566063dSJacob Faibussowitsch PetscCall(PetscObjectBaseTypeCompare(obj, type_name, &found)); 286b9e7e5c1SBarry Smith if (found) { 287b9e7e5c1SBarry Smith *match = PETSC_TRUE; 288b9e7e5c1SBarry Smith break; 289b9e7e5c1SBarry Smith } 290b9e7e5c1SBarry Smith type_name = va_arg(Argp, const char *); 291b9e7e5c1SBarry Smith } 292b9e7e5c1SBarry Smith va_end(Argp); 293b9e7e5c1SBarry Smith PetscFunctionReturn(0); 294b9e7e5c1SBarry Smith } 295b9e7e5c1SBarry Smith 2963cfa8680SLisandro Dalcin #define MAXREGDESOBJS 256 297e5c89e4eSSatish Balay static int PetscObjectRegisterDestroy_Count = 0; 2983cfa8680SLisandro Dalcin static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS]; 299e5c89e4eSSatish Balay 300e5c89e4eSSatish Balay /*@C 301e5c89e4eSSatish Balay PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when 302811af0c4SBarry Smith `PetscFinalize()` is called. 303e5c89e4eSSatish Balay 304811af0c4SBarry Smith Logically Collective on obj 305e5c89e4eSSatish Balay 306e5c89e4eSSatish Balay Input Parameter: 307811af0c4SBarry Smith . obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. 308811af0c4SBarry Smith This must be cast with a (`PetscObject`), for example, 309811af0c4SBarry Smith `PetscObjectRegisterDestroy`((`PetscObject`)mat); 310e5c89e4eSSatish Balay 311e5c89e4eSSatish Balay Level: developer 312e5c89e4eSSatish Balay 313811af0c4SBarry Smith Note: 314e5c89e4eSSatish Balay This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer 315e5c89e4eSSatish Balay when PETSc ends. 316e5c89e4eSSatish Balay 317db781477SPatrick Sanan .seealso: `PetscObjectRegisterDestroyAll()` 318e5c89e4eSSatish Balay @*/ 3199371c9d4SSatish Balay PetscErrorCode PetscObjectRegisterDestroy(PetscObject obj) { 320e5c89e4eSSatish Balay PetscFunctionBegin; 321e5c89e4eSSatish Balay PetscValidHeader(obj, 1); 3225f80ce2aSJacob Faibussowitsch PetscCheck(PetscObjectRegisterDestroy_Count < MAXREGDESOBJS, PETSC_COMM_SELF, PETSC_ERR_PLIB, "No more room in array, limit %d \n recompile %s with larger value for " PetscStringize_(MAXREGDESOBJS), MAXREGDESOBJS, __FILE__); 3235f80ce2aSJacob Faibussowitsch PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj; 324e5c89e4eSSatish Balay PetscFunctionReturn(0); 325e5c89e4eSSatish Balay } 326e5c89e4eSSatish Balay 327e5c89e4eSSatish Balay /*@C 328e5c89e4eSSatish Balay PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered 329811af0c4SBarry Smith with `PetscObjectRegisterDestroy()`. Called by `PetscFinalize()` 330e5c89e4eSSatish Balay 331811af0c4SBarry Smith Logically Collective on the individual `PetscObject`s that are being processed 332e5c89e4eSSatish Balay 333e5c89e4eSSatish Balay Level: developer 334e5c89e4eSSatish Balay 335db781477SPatrick Sanan .seealso: `PetscObjectRegisterDestroy()` 336e5c89e4eSSatish Balay @*/ 3379371c9d4SSatish Balay PetscErrorCode PetscObjectRegisterDestroyAll(void) { 338e5c89e4eSSatish Balay PetscFunctionBegin; 3399566063dSJacob Faibussowitsch for (PetscInt i = 0; i < PetscObjectRegisterDestroy_Count; i++) PetscCall(PetscObjectDestroy(&PetscObjectRegisterDestroy_Objects[i])); 3403cfa8680SLisandro Dalcin PetscObjectRegisterDestroy_Count = 0; 341e5c89e4eSSatish Balay PetscFunctionReturn(0); 342e5c89e4eSSatish Balay } 343e5c89e4eSSatish Balay 344eb8be38cSBarry Smith #define MAXREGFIN 256 345eb8be38cSBarry Smith static int PetscRegisterFinalize_Count = 0; 34600a402e0SLisandro Dalcin static PetscErrorCode (*PetscRegisterFinalize_Functions[MAXREGFIN])(void); 347eb8be38cSBarry Smith 348eb8be38cSBarry Smith /*@C 349811af0c4SBarry Smith PetscRegisterFinalize - Registers a function that is to be called in `PetscFinalize()` 350eb8be38cSBarry Smith 351eb8be38cSBarry Smith Not Collective 352eb8be38cSBarry Smith 353eb8be38cSBarry Smith Input Parameter: 354eb8be38cSBarry Smith . PetscErrorCode (*fun)(void) - 355eb8be38cSBarry Smith 356eb8be38cSBarry Smith Level: developer 357eb8be38cSBarry Smith 358811af0c4SBarry Smith Note: 359811af0c4SBarry Smith This is used by, for example, `DMInitializePackage()` to have `DMFinalizePackage()` called 360eb8be38cSBarry Smith 361db781477SPatrick Sanan .seealso: `PetscRegisterFinalizeAll()` 362eb8be38cSBarry Smith @*/ 3639371c9d4SSatish Balay PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*f)(void)) { 364f4aac215SBarry Smith PetscFunctionBegin; 3655f80ce2aSJacob Faibussowitsch for (PetscInt i = 0; i < PetscRegisterFinalize_Count; i++) { 366aee23540SBarry Smith if (f == PetscRegisterFinalize_Functions[i]) PetscFunctionReturn(0); 367f4aac215SBarry Smith } 3685f80ce2aSJacob Faibussowitsch PetscCheck(PetscRegisterFinalize_Count < MAXREGFIN, PETSC_COMM_SELF, PETSC_ERR_PLIB, "No more room in array, limit %d \n recompile %s with larger value for " PetscStringize_(MAXREGFIN), MAXREGFIN, __FILE__); 3695f80ce2aSJacob Faibussowitsch PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f; 370eb8be38cSBarry Smith PetscFunctionReturn(0); 371eb8be38cSBarry Smith } 372eb8be38cSBarry Smith 373eb8be38cSBarry Smith /*@C 374811af0c4SBarry Smith PetscRegisterFinalizeAll - Runs all the finalize functions set with `PetscRegisterFinalize()` 375eb8be38cSBarry Smith 376eb8be38cSBarry Smith Not Collective unless registered functions are collective 377eb8be38cSBarry Smith 378eb8be38cSBarry Smith Level: developer 379eb8be38cSBarry Smith 380db781477SPatrick Sanan .seealso: `PetscRegisterFinalize()` 381eb8be38cSBarry Smith @*/ 3829371c9d4SSatish Balay PetscErrorCode PetscRegisterFinalizeAll(void) { 383eb8be38cSBarry Smith PetscFunctionBegin; 3849566063dSJacob Faibussowitsch for (PetscInt i = 0; i < PetscRegisterFinalize_Count; i++) PetscCall((*PetscRegisterFinalize_Functions[i])()); 385eb8be38cSBarry Smith PetscRegisterFinalize_Count = 0; 386eb8be38cSBarry Smith PetscFunctionReturn(0); 387eb8be38cSBarry Smith } 388