Actual source code: pinit.c
petsc-3.6.4 2016-04-12
2: /*
3: This file defines the initialization of PETSc, including PetscInitialize()
4: */
5: #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/
6: #include <petscvalgrind.h>
7: #include <petscviewer.h>
9: #if defined(PETSC_HAVE_CUDA)
10: #include <cublas.h>
11: #endif
13: #if defined(PETSC_USE_LOG)
14: extern PetscErrorCode PetscLogInitialize(void);
15: #endif
17: #if defined(PETSC_SERIALIZE_FUNCTIONS)
18: PetscFPT PetscFPTData = 0;
19: #endif
21: #if defined(PETSC_HAVE_SAWS)
22: #include <petscviewersaws.h>
23: #endif
24: /* -----------------------------------------------------------------------------------------*/
26: extern FILE *petsc_history;
28: extern PetscErrorCode PetscInitialize_DynamicLibraries(void);
29: extern PetscErrorCode PetscFinalize_DynamicLibraries(void);
30: extern PetscErrorCode PetscFunctionListPrintAll(void);
31: extern PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int);
32: extern PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int);
33: extern PetscErrorCode PetscCloseHistoryFile(FILE**);
35: /* user may set this BEFORE calling PetscInitialize() */
36: MPI_Comm PETSC_COMM_WORLD = MPI_COMM_NULL;
38: PetscMPIInt Petsc_Counter_keyval = MPI_KEYVAL_INVALID;
39: PetscMPIInt Petsc_InnerComm_keyval = MPI_KEYVAL_INVALID;
40: PetscMPIInt Petsc_OuterComm_keyval = MPI_KEYVAL_INVALID;
42: /*
43: Declare and set all the string names of the PETSc enums
44: */
45: const char *const PetscBools[] = {"FALSE","TRUE","PetscBool","PETSC_",0};
46: const char *const PetscCopyModes[] = {"COPY_VALUES","OWN_POINTER","USE_POINTER","PetscCopyMode","PETSC_",0};
47: const char *const PetscDataTypes[] = {"INT","DOUBLE","COMPLEX","LONG","SHORT","FLOAT",
48: "CHAR","LOGICAL","ENUM","BOOL","LONGDOUBLE","OBJECT","FUNCTION","PetscDataType","PETSC_",0};
50: PetscBool PetscPreLoadingUsed = PETSC_FALSE;
51: PetscBool PetscPreLoadingOn = PETSC_FALSE;
53: PetscInt PetscHotRegionDepth;
55: /*
56: Checks the options database for initializations related to the
57: PETSc components
58: */
61: PetscErrorCode PetscOptionsCheckInitial_Components(void)
62: {
63: PetscBool flg1;
67: PetscOptionsHasName(NULL,"-help",&flg1);
68: if (flg1) {
69: #if defined(PETSC_USE_LOG)
70: MPI_Comm comm = PETSC_COMM_WORLD;
71: (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");
72: (*PetscHelpPrintf)(comm," -log_summary_exclude: <vec,mat,pc.ksp,snes>\n");
73: (*PetscHelpPrintf)(comm," -info_exclude: <null,vec,mat,pc,ksp,snes,ts>\n");
74: (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
75: #endif
76: }
77: return(0);
78: }
82: /*
83: PetscInitializeNoPointers - Calls PetscInitialize() from C/C++ without the pointers to argc and args
85: Collective
87: Level: advanced
89: Notes: this is called only by the PETSc MATLAB and Julia interface. Even though it might start MPI it sets the flag to
90: indicate that it did NOT start MPI so that the PetscFinalize() does not end MPI, thus allowing PetscInitialize() to
91: be called multiple times from MATLAB and Julia without the problem of trying to initialize MPI more than once.
93: Turns off PETSc signal handling because that can interact with MATLAB's signal handling causing random crashes.
95: .seealso: PetscInitialize(), PetscInitializeFortran(), PetscInitializeNoArguments()
96: */
97: PetscErrorCode PetscInitializeNoPointers(int argc,char **args,const char *filename,const char *help)
98: {
100: int myargc = argc;
101: char **myargs = args;
104: PetscInitialize(&myargc,&myargs,filename,help);
105: PetscPopSignalHandler();
106: PetscBeganMPI = PETSC_FALSE;
107: PetscFunctionReturn(ierr);
108: }
112: /*
113: Used by MATLAB and Julia interface to get communicator
114: */
115: PetscErrorCode PetscGetPETSC_COMM_SELF(MPI_Comm *comm)
116: {
118: *comm = PETSC_COMM_SELF;
119: return(0);
120: }
124: /*@C
125: PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
126: the command line arguments.
128: Collective
130: Level: advanced
132: .seealso: PetscInitialize(), PetscInitializeFortran()
133: @*/
134: PetscErrorCode PetscInitializeNoArguments(void)
135: {
137: int argc = 0;
138: char **args = 0;
141: PetscInitialize(&argc,&args,NULL,NULL);
142: PetscFunctionReturn(ierr);
143: }
147: /*@
148: PetscInitialized - Determine whether PETSc is initialized.
150: Level: beginner
152: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
153: @*/
154: PetscErrorCode PetscInitialized(PetscBool *isInitialized)
155: {
156: *isInitialized = PetscInitializeCalled;
157: return 0;
158: }
162: /*@
163: PetscFinalized - Determine whether PetscFinalize() has been called yet
165: Level: developer
167: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
168: @*/
169: PetscErrorCode PetscFinalized(PetscBool *isFinalized)
170: {
171: *isFinalized = PetscFinalizeCalled;
172: return 0;
173: }
175: extern PetscErrorCode PetscOptionsCheckInitial_Private(void);
177: /*
178: This function is the MPI reduction operation used to compute the sum of the
179: first half of the datatype and the max of the second half.
180: */
181: MPI_Op PetscMaxSum_Op = 0;
185: PETSC_EXTERN void MPIAPI PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
186: {
187: PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;
190: if (*datatype != MPIU_2INT) {
191: (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
192: MPI_Abort(MPI_COMM_WORLD,1);
193: }
195: for (i=0; i<count; i++) {
196: xout[2*i] = PetscMax(xout[2*i],xin[2*i]);
197: xout[2*i+1] += xin[2*i+1];
198: }
199: PetscFunctionReturnVoid();
200: }
202: /*
203: Returns the max of the first entry owned by this processor and the
204: sum of the second entry.
206: The reason nprocs[2*i] contains lengths nprocs[2*i+1] contains flag of 1 if length is nonzero
207: is so that the PetscMaxSum_Op() can set TWO values, if we passed in only nprocs[i] with lengths
208: there would be no place to store the both needed results.
209: */
212: PetscErrorCode PetscMaxSum(MPI_Comm comm,const PetscInt sizes[],PetscInt *max,PetscInt *sum)
213: {
214: PetscMPIInt size,rank;
215: struct {PetscInt max,sum;} *work;
219: MPI_Comm_size(comm,&size);
220: MPI_Comm_rank(comm,&rank);
221: PetscMalloc1(size,&work);
222: MPI_Allreduce((void*)sizes,work,size,MPIU_2INT,PetscMaxSum_Op,comm);
223: *max = work[rank].max;
224: *sum = work[rank].sum;
225: PetscFree(work);
226: return(0);
227: }
229: /* ----------------------------------------------------------------------------*/
231: #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128)
232: MPI_Op MPIU_SUM = 0;
236: PETSC_EXTERN void PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
237: {
238: PetscInt i,count = *cnt;
241: if (*datatype == MPIU_REAL) {
242: PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
243: for (i=0; i<count; i++) xout[i] += xin[i];
244: }
245: #if defined(PETSC_HAVE_COMPLEX)
246: else if (*datatype == MPIU_COMPLEX) {
247: PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
248: for (i=0; i<count; i++) xout[i] += xin[i];
249: }
250: #endif
251: else {
252: (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types");
253: MPI_Abort(MPI_COMM_WORLD,1);
254: }
255: PetscFunctionReturnVoid();
256: }
257: #endif
259: #if defined(PETSC_USE_REAL___FLOAT128)
260: MPI_Op MPIU_MAX = 0;
261: MPI_Op MPIU_MIN = 0;
265: PETSC_EXTERN void PetscMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
266: {
267: PetscInt i,count = *cnt;
270: if (*datatype == MPIU_REAL) {
271: PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
272: for (i=0; i<count; i++) xout[i] = PetscMax(xout[i],xin[i]);
273: }
274: #if defined(PETSC_HAVE_COMPLEX)
275: else if (*datatype == MPIU_COMPLEX) {
276: PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
277: for (i=0; i<count; i++) {
278: xout[i] = PetscRealPartComplex(xout[i])<PetscRealPartComplex(xin[i]) ? xin[i] : xout[i];
279: }
280: }
281: #endif
282: else {
283: (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types");
284: MPI_Abort(MPI_COMM_WORLD,1);
285: }
286: PetscFunctionReturnVoid();
287: }
291: PETSC_EXTERN void PetscMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
292: {
293: PetscInt i,count = *cnt;
296: if (*datatype == MPIU_REAL) {
297: PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
298: for (i=0; i<count; i++) xout[i] = PetscMin(xout[i],xin[i]);
299: }
300: #if defined(PETSC_HAVE_COMPLEX)
301: else if (*datatype == MPIU_COMPLEX) {
302: PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
303: for (i=0; i<count; i++) {
304: xout[i] = PetscRealPartComplex(xout[i])>PetscRealPartComplex(xin[i]) ? xin[i] : xout[i];
305: }
306: }
307: #endif
308: else {
309: (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_SCALAR data (i.e. double or complex) types");
310: MPI_Abort(MPI_COMM_WORLD,1);
311: }
312: PetscFunctionReturnVoid();
313: }
314: #endif
318: /*
319: Private routine to delete internal tag/name counter storage when a communicator is freed.
321: This is called by MPI, not by users. This is called by MPI_Comm_free() when the communicator that has this data as an attribute is freed.
323: Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
325: */
326: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelCounter(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
327: {
331: PetscInfo1(0,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
332: PetscFree(count_val);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
333: PetscFunctionReturn(MPI_SUCCESS);
334: }
338: /*
339: This is invoked on the outer comm as a result of either PetscCommDestroy() (via MPI_Attr_delete) or when the user
340: calls MPI_Comm_free().
342: This is the only entry point for breaking the links between inner and outer comms.
344: This is called by MPI, not by users. This is called when MPI_Comm_free() is called on the communicator.
346: Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
348: */
349: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelComm_Outer(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
350: {
352: PetscMPIInt flg;
353: union {MPI_Comm comm; void *ptr;} icomm,ocomm;
356: if (keyval != Petsc_InnerComm_keyval) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Unexpected keyval");
357: icomm.ptr = attr_val;
359: MPI_Attr_get(icomm.comm,Petsc_OuterComm_keyval,&ocomm,&flg);
360: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected reference to outer comm");
361: if (ocomm.comm != comm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm has reference to non-matching outer comm");
362: MPI_Attr_delete(icomm.comm,Petsc_OuterComm_keyval); /* Calls Petsc_DelComm_Inner */
363: PetscInfo1(0,"User MPI_Comm %ld is being freed after removing reference from inner PETSc comm to this outer comm\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
364: PetscFunctionReturn(MPI_SUCCESS);
365: }
369: /*
370: * This is invoked on the inner comm when Petsc_DelComm_Outer calls MPI_Attr_delete. It should not be reached any other way.
371: */
372: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelComm_Inner(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
373: {
377: PetscInfo1(0,"Removing reference to PETSc communicator embedded in a user MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
378: PetscFunctionReturn(MPI_SUCCESS);
379: }
381: #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
382: #if !defined(PETSC_WORDS_BIGENDIAN)
383: PETSC_EXTERN PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype,MPI_Aint*,void*);
384: PETSC_EXTERN PetscMPIInt PetscDataRep_read_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
385: PETSC_EXTERN PetscMPIInt PetscDataRep_write_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
386: #endif
387: #endif
389: int PetscGlobalArgc = 0;
390: char **PetscGlobalArgs = 0;
391: PetscSegBuffer PetscCitationsList;
395: PetscErrorCode PetscCitationsInitialize()
396: {
400: PetscSegBufferCreate(1,10000,&PetscCitationsList);
401: PetscCitationsRegister("@TechReport{petsc-user-ref,\n Author = {Satish Balay and Shrirang Abhyankar and Mark F. Adams and Jed Brown and Peter Brune\n and Kris Buschelman and Lisandro Dalcin and Victor Eijkhout and William D. Gropp\n and Dinesh Kaushik and Matthew G. Knepley\n and Lois Curfman McInnes and Karl Rupp and Barry F. Smith\n and Stefano Zampini and Hong Zhang},\n Title = {{PETS}c Users Manual},\n Number = {ANL-95/11 - Revision 3.6},\n Institution = {Argonne National Laboratory},\n Year = {2015}\n}\n",NULL);
402: PetscCitationsRegister("@InProceedings{petsc-efficient,\n Author = {Satish Balay and William D. Gropp and Lois Curfman McInnes and Barry F. Smith},\n Title = {Efficient Management of Parallelism in Object Oriented Numerical Software Libraries},\n Booktitle = {Modern Software Tools in Scientific Computing},\n Editor = {E. Arge and A. M. Bruaset and H. P. Langtangen},\n Pages = {163--202},\n Publisher = {Birkh{\\\"{a}}user Press},\n Year = {1997}\n}\n",NULL);
403: return(0);
404: }
408: /*@C
409: PetscGetArgs - Allows you to access the raw command line arguments anywhere
410: after PetscInitialize() is called but before PetscFinalize().
412: Not Collective
414: Output Parameters:
415: + argc - count of number of command line arguments
416: - args - the command line arguments
418: Level: intermediate
420: Notes:
421: This is usually used to pass the command line arguments into other libraries
422: that are called internally deep in PETSc or the application.
424: The first argument contains the program name as is normal for C arguments.
426: Concepts: command line arguments
428: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()
430: @*/
431: PetscErrorCode PetscGetArgs(int *argc,char ***args)
432: {
434: if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
435: *argc = PetscGlobalArgc;
436: *args = PetscGlobalArgs;
437: return(0);
438: }
442: /*@C
443: PetscGetArguments - Allows you to access the command line arguments anywhere
444: after PetscInitialize() is called but before PetscFinalize().
446: Not Collective
448: Output Parameters:
449: . args - the command line arguments
451: Level: intermediate
453: Notes:
454: This does NOT start with the program name and IS null terminated (final arg is void)
456: Concepts: command line arguments
458: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()
460: @*/
461: PetscErrorCode PetscGetArguments(char ***args)
462: {
463: PetscInt i,argc = PetscGlobalArgc;
467: if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
468: if (!argc) {*args = 0; return(0);}
469: PetscMalloc1(argc,args);
470: for (i=0; i<argc-1; i++) {
471: PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);
472: }
473: (*args)[argc-1] = 0;
474: return(0);
475: }
479: /*@C
480: PetscFreeArguments - Frees the memory obtained with PetscGetArguments()
482: Not Collective
484: Output Parameters:
485: . args - the command line arguments
487: Level: intermediate
489: Concepts: command line arguments
491: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()
493: @*/
494: PetscErrorCode PetscFreeArguments(char **args)
495: {
496: PetscInt i = 0;
500: if (!args) return(0);
501: while (args[i]) {
502: PetscFree(args[i]);
503: i++;
504: }
505: PetscFree(args);
506: return(0);
507: }
509: #if defined(PETSC_HAVE_SAWS)
510: #include <petscconfiginfo.h>
514: PetscErrorCode PetscInitializeSAWs(const char help[])
515: {
516: if (!PetscGlobalRank) {
517: char cert[PETSC_MAX_PATH_LEN],root[PETSC_MAX_PATH_LEN],*intro,programname[64],*appline,*options,version[64];
518: int port;
519: PetscBool flg,rootlocal = PETSC_FALSE,flg2;
520: size_t applinelen,introlen;
522: /* char sawsurl[256]; */
523:
524: PetscOptionsHasName(NULL,"-saws_log",&flg);
525: if (flg) {
526: char sawslog[PETSC_MAX_PATH_LEN];
528: PetscOptionsGetString(NULL,"-saws_log",sawslog,PETSC_MAX_PATH_LEN,NULL);
529: if (sawslog[0]) {
530: PetscStackCallSAWs(SAWs_Set_Use_Logfile,(sawslog));
531: } else {
532: PetscStackCallSAWs(SAWs_Set_Use_Logfile,(NULL));
533: }
534: }
535: PetscOptionsGetString(NULL,"-saws_https",cert,PETSC_MAX_PATH_LEN,&flg);
536: if (flg) {
537: PetscStackCallSAWs(SAWs_Set_Use_HTTPS,(cert));
538: }
539: PetscOptionsGetInt(NULL,"-saws_port",&port,&flg);
540: if (flg) {
541: PetscStackCallSAWs(SAWs_Set_Port,(port));
542: }
543: PetscOptionsGetString(NULL,"-saws_root",root,PETSC_MAX_PATH_LEN,&flg);
544: if (flg) {
545: PetscStackCallSAWs(SAWs_Set_Document_Root,(root));
546: PetscStrcmp(root,".",&rootlocal);
547: } else {
548: PetscOptionsHasName(NULL,"-saws_options",&flg);
549: if (flg) {
550: PetscStrreplace(PETSC_COMM_WORLD,"${PETSC_DIR}/share/petsc/saws",root,PETSC_MAX_PATH_LEN);
551: PetscStackCallSAWs(SAWs_Set_Document_Root,(root));
552: }
553: }
554: PetscOptionsHasName(NULL,"-saws_local",&flg2);
555: if (flg2) {
556: char jsdir[PETSC_MAX_PATH_LEN];
557: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"-saws_local option requires -saws_root option");
558: PetscSNPrintf(jsdir,PETSC_MAX_PATH_LEN,"%s/js",root);
559: PetscTestDirectory(jsdir,'r',&flg);
560: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"-saws_local option requires js directory in root directory");
561: PetscStackCallSAWs(SAWs_Push_Local_Header,());
562: }
563: PetscGetProgramName(programname,64);
564: PetscStrlen(help,&applinelen);
565: introlen = 4096 + applinelen;
566: applinelen += 1024;
567: PetscMalloc(applinelen,&appline);
568: PetscMalloc(introlen,&intro);
570: if (rootlocal) {
571: PetscSNPrintf(appline,applinelen,"%s.c.html",programname);
572: PetscTestFile(appline,'r',&rootlocal);
573: }
574: PetscOptionsGetAll(&options);
575: if (rootlocal && help) {
576: PetscSNPrintf(appline,applinelen,"<center> Running <a href=\"%s.c.html\">%s</a> %s</center><br><center><pre>%s</pre></center><br>\n",programname,programname,options,help);
577: } else if (help) {
578: PetscSNPrintf(appline,applinelen,"<center>Running %s %s</center><br><center><pre>%s</pre></center><br>",programname,options,help);
579: } else {
580: PetscSNPrintf(appline,applinelen,"<center> Running %s %s</center><br>\n",programname,options);
581: }
582: PetscFree(options);
583: PetscGetVersion(version,sizeof(version));
584: PetscSNPrintf(intro,introlen,"<body>\n"
585: "<center><h2> <a href=\"http://www.mcs.anl.gov/petsc\">PETSc</a> Application Web server powered by <a href=\"https://bitbucket.org/saws/saws\">SAWs</a> </h2></center>\n"
586: "<center>This is the default PETSc application dashboard, from it you can access any published PETSc objects or logging data</center><br><center>%s configured with %s</center><br>\n"
587: "%s",version,petscconfigureoptions,appline);
588: PetscStackCallSAWs(SAWs_Push_Body,("index.html",0,intro));
589: PetscFree(intro);
590: PetscFree(appline);
591: PetscStackCallSAWs(SAWs_Initialize,());
592: /* PetscStackCallSAWs(SAWs_Get_FullURL,(sizeof(sawsurl),sawsurl));
593: PetscPrintf(PETSC_COMM_WORLD,"Point your browser to %s for SAWs\n",sawsurl); */
594: PetscCitationsRegister("@TechReport{ saws,\n"
595: " Author = {Matt Otten and Jed Brown and Barry Smith},\n"
596: " Title = {Scientific Application Web Server (SAWs) Users Manual},\n"
597: " Institution = {Argonne National Laboratory},\n"
598: " Year = 2013\n}\n",NULL);
599: }
600: return(0);
601: }
602: #endif
606: /*@C
607: PetscInitialize - Initializes the PETSc database and MPI.
608: PetscInitialize() calls MPI_Init() if that has yet to be called,
609: so this routine should always be called near the beginning of
610: your program -- usually the very first line!
612: Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set
614: Input Parameters:
615: + argc - count of number of command line arguments
616: . args - the command line arguments
617: . file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use NULL to not check for
618: code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
619: - help - [optional] Help message to print, use NULL for no message
621: If you wish PETSc code to run ONLY on a subcommunicator of MPI_COMM_WORLD, create that
622: communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize(). Thus if you are running a
623: four process job and two processes will run PETSc and have PetscInitialize() and PetscFinalize() and two process will not,
624: then do this. If ALL processes in the job are using PetscInitialize() and PetscFinalize() then you don't need to do this, even
625: if different subcommunicators of the job are doing different things with PETSc.
627: Options Database Keys:
628: + -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
629: . -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
630: . -on_error_emacs <machinename> causes emacsclient to jump to error file
631: . -on_error_abort calls abort() when error detected (no traceback)
632: . -on_error_mpiabort calls MPI_abort() when error detected
633: . -error_output_stderr prints error messages to stderr instead of the default stdout
634: . -error_output_none does not print the error messages (but handles errors in the same way as if this was not called)
635: . -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
636: . -debugger_pause [sleeptime] (in seconds) - Pauses debugger
637: . -stop_for_debugger - Print message on how to attach debugger manually to
638: process and wait (-debugger_pause) seconds for attachment
639: . -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries)
640: . -malloc no - Indicates not to use error-checking malloc
641: . -malloc_debug - check for memory corruption at EVERY malloc or free
642: . -malloc_dump - prints a list of all unfreed memory at the end of the run
643: . -malloc_test - like -malloc_dump -malloc_debug, but only active for debugging builds
644: . -fp_trap - Stops on floating point exceptions (Note that on the
645: IBM RS6000 this slows code by at least a factor of 10.)
646: . -no_signal_handler - Indicates not to trap error signals
647: . -shared_tmp - indicates /tmp directory is shared by all processors
648: . -not_shared_tmp - each processor has own /tmp
649: . -tmp - alternative name of /tmp directory
650: . -get_total_flops - returns total flops done by all processors
651: - -memory_info - Print memory usage at end of run
653: Options Database Keys for Profiling:
654: See Users-Manual: Chapter 13 Profiling for details.
655: + -info <optional filename> - Prints verbose information to the screen
656: . -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages
657: . -log_sync - Log the synchronization in scatters, inner products and norms
658: . -log_trace [filename] - Print traces of all PETSc calls to the screen (useful to determine where a program
659: hangs without running in the debugger). See PetscLogTraceBegin().
660: . -log_summary [filename] - Prints summary of flop and timing information to screen. If the filename is specified the
661: summary is written to the file. See PetscLogView().
662: . -log_all [filename] - Logs extensive profiling information See PetscLogDump().
663: . -log [filename] - Logs basic profiline information See PetscLogDump().
664: - -log_mpe [filename] - Creates a logfile viewable by the utility Jumpshot (in MPICH distribution)
666: Only one of -log_trace, -log_summary, -log_all, -log, or -log_mpe may be used at a time
668: Environmental Variables:
669: + PETSC_TMP - alternative tmp directory
670: . PETSC_SHARED_TMP - tmp is shared by all processes
671: . PETSC_NOT_SHARED_TMP - each process has its own private tmp
672: . PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
673: - PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to
676: Level: beginner
678: Notes:
679: If for some reason you must call MPI_Init() separately, call
680: it before PetscInitialize().
682: Fortran Version:
683: In Fortran this routine has the format
684: $ call PetscInitialize(file,ierr)
686: + ierr - error return code
687: - file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL_CHARACTER to not check for
688: code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
690: Important Fortran Note:
691: In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
692: null character string; you CANNOT just use NULL as
693: in the C version. See Users-Manual: Chapter 12 PETSc for Fortran Users for details.
695: If your main program is C but you call Fortran code that also uses PETSc you need to call PetscInitializeFortran() soon after
696: calling PetscInitialize().
698: Concepts: initializing PETSc
700: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments()
702: @*/
703: PetscErrorCode PetscInitialize(int *argc,char ***args,const char file[],const char help[])
704: {
706: PetscMPIInt flag, size;
707: PetscBool flg;
708: char hostname[256];
711: if (PetscInitializeCalled) return(0);
713: /* these must be initialized in a routine, not as a constant declaration*/
714: PETSC_STDOUT = stdout;
715: PETSC_STDERR = stderr;
717: /* on Windows - set printf to default to printing 2 digit exponents */
718: #if defined(PETSC_HAVE__SET_OUTPUT_FORMAT)
719: _set_output_format(_TWO_DIGIT_EXPONENT);
720: #endif
722: PetscOptionsCreate();
724: /*
725: We initialize the program name here (before MPI_Init()) because MPICH has a bug in
726: it that it sets args[0] on all processors to be args[0] on the first processor.
727: */
728: if (argc && *argc) {
729: PetscSetProgramName(**args);
730: } else {
731: PetscSetProgramName("Unknown Name");
732: }
734: MPI_Initialized(&flag);
735: if (!flag) {
736: if (PETSC_COMM_WORLD != MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"You cannot set PETSC_COMM_WORLD if you have not initialized MPI first");
737: #if defined(PETSC_HAVE_MPI_INIT_THREAD)
738: {
739: PetscMPIInt provided;
740: MPI_Init_thread(argc,args,MPI_THREAD_FUNNELED,&provided);
741: }
742: #else
743: MPI_Init(argc,args);
744: #endif
745: PetscBeganMPI = PETSC_TRUE;
746: }
747: if (argc && args) {
748: PetscGlobalArgc = *argc;
749: PetscGlobalArgs = *args;
750: }
751: PetscFinalizeCalled = PETSC_FALSE;
753: if (PETSC_COMM_WORLD == MPI_COMM_NULL) PETSC_COMM_WORLD = MPI_COMM_WORLD;
754: MPI_Comm_set_errhandler(PETSC_COMM_WORLD,MPI_ERRORS_RETURN);
756: /* Done after init due to a bug in MPICH-GM? */
757: PetscErrorPrintfInitialize();
759: MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);
760: MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);
762: MPIU_BOOL = MPI_INT;
763: MPIU_ENUM = MPI_INT;
765: /*
766: Initialized the global complex variable; this is because with
767: shared libraries the constructors for global variables
768: are not called; at least on IRIX.
769: */
770: #if defined(PETSC_HAVE_COMPLEX)
771: {
772: #if defined(PETSC_CLANGUAGE_CXX)
773: PetscComplex ic(0.0,1.0);
774: PETSC_i = ic;
775: #elif defined(PETSC_CLANGUAGE_C)
776: PETSC_i = _Complex_I;
777: #endif
778: }
780: #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
781: MPI_Type_contiguous(2,MPI_DOUBLE,&MPIU_C_DOUBLE_COMPLEX);
782: MPI_Type_commit(&MPIU_C_DOUBLE_COMPLEX);
783: MPI_Type_contiguous(2,MPI_FLOAT,&MPIU_C_COMPLEX);
784: MPI_Type_commit(&MPIU_C_COMPLEX);
785: #endif
786: #endif /* PETSC_HAVE_COMPLEX */
788: /*
789: Create the PETSc MPI reduction operator that sums of the first
790: half of the entries and maxes the second half.
791: */
792: MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);
794: #if defined(PETSC_USE_REAL___FLOAT128)
795: MPI_Type_contiguous(2,MPI_DOUBLE,&MPIU___FLOAT128);
796: MPI_Type_commit(&MPIU___FLOAT128);
797: #if defined(PETSC_HAVE_COMPLEX)
798: MPI_Type_contiguous(4,MPI_DOUBLE,&MPIU___COMPLEX128);
799: MPI_Type_commit(&MPIU___COMPLEX128);
800: #endif
801: MPI_Op_create(PetscMax_Local,1,&MPIU_MAX);
802: MPI_Op_create(PetscMin_Local,1,&MPIU_MIN);
803: #endif
805: #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128)
806: MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);
807: #endif
809: MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);
810: MPI_Type_commit(&MPIU_2SCALAR);
812: #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
813: MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);
814: MPI_Type_commit(&MPIU_2INT);
815: #endif
818: /*
819: Attributes to be set on PETSc communicators
820: */
821: MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelCounter,&Petsc_Counter_keyval,(void*)0);
822: MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm_Outer,&Petsc_InnerComm_keyval,(void*)0);
823: MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm_Inner,&Petsc_OuterComm_keyval,(void*)0);
825: /*
826: Build the options database
827: */
828: PetscOptionsInsert(argc,args,file);
831: /*
832: Print main application help message
833: */
834: PetscOptionsHasName(NULL,"-help",&flg);
835: if (help && flg) {
836: PetscPrintf(PETSC_COMM_WORLD,help);
837: }
838: PetscOptionsCheckInitial_Private();
840: PetscCitationsInitialize();
842: #if defined(PETSC_HAVE_SAWS)
843: PetscInitializeSAWs(help);
844: #endif
846: /* Creates the logging data structures; this is enabled even if logging is not turned on */
847: #if defined(PETSC_USE_LOG)
848: PetscLogInitialize();
849: #endif
851: /*
852: Load the dynamic libraries (on machines that support them), this registers all
853: the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
854: */
855: PetscInitialize_DynamicLibraries();
857: MPI_Comm_size(PETSC_COMM_WORLD,&size);
858: PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);
859: PetscGetHostName(hostname,256);
860: PetscInfo1(0,"Running on machine: %s\n",hostname);
862: PetscOptionsCheckInitial_Components();
863: /* Check the options database for options related to the options database itself */
864: PetscOptionsSetFromOptions();
866: #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
867: /*
868: Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI
870: Currently not used because it is not supported by MPICH.
871: */
872: #if !defined(PETSC_WORDS_BIGENDIAN)
873: MPI_Register_datarep((char*)"petsc",PetscDataRep_read_conv_fn,PetscDataRep_write_conv_fn,PetscDataRep_extent_fn,NULL);
874: #endif
875: #endif
877: #if defined(PETSC_HAVE_CUDA)
878: flg = PETSC_TRUE;
879: PetscOptionsGetBool(NULL,"-cublas",&flg,NULL);
880: if (flg) {
881: PetscMPIInt p;
882: for (p = 0; p < PetscGlobalSize; ++p) {
883: if (p == PetscGlobalRank) cublasInit();
884: MPI_Barrier(PETSC_COMM_WORLD);
885: }
886: }
887: #endif
889: PetscOptionsHasName(NULL,"-python",&flg);
890: if (flg) {
891: PetscInitializeCalled = PETSC_TRUE;
892: PetscPythonInitialize(NULL,NULL);
893: }
895: /*
896: Setup building of stack frames for all function calls
897: */
898: #if defined(PETSC_USE_DEBUG)
899: PetscStackCreate();
900: #endif
902: #if defined(PETSC_SERIALIZE_FUNCTIONS)
903: PetscFPTCreate(10000);
904: #endif
907: /*
908: Once we are completedly initialized then we can set this variables
909: */
910: PetscInitializeCalled = PETSC_TRUE;
911: return(0);
912: }
914: #if defined(PETSC_USE_LOG)
915: extern PetscObject *PetscObjects;
916: extern PetscInt PetscObjectsCounts, PetscObjectsMaxCounts;
917: extern PetscBool PetscObjectsLog;
918: #endif
922: /*@C
923: PetscFinalize - Checks for options to be called at the conclusion
924: of the program. MPI_Finalize() is called only if the user had not
925: called MPI_Init() before calling PetscInitialize().
927: Collective on PETSC_COMM_WORLD
929: Options Database Keys:
930: + -options_table - Calls PetscOptionsView()
931: . -options_left - Prints unused options that remain in the database
932: . -objects_dump [all] - Prints list of objects allocated by the user that have not been freed, the option all cause all outstanding objects to be listed
933: . -mpidump - Calls PetscMPIDump()
934: . -malloc_dump - Calls PetscMallocDump()
935: . -malloc_info - Prints total memory usage
936: - -malloc_log - Prints summary of memory usage
938: Level: beginner
940: Note:
941: See PetscInitialize() for more general runtime options.
943: .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
944: @*/
945: PetscErrorCode PetscFinalize(void)
946: {
948: PetscMPIInt rank;
949: PetscInt nopt;
950: PetscBool flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
951: PetscBool flg;
952: #if defined(PETSC_USE_LOG)
953: char mname[PETSC_MAX_PATH_LEN];
954: #endif
957: if (!PetscInitializeCalled) {
958: printf("PetscInitialize() must be called before PetscFinalize()\n");
959: PetscFunctionReturn(PETSC_ERR_ARG_WRONGSTATE);
960: }
961: PetscInfo(NULL,"PetscFinalize() called\n");
963: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
965: PetscOptionsHasName(NULL,"-citations",&flg);
966: if (flg) {
967: char *cits, filename[PETSC_MAX_PATH_LEN];
968: FILE *fd = PETSC_STDOUT;
970: PetscOptionsGetString(NULL,"-citations",filename,PETSC_MAX_PATH_LEN,NULL);
971: if (filename[0]) {
972: PetscFOpen(PETSC_COMM_WORLD,filename,"w",&fd);
973: }
974: PetscSegBufferGet(PetscCitationsList,1,&cits);
975: cits[0] = 0;
976: PetscSegBufferExtractAlloc(PetscCitationsList,&cits);
977: PetscFPrintf(PETSC_COMM_WORLD,fd,"If you publish results based on this computation please cite the following:\n");
978: PetscFPrintf(PETSC_COMM_WORLD,fd,"===========================================================================\n");
979: PetscFPrintf(PETSC_COMM_WORLD,fd,"%s",cits);
980: PetscFPrintf(PETSC_COMM_WORLD,fd,"===========================================================================\n");
981: PetscFClose(PETSC_COMM_WORLD,fd);
982: PetscFree(cits);
983: }
984: PetscSegBufferDestroy(&PetscCitationsList);
986: #if defined(PETSC_HAVE_SSL) && defined(PETSC_USE_SOCKET_VIEWER)
987: /* TextBelt is run for testing purposes only, please do not use this feature often */
988: {
989: PetscInt nmax = 2;
990: char **buffs;
991: PetscMalloc1(2,&buffs);
992: PetscOptionsGetStringArray(NULL,"-textbelt",buffs,&nmax,&flg1);
993: if (flg1) {
994: if (!nmax) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"-textbelt requires either the phone number or number,\"message\"");
995: if (nmax == 1) {
996: PetscMalloc1(128,&buffs[1]);
997: PetscGetProgramName(buffs[1],32);
998: PetscStrcat(buffs[1]," has completed");
999: }
1000: PetscTextBelt(PETSC_COMM_WORLD,buffs[0],buffs[1],NULL);
1001: PetscFree(buffs[0]);
1002: PetscFree(buffs[1]);
1003: }
1004: PetscFree(buffs);
1005: }
1006: #endif
1007: /*
1008: It should be safe to cancel the options monitors, since we don't expect to be setting options
1009: here (at least that are worth monitoring). Monitors ought to be released so that they release
1010: whatever memory was allocated there before -malloc_dump reports unfreed memory.
1011: */
1012: PetscOptionsMonitorCancel();
1014: #if defined(PETSC_SERIALIZE_FUNCTIONS)
1015: PetscFPTDestroy();
1016: #endif
1019: #if defined(PETSC_HAVE_SAWS)
1020: flg = PETSC_FALSE;
1021: PetscOptionsGetBool(NULL,"-saw_options",&flg,NULL);
1022: if (flg) {
1023: PetscOptionsSAWsDestroy();
1024: }
1025: #endif
1027: #if defined(PETSC_HAVE_X)
1028: flg1 = PETSC_FALSE;
1029: PetscOptionsGetBool(NULL,"-x_virtual",&flg1,NULL);
1030: if (flg1) {
1031: /* this is a crude hack, but better than nothing */
1032: PetscPOpen(PETSC_COMM_WORLD,NULL,"pkill -9 Xvfb","r",NULL);
1033: }
1034: #endif
1036: #if !defined(PETSC_HAVE_THREADSAFETY)
1037: PetscOptionsGetBool(NULL,"-malloc_info",&flg2,NULL);
1038: if (!flg2) {
1039: flg2 = PETSC_FALSE;
1040: PetscOptionsGetBool(NULL,"-memory_info",&flg2,NULL);
1041: }
1042: if (flg2) {
1043: PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");
1044: }
1045: #endif
1047: #if defined(PETSC_USE_LOG)
1048: flg1 = PETSC_FALSE;
1049: PetscOptionsGetBool(NULL,"-get_total_flops",&flg1,NULL);
1050: if (flg1) {
1051: PetscLogDouble flops = 0;
1052: MPI_Reduce(&petsc_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);
1053: PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);
1054: }
1055: #endif
1058: #if defined(PETSC_USE_LOG)
1059: #if defined(PETSC_HAVE_MPE)
1060: mname[0] = 0;
1062: PetscOptionsGetString(NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);
1063: if (flg1) {
1064: if (mname[0]) {PetscLogMPEDump(mname);}
1065: else {PetscLogMPEDump(0);}
1066: }
1067: #endif
1068: mname[0] = 0;
1070: PetscLogViewFromOptions();
1071: PetscOptionsGetString(NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);
1072: if (flg1) {
1073: PetscViewer viewer;
1074: if (mname[0]) {
1075: PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);
1076: PetscLogView(viewer);
1077: PetscViewerDestroy(&viewer);
1078: } else {
1079: viewer = PETSC_VIEWER_STDOUT_WORLD;
1080: PetscLogView(viewer);
1081: }
1082: }
1083: mname[0] = 0;
1085: PetscOptionsGetString(NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);
1086: PetscOptionsGetString(NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);
1087: if (flg1 || flg2) {
1088: if (mname[0]) PetscLogDump(mname);
1089: else PetscLogDump(0);
1090: }
1091: #endif
1093: /*
1094: Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_().
1095: */
1096: PetscObjectRegisterDestroyAll();
1098: PetscStackDestroy();
1100: flg1 = PETSC_FALSE;
1101: PetscOptionsGetBool(NULL,"-no_signal_handler",&flg1,NULL);
1102: if (!flg1) { PetscPopSignalHandler();}
1103: flg1 = PETSC_FALSE;
1104: PetscOptionsGetBool(NULL,"-mpidump",&flg1,NULL);
1105: if (flg1) {
1106: PetscMPIDump(stdout);
1107: }
1108: flg1 = PETSC_FALSE;
1109: flg2 = PETSC_FALSE;
1110: /* preemptive call to avoid listing this option in options table as unused */
1111: PetscOptionsHasName(NULL,"-malloc_dump",&flg1);
1112: PetscOptionsHasName(NULL,"-objects_dump",&flg1);
1113: PetscOptionsGetBool(NULL,"-options_view",&flg2,NULL);
1115: if (flg2) {
1116: PetscViewer viewer;
1117: PetscViewerCreate(PETSC_COMM_WORLD,&viewer);
1118: PetscViewerSetType(viewer,PETSCVIEWERASCII);
1119: PetscOptionsView(viewer);
1120: PetscViewerDestroy(&viewer);
1121: }
1123: /* to prevent PETSc -options_left from warning */
1124: PetscOptionsHasName(NULL,"-nox",&flg1);
1125: PetscOptionsHasName(NULL,"-nox_warning",&flg1);
1127: flg3 = PETSC_FALSE; /* default value is required */
1128: PetscOptionsGetBool(NULL,"-options_left",&flg3,&flg1);
1129: PetscOptionsAllUsed(&nopt);
1130: if (flg3) {
1131: if (!flg2) { /* have not yet printed the options */
1132: PetscViewer viewer;
1133: PetscViewerCreate(PETSC_COMM_WORLD,&viewer);
1134: PetscViewerSetType(viewer,PETSCVIEWERASCII);
1135: PetscOptionsView(viewer);
1136: PetscViewerDestroy(&viewer);
1137: }
1138: if (!nopt) {
1139: PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");
1140: } else if (nopt == 1) {
1141: PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");
1142: } else {
1143: PetscPrintf(PETSC_COMM_WORLD,"There are %D unused database options. They are:\n",nopt);
1144: }
1145: }
1146: #if defined(PETSC_USE_DEBUG)
1147: if (nopt && !flg3 && !flg1) {
1148: PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");
1149: PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");
1150: PetscOptionsLeft();
1151: } else if (nopt && flg3) {
1152: #else
1153: if (nopt && flg3) {
1154: #endif
1155: PetscOptionsLeft();
1156: }
1158: #if defined(PETSC_HAVE_SAWS)
1159: if (!PetscGlobalRank) {
1160: PetscStackSAWsViewOff();
1161: PetscStackCallSAWs(SAWs_Finalize,());
1162: }
1163: #endif
1165: #if defined(PETSC_USE_LOG)
1166: /*
1167: List all objects the user may have forgot to free
1168: */
1169: if (PetscObjectsLog) {
1170: PetscOptionsHasName(NULL,"-objects_dump",&flg1);
1171: if (flg1) {
1172: MPI_Comm local_comm;
1173: char string[64];
1175: PetscOptionsGetString(NULL,"-objects_dump",string,64,NULL);
1176: MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);
1177: PetscSequentialPhaseBegin_Private(local_comm,1);
1178: PetscObjectsDump(stdout,(string[0] == 'a') ? PETSC_TRUE : PETSC_FALSE);
1179: PetscSequentialPhaseEnd_Private(local_comm,1);
1180: MPI_Comm_free(&local_comm);
1181: }
1182: }
1183: #endif
1185: #if defined(PETSC_USE_LOG)
1186: PetscObjectsCounts = 0;
1187: PetscObjectsMaxCounts = 0;
1188: PetscFree(PetscObjects);
1189: #endif
1191: #if defined(PETSC_USE_LOG)
1192: PetscLogDestroy();
1193: #endif
1195: /*
1196: Close any open dynamic libraries
1197: */
1198: PetscFinalize_DynamicLibraries();
1200: /*
1201: Destroy any packages that registered a finalize
1202: */
1203: PetscRegisterFinalizeAll();
1205: /*
1206: Print PetscFunctionLists that have not been properly freed
1208: PetscFunctionListPrintAll();
1209: */
1211: if (petsc_history) {
1212: PetscCloseHistoryFile(&petsc_history);
1213: petsc_history = 0;
1214: }
1216: PetscInfoAllow(PETSC_FALSE,NULL);
1218: #if !defined(PETSC_HAVE_THREADSAFETY)
1219: {
1220: char fname[PETSC_MAX_PATH_LEN];
1221: FILE *fd;
1222: int err;
1224: fname[0] = 0;
1226: PetscOptionsGetString(NULL,"-malloc_dump",fname,250,&flg1);
1227: flg2 = PETSC_FALSE;
1228: PetscOptionsGetBool(NULL,"-malloc_test",&flg2,NULL);
1229: #if defined(PETSC_USE_DEBUG)
1230: if (PETSC_RUNNING_ON_VALGRIND) flg2 = PETSC_FALSE;
1231: #else
1232: flg2 = PETSC_FALSE; /* Skip reporting for optimized builds regardless of -malloc_test */
1233: #endif
1234: if (flg1 && fname[0]) {
1235: char sname[PETSC_MAX_PATH_LEN];
1237: sprintf(sname,"%s_%d",fname,rank);
1238: fd = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
1239: PetscMallocDump(fd);
1240: err = fclose(fd);
1241: if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
1242: } else if (flg1 || flg2) {
1243: MPI_Comm local_comm;
1245: MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);
1246: PetscSequentialPhaseBegin_Private(local_comm,1);
1247: PetscMallocDump(stdout);
1248: PetscSequentialPhaseEnd_Private(local_comm,1);
1249: MPI_Comm_free(&local_comm);
1250: }
1251: }
1253: {
1254: char fname[PETSC_MAX_PATH_LEN];
1255: FILE *fd = NULL;
1257: fname[0] = 0;
1259: PetscOptionsGetString(NULL,"-malloc_log",fname,250,&flg1);
1260: PetscOptionsHasName(NULL,"-malloc_log_threshold",&flg2);
1261: if (flg1 && fname[0]) {
1262: int err;
1264: if (!rank) {
1265: fd = fopen(fname,"w");
1266: if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",fname);
1267: }
1268: PetscMallocDumpLog(fd);
1269: if (fd) {
1270: err = fclose(fd);
1271: if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
1272: }
1273: } else if (flg1 || flg2) {
1274: PetscMallocDumpLog(stdout);
1275: }
1276: }
1277: #endif
1279: #if defined(PETSC_HAVE_CUDA)
1280: flg = PETSC_TRUE;
1281: PetscOptionsGetBool(NULL,"-cublas",&flg,NULL);
1282: if (flg) {
1283: PetscInt p;
1284: for (p = 0; p < PetscGlobalSize; ++p) {
1285: if (p == PetscGlobalRank) cublasShutdown();
1286: MPI_Barrier(PETSC_COMM_WORLD);
1287: }
1288: }
1289: #endif
1291: /* Can be destroyed only after all the options are used */
1292: PetscOptionsDestroy();
1294: PetscGlobalArgc = 0;
1295: PetscGlobalArgs = 0;
1297: #if defined(PETSC_USE_REAL___FLOAT128)
1298: MPI_Type_free(&MPIU___FLOAT128);
1299: #if defined(PETSC_HAVE_COMPLEX)
1300: MPI_Type_free(&MPIU___COMPLEX128);
1301: #endif
1302: MPI_Op_free(&MPIU_MAX);
1303: MPI_Op_free(&MPIU_MIN);
1304: #endif
1306: #if defined(PETSC_HAVE_COMPLEX)
1307: #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
1308: MPI_Type_free(&MPIU_C_DOUBLE_COMPLEX);
1309: MPI_Type_free(&MPIU_C_COMPLEX);
1310: #endif
1311: #endif
1313: #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128)
1314: MPI_Op_free(&MPIU_SUM);
1315: #endif
1317: MPI_Type_free(&MPIU_2SCALAR);
1318: #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
1319: MPI_Type_free(&MPIU_2INT);
1320: #endif
1321: MPI_Op_free(&PetscMaxSum_Op);
1323: /*
1324: Destroy any known inner MPI_Comm's and attributes pointing to them
1325: Note this will not destroy any new communicators the user has created.
1327: If all PETSc objects were not destroyed those left over objects will have hanging references to
1328: the MPI_Comms that were freed; but that is ok because those PETSc objects will never be used again
1329: */
1330: {
1331: PetscCommCounter *counter;
1332: PetscMPIInt flg;
1333: MPI_Comm icomm;
1334: union {MPI_Comm comm; void *ptr;} ucomm;
1335: MPI_Attr_get(PETSC_COMM_SELF,Petsc_InnerComm_keyval,&ucomm,&flg);
1336: if (flg) {
1337: icomm = ucomm.comm;
1338: MPI_Attr_get(icomm,Petsc_Counter_keyval,&counter,&flg);
1339: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory");
1341: MPI_Attr_delete(PETSC_COMM_SELF,Petsc_InnerComm_keyval);
1342: MPI_Attr_delete(icomm,Petsc_Counter_keyval);
1343: MPI_Comm_free(&icomm);
1344: }
1345: MPI_Attr_get(PETSC_COMM_WORLD,Petsc_InnerComm_keyval,&ucomm,&flg);
1346: if (flg) {
1347: icomm = ucomm.comm;
1348: MPI_Attr_get(icomm,Petsc_Counter_keyval,&counter,&flg);
1349: if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory");
1351: MPI_Attr_delete(PETSC_COMM_WORLD,Petsc_InnerComm_keyval);
1352: MPI_Attr_delete(icomm,Petsc_Counter_keyval);
1353: MPI_Comm_free(&icomm);
1354: }
1355: }
1357: MPI_Keyval_free(&Petsc_Counter_keyval);
1358: MPI_Keyval_free(&Petsc_InnerComm_keyval);
1359: MPI_Keyval_free(&Petsc_OuterComm_keyval);
1361: if (PetscBeganMPI) {
1362: #if defined(PETSC_HAVE_MPI_FINALIZED)
1363: PetscMPIInt flag;
1364: MPI_Finalized(&flag);
1365: if (flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()");
1366: #endif
1367: MPI_Finalize();
1368: }
1369: /*
1371: Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because
1372: the communicator has some outstanding requests on it. Specifically if the
1373: flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See
1374: src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate()
1375: is never freed as it should be. Thus one may obtain messages of the form
1376: [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the
1377: memory was not freed.
1379: */
1380: PetscMallocClear();
1382: PetscInitializeCalled = PETSC_FALSE;
1383: PetscFinalizeCalled = PETSC_TRUE;
1384: PetscFunctionReturn(ierr);
1385: }
1387: #if defined(PETSC_MISSING_LAPACK_lsame_)
1388: PETSC_EXTERN int lsame_(char *a,char *b)
1389: {
1390: if (*a == *b) return 1;
1391: if (*a + 32 == *b) return 1;
1392: if (*a - 32 == *b) return 1;
1393: return 0;
1394: }
1395: #endif
1397: #if defined(PETSC_MISSING_LAPACK_lsame)
1398: PETSC_EXTERN int lsame(char *a,char *b)
1399: {
1400: if (*a == *b) return 1;
1401: if (*a + 32 == *b) return 1;
1402: if (*a - 32 == *b) return 1;
1403: return 0;
1404: }
1405: #endif