xref: /petsc/src/sys/objects/pinit.c (revision 823e7f9ce47b8581a99a605293cd5b0025a684a2)
1 #define PETSC_DLL
2 /*
3    This file defines the initialization of PETSc, including PetscInitialize()
4 */
5 
6 #include "petsc.h"        /*I  "petsc.h"   I*/
7 #include "petscsys.h"
8 #include "zope.h"
9 
10 int PETSC_SOCKFD = 0;
11 int PETSC_LISTENFD = 0;
12 int PETSC_LISTEN_CHECK = 0;
13 
14 #if defined(PETSC_USE_LOG)
15 EXTERN PetscErrorCode PetscLogBegin_Private(void);
16 #endif
17 
18 /* -----------------------------------------------------------------------------------------*/
19 
20 extern FILE *petsc_history;
21 
22 EXTERN PetscErrorCode PetscInitialize_DynamicLibraries(void);
23 EXTERN PetscErrorCode PetscFinalize_DynamicLibraries(void);
24 EXTERN PetscErrorCode PetscFListDestroyAll(void);
25 EXTERN PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int);
26 EXTERN PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int);
27 EXTERN PetscErrorCode PetscLogCloseHistoryFile(FILE **);
28 EXTERN PetscErrorCode PetscOptionsHelpDestroyList(void);
29 
30 /* this is used by the _, __, and ___ macros (see include/petscerror.h) */
31 PetscErrorCode __gierr = 0;
32 
33 /* user may set this BEFORE calling PetscInitialize() */
34 MPI_Comm PETSC_COMM_WORLD = 0;
35 
36 /*
37      Declare and set all the string names of the PETSc enums
38 */
39 const char *PetscTruths[]    = {"FALSE","TRUE","PetscTruth","PETSC_",0};
40 const char *PetscDataTypes[] = {"INT", "DOUBLE", "COMPLEX",
41                                 "LONG","SHORT",  "FLOAT",
42                                 "CHAR","LOGICAL","ENUM","TRUTH","LONGDOUBLE","PetscDataType","PETSC_",0};
43 
44 PetscCookie PETSC_LARGEST_COOKIE = PETSC_SMALLEST_COOKIE;
45 PetscCookie PETSC_OBJECT_COOKIE = 0;
46 
47 PetscTruth PetscPreLoadingUsed = PETSC_FALSE;
48 PetscTruth PetscPreLoadingOn   = PETSC_FALSE;
49 
50 PetscErrorCode PETSC_DLLEXPORT PetscCookieRegister(PetscCookie *cookie)
51 {
52   *cookie = ++PETSC_LARGEST_COOKIE;
53   return 0;
54 }
55 
56 /*
57        Checks the options database for initializations related to the
58     PETSc components
59 */
60 #undef __FUNCT__
61 #define __FUNCT__ "PetscOptionsCheckInitial_Components"
62 PetscErrorCode PETSC_DLLEXPORT PetscOptionsCheckInitial_Components(void)
63 {
64   PetscTruth flg1;
65   PetscErrorCode ierr;
66 
67   PetscFunctionBegin;
68   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg1);CHKERRQ(ierr);
69   if (flg1) {
70 #if defined (PETSC_USE_LOG)
71     MPI_Comm   comm = PETSC_COMM_WORLD;
72     ierr = (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");CHKERRQ(ierr);
73     ierr = (*PetscHelpPrintf)(comm," -log_summary_exclude: <vec,mat,pc.ksp,snes>\n");CHKERRQ(ierr);
74     ierr = (*PetscHelpPrintf)(comm," -info_exclude: <null,vec,mat,pc,ksp,snes,ts>\n");CHKERRQ(ierr);
75     ierr = (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");CHKERRQ(ierr);
76 #endif
77   }
78   PetscFunctionReturn(0);
79 }
80 
81 #undef __FUNCT__
82 #define __FUNCT__ "PetscInitializeNoArguments"
83 /*@C
84       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
85         the command line arguments.
86 
87    Collective
88 
89    Level: advanced
90 
91 .seealso: PetscInitialize(), PetscInitializeFortran()
92 @*/
93 PetscErrorCode PETSC_DLLEXPORT PetscInitializeNoArguments(void)
94 {
95   PetscErrorCode ierr;
96   int            argc = 0;
97   char           **args = 0;
98 
99   PetscFunctionBegin;
100   ierr = PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL);
101   PetscFunctionReturn(ierr);
102 }
103 
104 #undef __FUNCT__
105 #define __FUNCT__ "PetscInitialized"
106 /*@
107       PetscInitialized - Determine whether PETSc is initialized.
108 
109 7   Level: beginner
110 
111 .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
112 @*/
113 PetscErrorCode PETSC_DLLEXPORT PetscInitialized(PetscTruth *isInitialized)
114 {
115   PetscFunctionBegin;
116   PetscValidPointer(isInitialized, 1);
117   *isInitialized = PetscInitializeCalled;
118   PetscFunctionReturn(0);
119 }
120 
121 #undef __FUNCT__
122 #define __FUNCT__ "PetscFinalized"
123 /*@
124       PetscFinalized - Determine whether PetscFinalize() has been called yet
125 
126    Level: developer
127 
128 .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
129 @*/
130 PetscErrorCode PETSC_DLLEXPORT PetscFinalized(PetscTruth *isFinalized)
131 {
132   PetscFunctionBegin;
133   PetscValidPointer(isFinalized, 1);
134   *isFinalized = PetscFinalizeCalled;
135   PetscFunctionReturn(0);
136 }
137 
138 EXTERN PetscErrorCode        PetscOptionsCheckInitial_Private(void);
139 extern PetscTruth PetscBeganMPI;
140 
141 /*
142        This function is the MPI reduction operation used to compute the sum of the
143    first half of the datatype and the max of the second half.
144 */
145 MPI_Op PetscMaxSum_Op = 0;
146 
147 EXTERN_C_BEGIN
148 #undef __FUNCT__
149 #define __FUNCT__ "PetscMaxSum_Local"
150 void PETSC_DLLEXPORT PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
151 {
152   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;
153 
154   PetscFunctionBegin;
155   if (*datatype != MPIU_2INT) {
156     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
157     MPI_Abort(MPI_COMM_WORLD,1);
158   }
159 
160   for (i=0; i<count; i++) {
161     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
162     xout[2*i+1] += xin[2*i+1];
163   }
164   PetscStackPop;
165   return;
166 }
167 EXTERN_C_END
168 
169 /*
170     Returns the max of the first entry owned by this processor and the
171 sum of the second entry.
172 */
173 #undef __FUNCT__
174 #define __FUNCT__ "PetscMaxSum"
175 PetscErrorCode PETSC_DLLEXPORT PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum)
176 {
177   PetscMPIInt    size,rank;
178   PetscInt       *work;
179   PetscErrorCode ierr;
180 
181   PetscFunctionBegin;
182   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
183   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
184   ierr   = PetscMalloc(2*size*sizeof(PetscInt),&work);CHKERRQ(ierr);
185   ierr   = MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
186   *max   = work[2*rank];
187   *sum   = work[2*rank+1];
188   ierr   = PetscFree(work);CHKERRQ(ierr);
189   PetscFunctionReturn(0);
190 }
191 
192 /* ----------------------------------------------------------------------------*/
193 MPI_Op PETSC_DLLEXPORT PetscADMax_Op = 0;
194 
195 EXTERN_C_BEGIN
196 #undef __FUNCT__
197 #define __FUNCT__ "PetscADMax_Local"
198 void PETSC_DLLEXPORT PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
199 {
200   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
201   PetscInt    i,count = *cnt;
202 
203   PetscFunctionBegin;
204   if (*datatype != MPIU_2SCALAR) {
205     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
206     MPI_Abort(MPI_COMM_WORLD,1);
207   }
208 
209   for (i=0; i<count; i++) {
210     if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) {
211       xout[2*i]   = xin[2*i];
212       xout[2*i+1] = xin[2*i+1];
213     }
214   }
215 
216   PetscStackPop;
217   return;
218 }
219 EXTERN_C_END
220 
221 MPI_Op PETSC_DLLEXPORT PetscADMin_Op = 0;
222 
223 EXTERN_C_BEGIN
224 #undef __FUNCT__
225 #define __FUNCT__ "PetscADMin_Local"
226 void PETSC_DLLEXPORT PetscADMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
227 {
228   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
229   PetscInt    i,count = *cnt;
230 
231   PetscFunctionBegin;
232   if (*datatype != MPIU_2SCALAR) {
233     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
234     MPI_Abort(MPI_COMM_WORLD,1);
235   }
236 
237   for (i=0; i<count; i++) {
238     if (PetscRealPart(xout[2*i]) > PetscRealPart(xin[2*i])) {
239       xout[2*i]   = xin[2*i];
240       xout[2*i+1] = xin[2*i+1];
241     }
242   }
243 
244   PetscStackPop;
245   return;
246 }
247 EXTERN_C_END
248 /* ---------------------------------------------------------------------------------------*/
249 
250 #if defined(PETSC_USE_COMPLEX)
251 MPI_Op PetscSum_Op = 0;
252 
253 EXTERN_C_BEGIN
254 #undef __FUNCT__
255 #define __FUNCT__ "PetscSum_Local"
256 void PETSC_DLLEXPORT PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
257 {
258   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
259   PetscInt         i,count = *cnt;
260 
261   PetscFunctionBegin;
262   if (*datatype != MPIU_SCALAR) {
263     (*PetscErrorPrintf)("Can only handle MPIU_SCALAR data (i.e. double or complex) types");
264     MPI_Abort(MPI_COMM_WORLD,1);
265   }
266 
267   for (i=0; i<count; i++) {
268     xout[i] += xin[i];
269   }
270 
271   PetscStackPop;
272   return;
273 }
274 EXTERN_C_END
275 #endif
276 
277 static int  PetscGlobalArgc   = 0;
278 static char **PetscGlobalArgs = 0;
279 
280 #undef __FUNCT__
281 #define __FUNCT__ "PetscGetArgs"
282 /*@C
283    PetscGetArgs - Allows you to access the raw command line arguments anywhere
284      after PetscInitialize() is called but before PetscFinalize().
285 
286    Not Collective
287 
288    Output Parameters:
289 +  argc - count of number of command line arguments
290 -  args - the command line arguments
291 
292    Level: intermediate
293 
294    Notes:
295       This is usually used to pass the command line arguments into other libraries
296    that are called internally deep in PETSc or the application.
297 
298    Concepts: command line arguments
299 
300 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()
301 
302 @*/
303 PetscErrorCode PETSC_DLLEXPORT PetscGetArgs(int *argc,char ***args)
304 {
305   PetscFunctionBegin;
306   if (!PetscGlobalArgs) {
307     SETERRQ(PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
308   }
309   *argc = PetscGlobalArgc;
310   *args = PetscGlobalArgs;
311   PetscFunctionReturn(0);
312 }
313 
314 #undef __FUNCT__
315 #define __FUNCT__ "PetscGetArguments"
316 /*@C
317    PetscGetArguments - Allows you to access the  command line arguments anywhere
318      after PetscInitialize() is called but before PetscFinalize().
319 
320    Not Collective
321 
322    Output Parameters:
323 .  args - the command line arguments
324 
325    Level: intermediate
326 
327    Notes:
328       This does NOT start with the program name and IS null terminated (final arg is void)
329 
330    Concepts: command line arguments
331 
332 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()
333 
334 @*/
335 PetscErrorCode PETSC_DLLEXPORT PetscGetArguments(char ***args)
336 {
337   PetscInt       i,argc = PetscGlobalArgc;
338   PetscErrorCode ierr;
339 
340   PetscFunctionBegin;
341   if (!PetscGlobalArgs) {
342     SETERRQ(PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
343   }
344   ierr = PetscMalloc(argc*sizeof(char*),args);CHKERRQ(ierr);
345   for (i=0; i<argc-1; i++) {
346     ierr = PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);CHKERRQ(ierr);
347   }
348   (*args)[argc-1] = 0;
349   PetscFunctionReturn(0);
350 }
351 
352 #undef __FUNCT__
353 #define __FUNCT__ "PetscFreeArguments"
354 /*@C
355    PetscFreeArguments - Frees the memory obtained with PetscGetArguments()
356 
357    Not Collective
358 
359    Output Parameters:
360 .  args - the command line arguments
361 
362    Level: intermediate
363 
364    Concepts: command line arguments
365 
366 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()
367 
368 @*/
369 PetscErrorCode PETSC_DLLEXPORT PetscFreeArguments(char **args)
370 {
371   PetscInt       i = 0;
372   PetscErrorCode ierr;
373 
374   PetscFunctionBegin;
375   while (args[i]) {
376     ierr = PetscFree(args[i]);CHKERRQ(ierr);
377     i++;
378   }
379   ierr = PetscFree(args);CHKERRQ(ierr);
380   PetscFunctionReturn(0);
381 }
382 
383 #undef __FUNCT__
384 #define __FUNCT__ "PetscInitialize"
385 /*@C
386    PetscInitialize - Initializes the PETSc database and MPI.
387    PetscInitialize() calls MPI_Init() if that has yet to be called,
388    so this routine should always be called near the beginning of
389    your program -- usually the very first line!
390 
391    Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set
392 
393    Input Parameters:
394 +  argc - count of number of command line arguments
395 .  args - the command line arguments
396 .  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL to not check for
397           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
398 -  help - [optional] Help message to print, use PETSC_NULL for no message
399 
400    If you wish PETSc to run on a subcommunicator of MPI_COMM_WORLD, create that
401    communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize()
402 
403    Options Database Keys:
404 +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
405 .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
406 .  -on_error_emacs <machinename> causes emacsclient to jump to error file
407 .  -on_error_abort calls abort() when error detected (no traceback)
408 .  -on_error_mpiabort calls MPI_abort() when error detected
409 .  -error_output_stderr prints error messages to stderr instead of the default stdout
410 .  -error_output_none does not print the error messages (but handles errors in the same way as if this was not called)
411 .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
412 .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
413 .  -stop_for_debugger - Print message on how to attach debugger manually to
414                         process and wait (-debugger_pause) seconds for attachment
415 .  -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries)
416 .  -malloc no - Indicates not to use error-checking malloc
417 .  -malloc_debug - check for memory corruption at EVERY malloc or free
418 .  -fp_trap - Stops on floating point exceptions (Note that on the
419               IBM RS6000 this slows code by at least a factor of 10.)
420 .  -no_signal_handler - Indicates not to trap error signals
421 .  -shared_tmp - indicates /tmp directory is shared by all processors
422 .  -not_shared_tmp - each processor has own /tmp
423 .  -tmp - alternative name of /tmp directory
424 .  -get_total_flops - returns total flops done by all processors
425 -  -memory_info - Print memory usage at end of run
426 
427    Options Database Keys for Profiling:
428    See the Profiling chapter of the users manual for details.
429 +  -log_trace [filename] - Print traces of all PETSc calls
430         to the screen (useful to determine where a program
431         hangs without running in the debugger).  See PetscLogTraceBegin().
432 .  -info <optional filename> - Prints verbose information to the screen
433 -  -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages
434 
435    Environmental Variables:
436 +   PETSC_TMP - alternative tmp directory
437 .   PETSC_SHARED_TMP - tmp is shared by all processes
438 .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
439 .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
440 -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to
441 
442 
443    Level: beginner
444 
445    Notes:
446    If for some reason you must call MPI_Init() separately, call
447    it before PetscInitialize().
448 
449    Fortran Version:
450    In Fortran this routine has the format
451 $       call PetscInitialize(file,ierr)
452 
453 +   ierr - error return code
454 -  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_CHARACTER_NULL to not check for
455           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
456 
457    Important Fortran Note:
458    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
459    null character string; you CANNOT just use PETSC_NULL as
460    in the C version.  See the users manual for details.
461 
462 
463    Concepts: initializing PETSc
464 
465 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs()
466 
467 @*/
468 PetscErrorCode PETSC_DLLEXPORT PetscInitialize(int *argc,char ***args,const char file[],const char help[])
469 {
470   PetscErrorCode ierr;
471   PetscMPIInt    flag, size,nodesize;
472   PetscTruth     flg;
473   char           hostname[256];
474 
475   PetscFunctionBegin;
476   if (PetscInitializeCalled) PetscFunctionReturn(0);
477 
478   /* these must be initialized in a routine, not as a constant declaration*/
479   PETSC_STDOUT = stdout;
480   PETSC_STDERR = stderr;
481 
482   ierr = PetscOptionsCreate();CHKERRQ(ierr);
483 
484   /*
485      We initialize the program name here (before MPI_Init()) because MPICH has a bug in
486      it that it sets args[0] on all processors to be args[0] on the first processor.
487   */
488   if (argc && *argc) {
489     ierr = PetscSetProgramName(**args);CHKERRQ(ierr);
490   } else {
491     ierr = PetscSetProgramName("Unknown Name");CHKERRQ(ierr);
492   }
493 
494   ierr = MPI_Initialized(&flag);CHKERRQ(ierr);
495   if (!flag) {
496     if (PETSC_COMM_WORLD) SETERRQ(PETSC_ERR_SUP,"You cannot set PETSC_COMM_WORLD if you have not initialized MPI first");
497     ierr          = MPI_Init(argc,args);CHKERRQ(ierr);
498     PetscBeganMPI = PETSC_TRUE;
499   }
500   if (argc && args) {
501     PetscGlobalArgc = *argc;
502     PetscGlobalArgs = *args;
503   }
504   PetscInitializeCalled = PETSC_TRUE;
505   PetscFinalizeCalled   = PETSC_FALSE;
506 
507   if (!PETSC_COMM_WORLD) {
508     PETSC_COMM_WORLD = MPI_COMM_WORLD;
509   }
510 
511   /* Done after init due to a bug in MPICH-GM? */
512   ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr);
513 
514   ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);CHKERRQ(ierr);
515   ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);CHKERRQ(ierr);
516 
517 #if defined(PETSC_USE_COMPLEX)
518   /*
519      Initialized the global complex variable; this is because with
520      shared libraries the constructors for global variables
521      are not called; at least on IRIX.
522   */
523   {
524 #if defined(PETSC_CLANGUAGE_CXX)
525     PetscScalar ic(0.0,1.0);
526     PETSC_i = ic;
527 #else
528     PetscScalar ic;
529     ic = 1.I;
530     PETSC_i = ic;
531 #endif
532   }
533 
534   ierr = MPI_Type_contiguous(2,MPIU_REAL,&MPIU_COMPLEX);CHKERRQ(ierr);
535   ierr = MPI_Type_commit(&MPIU_COMPLEX);CHKERRQ(ierr);
536   ierr = MPI_Op_create(PetscSum_Local,1,&PetscSum_Op);CHKERRQ(ierr);
537 #endif
538 
539   /*
540      Create the PETSc MPI reduction operator that sums of the first
541      half of the entries and maxes the second half.
542   */
543   ierr = MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);CHKERRQ(ierr);
544 
545   ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);CHKERRQ(ierr);
546   ierr = MPI_Type_commit(&MPIU_2SCALAR);CHKERRQ(ierr);
547   ierr = MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);CHKERRQ(ierr);
548   ierr = MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);CHKERRQ(ierr);
549 
550   ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);CHKERRQ(ierr);
551   ierr = MPI_Type_commit(&MPIU_2INT);CHKERRQ(ierr);
552 
553 
554   /*
555      Build the options database
556   */
557   ierr = PetscOptionsInsert(argc,args,file);CHKERRQ(ierr);
558 
559 
560   /*
561      Print main application help message
562   */
563   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
564   if (help && flg) {
565     ierr = PetscPrintf(PETSC_COMM_WORLD,help);CHKERRQ(ierr);
566   }
567   ierr = PetscOptionsCheckInitial_Private();CHKERRQ(ierr);
568 
569   /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */
570 #if defined(PETSC_USE_LOG)
571   ierr = PetscLogBegin_Private();CHKERRQ(ierr);
572 #endif
573 
574   /*
575      Load the dynamic libraries (on machines that support them), this registers all
576      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
577   */
578   ierr = PetscInitialize_DynamicLibraries();CHKERRQ(ierr);
579 
580   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
581   ierr = PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);CHKERRQ(ierr);
582   ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr);
583   ierr = PetscInfo1(0,"Running on machine: %s\n",hostname);CHKERRQ(ierr);
584 
585   ierr = PetscOptionsCheckInitial_Components();CHKERRQ(ierr);
586   /* Check the options database for options related to the options database itself */
587   ierr = PetscOptionsSetFromOptions(); CHKERRQ(ierr);
588 
589   ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_spawn_size",&nodesize,&flg);CHKERRQ(ierr);
590   if (flg) {
591 #if defined(PETSC_HAVE_MPI_COMM_SPAWN)
592     ierr = PetscOpenMPSpawn(nodesize);CHKERRQ(ierr);
593 #else
594     SETERRQ(PETSC_ERR_SUP,"PETSc built without MPI 2 (MPI_Comm_spawn) support, use -openmp_merge_size instead");
595 #endif
596   } else {
597     ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_merge_size",&nodesize,&flg);CHKERRQ(ierr);
598     if (flg) {
599       ierr = PetscOpenMPMerge(nodesize);CHKERRQ(ierr);
600     }
601   }
602 
603   PetscFunctionReturn(ierr);
604 }
605 
606 
607 #undef __FUNCT__
608 #define __FUNCT__ "PetscFinalize"
609 /*@C
610    PetscFinalize - Checks for options to be called at the conclusion
611    of the program. MPI_Finalize() is called only if the user had not
612    called MPI_Init() before calling PetscInitialize().
613 
614    Collective on PETSC_COMM_WORLD
615 
616    Options Database Keys:
617 +  -options_table - Calls PetscOptionsPrint()
618 .  -options_left - Prints unused options that remain in the database
619 .  -options_left no - Does not print unused options that remain in the database
620 .  -mpidump - Calls PetscMPIDump()
621 .  -malloc_dump - Calls PetscMallocDump()
622 .  -malloc_info - Prints total memory usage
623 -  -malloc_log - Prints summary of memory usage
624 
625    Options Database Keys for Profiling:
626    See the Profiling chapter of the users manual for details.
627 +  -log_summary [filename] - Prints summary of flop and timing
628         information to screen. If the filename is specified the
629         summary is written to the file. (for code compiled with
630         PETSC_USE_LOG).  See PetscLogPrintSummary().
631 .  -log_all [filename] - Logs extensive profiling information
632         (for code compiled with PETSC_USE_LOG). See PetscLogDump().
633 .  -log [filename] - Logs basic profiline information (for
634         code compiled with PETSC_USE_LOG).  See PetscLogDump().
635 .  -log_sync - Log the synchronization in scatters, inner products
636         and norms
637 -  -log_mpe [filename] - Creates a logfile viewable by the
638       utility Upshot/Nupshot (in MPICH distribution)
639 
640    Level: beginner
641 
642    Note:
643    See PetscInitialize() for more general runtime options.
644 
645 .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
646 @*/
647 PetscErrorCode PETSC_DLLEXPORT PetscFinalize(void)
648 {
649   PetscErrorCode ierr;
650   PetscMPIInt    rank;
651   int            nopt;
652   PetscTruth     flg1,flg2,flg3;
653 
654   PetscFunctionBegin;
655 
656   if (!PetscInitializeCalled) {
657     (*PetscErrorPrintf)("PetscInitialize() must be called before PetscFinalize()\n");
658     PetscFunctionReturn(0);
659   }
660   ierr = PetscOpenMPFinalize();CHKERRQ(ierr);
661 
662   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
663   ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_info",&flg2);CHKERRQ(ierr);
664   if (!flg2) {
665     ierr = PetscOptionsHasName(PETSC_NULL,"-memory_info",&flg2);CHKERRQ(ierr);
666   }
667   if (flg2) {
668     ierr = PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");CHKERRQ(ierr);
669   }
670 
671   /* Destroy auxiliary packages */
672 #if defined(PETSC_HAVE_MATHEMATICA)
673   ierr = PetscViewerMathematicaFinalizePackage();CHKERRQ(ierr);
674 #endif
675 #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
676 #if defined(PETSC_HAVE_SIEVE)
677   {
678     PetscErrorCode (*func)(void);
679     char lib[PETSC_MAX_PATH_LEN];
680 
681     ierr = PetscStrcpy(lib,PETSC_LIB_DIR);CHKERRQ(ierr);
682     ierr = PetscStrcat(lib,"/libpetscdm");CHKERRQ(ierr);
683     ierr = PetscDLLibrarySym(PETSC_COMM_WORLD,&DLLibrariesLoaded,lib,"DMFinalizePackage",(void **) &func);CHKERRQ(ierr);
684     if (func) {
685       ierr = (*func)();CHKERRQ(ierr);
686     }
687   }
688 #endif
689 #endif
690 
691   /*
692      Destroy all the function registration lists created
693   */
694   ierr = PetscFinalize_DynamicLibraries();CHKERRQ(ierr);
695 
696 #if defined(PETSC_USE_LOG)
697   ierr = PetscOptionsHasName(PETSC_NULL,"-get_total_flops",&flg1);CHKERRQ(ierr);
698   if (flg1) {
699     PetscLogDouble flops = 0;
700     ierr = MPI_Reduce(&_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
701     ierr = PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);CHKERRQ(ierr);
702   }
703 #endif
704 
705   /*
706      Free all objects registered with PetscObjectRegisterDestroy() such ast
707     PETSC_VIEWER_XXX_().
708   */
709   ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr);
710   ierr = PetscOptionsHelpDestroyList();CHKERRQ(ierr);
711 
712 #if defined(PETSC_USE_DEBUG)
713   if (PetscStackActive) {
714     ierr = PetscStackDestroy();CHKERRQ(ierr);
715   }
716 #endif
717 
718 #if defined(PETSC_USE_LOG)
719   {
720     char mname[PETSC_MAX_PATH_LEN];
721 #if defined(PETSC_HAVE_MPE)
722     mname[0] = 0;
723     ierr = PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
724     if (flg1){
725       if (mname[0]) {ierr = PetscLogMPEDump(mname);CHKERRQ(ierr);}
726       else          {ierr = PetscLogMPEDump(0);CHKERRQ(ierr);}
727     }
728 #endif
729     mname[0] = 0;
730     ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
731     if (flg1) {
732       if (mname[0])  {ierr = PetscLogPrintSummary(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);}
733       else           {ierr = PetscLogPrintSummary(PETSC_COMM_WORLD,0);CHKERRQ(ierr);}
734     }
735 
736     ierr = PetscOptionsGetString(PETSC_NULL,"-log_detailed",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
737     if (flg1) {
738       if (mname[0])  {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);}
739       else           {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,0);CHKERRQ(ierr);}
740     }
741 
742     mname[0] = 0;
743     ierr = PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
744     ierr = PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);CHKERRQ(ierr);
745     if (flg1 || flg2){
746       if (mname[0]) PetscLogDump(mname);
747       else          PetscLogDump(0);
748     }
749     ierr = PetscLogDestroy();CHKERRQ(ierr);
750   }
751 #endif
752   ierr = PetscOptionsHasName(PETSC_NULL,"-no_signal_handler",&flg1);CHKERRQ(ierr);
753   if (!flg1) { ierr = PetscPopSignalHandler();CHKERRQ(ierr);}
754   ierr = PetscOptionsHasName(PETSC_NULL,"-mpidump",&flg1);CHKERRQ(ierr);
755   if (flg1) {
756     ierr = PetscMPIDump(stdout);CHKERRQ(ierr);
757   }
758   ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);CHKERRQ(ierr);
759   ierr = PetscOptionsHasName(PETSC_NULL,"-options_table",&flg2);CHKERRQ(ierr);
760   if (flg2) {
761     if (!rank) {ierr = PetscOptionsPrint(stdout);CHKERRQ(ierr);}
762   }
763 
764   /* to prevent PETSc -options_left from warning */
765   ierr = PetscOptionsHasName(PETSC_NULL,"-nox",&flg1);CHKERRQ(ierr)
766   ierr = PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr)
767 
768   flg3 = PETSC_FALSE; /* default value is required */
769   ierr = PetscOptionsGetTruth(PETSC_NULL,"-options_left",&flg3,&flg1);CHKERRQ(ierr);
770   ierr = PetscOptionsAllUsed(&nopt);CHKERRQ(ierr);
771   if (flg3) {
772     if (!flg2) { /* have not yet printed the options */
773       ierr = PetscOptionsPrint(stdout);CHKERRQ(ierr);
774     }
775     if (!nopt) {
776       ierr = PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");CHKERRQ(ierr);
777     } else if (nopt == 1) {
778       ierr = PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");CHKERRQ(ierr);
779     } else {
780       ierr = PetscPrintf(PETSC_COMM_WORLD,"There are %d unused database options. They are:\n",nopt);CHKERRQ(ierr);
781     }
782   }
783 #if defined(PETSC_USE_DEBUG)
784   if (nopt && !flg3 && !flg1) {
785     ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");CHKERRQ(ierr);
786     ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");CHKERRQ(ierr);
787     ierr = PetscOptionsLeft();CHKERRQ(ierr);
788   } else if (nopt && flg3) {
789 #else
790   if (nopt && flg3) {
791 #endif
792     ierr = PetscOptionsLeft();CHKERRQ(ierr);
793   }
794 
795   ierr = PetscOptionsHasName(PETSC_NULL,"-log_history",&flg1);CHKERRQ(ierr);
796   if (flg1) {
797     ierr = PetscLogCloseHistoryFile(&petsc_history);CHKERRQ(ierr);
798     petsc_history = 0;
799   }
800 
801   ierr = PetscInfoAllow(PETSC_FALSE,PETSC_NULL);CHKERRQ(ierr);
802 
803   /*
804        Free all the registered create functions, such as KSPList, VecList, SNESList, etc
805   */
806   ierr = PetscFListDestroyAll();CHKERRQ(ierr);
807 
808   ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);CHKERRQ(ierr);
809   ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_log",&flg3);CHKERRQ(ierr);
810   if (flg1) {
811     char fname[PETSC_MAX_PATH_LEN];
812     FILE *fd;
813 
814     fname[0] = 0;
815     ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_dump",fname,250,&flg1);CHKERRQ(ierr);
816     if (flg1 && fname[0]) {
817       char sname[PETSC_MAX_PATH_LEN];
818 
819       sprintf(sname,"%s_%d",fname,rank);
820       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
821       ierr = PetscMallocDump(fd);CHKERRQ(ierr);
822       fclose(fd);
823     } else {
824       MPI_Comm local_comm;
825 
826       ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr);
827       ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr);
828         ierr = PetscMallocDump(stdout);CHKERRQ(ierr);
829       ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr);
830       ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr);
831     }
832   }
833   if (flg3) {
834     char fname[PETSC_MAX_PATH_LEN];
835     FILE *fd;
836 
837     fname[0] = 0;
838     ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_log",fname,250,&flg1);CHKERRQ(ierr);
839     if (flg1 && fname[0]) {
840       char sname[PETSC_MAX_PATH_LEN];
841 
842       sprintf(sname,"%s_%d",fname,rank);
843       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
844       ierr = PetscMallocDumpLog(fd);CHKERRQ(ierr);
845       fclose(fd);
846     } else {
847       ierr = PetscMallocDumpLog(stdout);CHKERRQ(ierr);
848     }
849   }
850   /* Can be destroyed only after all the options are used */
851   ierr = PetscOptionsDestroy();CHKERRQ(ierr);
852 
853   PetscGlobalArgc = 0;
854   PetscGlobalArgs = 0;
855 
856 #if defined(PETSC_USE_COMPLEX)
857   ierr = MPI_Op_free(&PetscSum_Op);CHKERRQ(ierr);
858   ierr = MPI_Type_free(&MPIU_COMPLEX);CHKERRQ(ierr);
859 #endif
860   ierr = MPI_Type_free(&MPIU_2SCALAR);CHKERRQ(ierr);
861   ierr = MPI_Type_free(&MPIU_2INT);CHKERRQ(ierr);
862   ierr = MPI_Op_free(&PetscMaxSum_Op);CHKERRQ(ierr);
863   ierr = MPI_Op_free(&PetscADMax_Op);CHKERRQ(ierr);
864   ierr = MPI_Op_free(&PetscADMin_Op);CHKERRQ(ierr);
865 
866   ierr = PetscInfo(0,"PETSc successfully ended!\n");CHKERRQ(ierr);
867   if (PetscBeganMPI) {
868     ierr = MPI_Finalize();CHKERRQ(ierr);
869   }
870 
871   if(PETSC_LISTEN_CHECK){
872     PETSC_LISTEN_CHECK = 0;
873     extern FILE * PETSC_STDOUT;
874     fprintf(PETSC_STDOUT, "<<<end>>>");
875     close(PETSC_LISTENFD);}
876 
877 /*
878 
879      Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because
880    the communicator has some outstanding requests on it. Specifically if the
881    flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See
882    src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate()
883    is never freed as it should be. Thus one may obtain messages of the form
884    [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the
885    memory was not freed.
886 
887 */
888   ierr = PetscClearMalloc();CHKERRQ(ierr);
889   PetscInitializeCalled = PETSC_FALSE;
890   PetscFinalizeCalled   = PETSC_TRUE;
891   PetscFunctionReturn(ierr);
892 }
893 
894 /*
895      These may be used in code that ADIC is to be used on
896 */
897 
898 #undef __FUNCT__
899 #define __FUNCT__ "PetscGlobalMax"
900 /*@C
901       PetscGlobalMax - Computes the maximum value over several processors
902 
903      Collective on MPI_Comm
904 
905    Input Parameters:
906 +   local - the local value
907 -   comm - the processors that find the maximum
908 
909    Output Parameter:
910 .   result - the maximum value
911 
912    Level: intermediate
913 
914    Notes:
915      These functions are to be used inside user functions that are to be processed with
916    ADIC. PETSc will automatically provide differentiated versions of these functions
917 
918 .seealso: PetscGlobalMin(), PetscGlobalSum()
919 @*/
920 PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal* local,PetscReal* result,MPI_Comm comm)
921 {
922   return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MAX,comm);
923 }
924 
925 #undef __FUNCT__
926 #define __FUNCT__ "PetscGlobalMin"
927 /*@C
928       PetscGlobalMin - Computes the minimum value over several processors
929 
930      Collective on MPI_Comm
931 
932    Input Parameters:
933 +   local - the local value
934 -   comm - the processors that find the minimum
935 
936    Output Parameter:
937 .   result - the minimum value
938 
939    Level: intermediate
940 
941    Notes:
942      These functions are to be used inside user functions that are to be processed with
943    ADIC. PETSc will automatically provide differentiated versions of these functions
944 
945 .seealso: PetscGlobalMax(), PetscGlobalSum()
946 @*/
947 PetscErrorCode PETSC_DLLEXPORT PetscGlobalMin(PetscReal* local,PetscReal* result,MPI_Comm comm)
948 {
949   return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MIN,comm);
950 }
951 
952 #undef __FUNCT__
953 #define __FUNCT__ "PetscGlobalSum"
954 /*@C
955       PetscGlobalSum - Computes the sum over sever processors
956 
957      Collective on MPI_Comm
958 
959    Input Parameters:
960 +   local - the local value
961 -   comm - the processors that find the sum
962 
963    Output Parameter:
964 .   result - the sum
965 
966    Level: intermediate
967 
968    Notes:
969      These functions are to be used inside user functions that are to be processed with
970    ADIC. PETSc will automatically provide differentiated versions of these functions
971 
972 .seealso: PetscGlobalMin(), PetscGlobalMax()
973 @*/
974 PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar* local,PetscScalar* result,MPI_Comm comm)
975 {
976   return MPI_Allreduce(local,result,1,MPIU_SCALAR,PetscSum_Op,comm);
977 }
978 
979 
980