xref: /petsc/src/sys/objects/ftn-custom/zstart.c (revision e383bbd07f1c175fdb04eea9fdcaf8d9f9e92c0f)
1ba9a049bSBarry Smith /*
2ba9a049bSBarry Smith   This file contains Fortran stubs for PetscInitialize and Finalize.
3ba9a049bSBarry Smith */
4ba9a049bSBarry Smith 
5ba9a049bSBarry Smith /*
6ba9a049bSBarry Smith     This is to prevent the Cray T3D version of MPI (University of Edinburgh)
7ba9a049bSBarry Smith   from stupidly redefining MPI_INIT(). They put this in to detect errors
8ba9a049bSBarry Smith   in C code,but here I do want to be calling the Fortran version from a
9ba9a049bSBarry Smith   C subroutine.
10ba9a049bSBarry Smith */
11ba9a049bSBarry Smith #define T3DMPI_FORTRAN
12ba9a049bSBarry Smith #define T3EMPI_FORTRAN
13ba9a049bSBarry Smith 
14*e383bbd0SJed Brown #define PETSC_DESIRE_COMPLEX
15ba9a049bSBarry Smith #include <petsc-private/fortranimpl.h>
16ba9a049bSBarry Smith 
17ba9a049bSBarry Smith #if defined(PETSC_HAVE_CUSP)
18ba9a049bSBarry Smith #include <cublas.h>
19ba9a049bSBarry Smith #endif
20ba9a049bSBarry Smith #include <petscthreadcomm.h>
21ba9a049bSBarry Smith 
22ba9a049bSBarry Smith extern  PetscBool  PetscBeganMPI;
23ba9a049bSBarry Smith 
24ba9a049bSBarry Smith extern PetscBool  PetscHMPIWorker;
25ba9a049bSBarry Smith 
26ba9a049bSBarry Smith #ifdef PETSC_HAVE_FORTRAN_CAPS
27ba9a049bSBarry Smith #define petscinitialize_              PETSCINITIALIZE
28ba9a049bSBarry Smith #define petscfinalize_                PETSCFINALIZE
29ba9a049bSBarry Smith #define petscend_                     PETSCEND
30ba9a049bSBarry Smith #define iargc_                        IARGC
31ba9a049bSBarry Smith #define getarg_                       GETARG
32ba9a049bSBarry Smith #define mpi_init_                     MPI_INIT
33ba9a049bSBarry Smith #define petscgetcommoncomm_           PETSCGETCOMMONCOMM
34ba9a049bSBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
35ba9a049bSBarry Smith #define petscinitialize_              petscinitialize
36ba9a049bSBarry Smith #define petscfinalize_                petscfinalize
37ba9a049bSBarry Smith #define petscend_                     petscend
38ba9a049bSBarry Smith #define mpi_init_                     mpi_init
39ba9a049bSBarry Smith #define iargc_                        iargc
40ba9a049bSBarry Smith #define getarg_                       getarg
41ba9a049bSBarry Smith #define petscgetcommoncomm_           petscgetcommoncomm
42ba9a049bSBarry Smith #endif
43ba9a049bSBarry Smith 
44ba9a049bSBarry Smith #if defined(PETSC_HAVE_NAGF90)
45ba9a049bSBarry Smith #undef iargc_
46ba9a049bSBarry Smith #undef getarg_
47ba9a049bSBarry Smith #define iargc_  f90_unix_MP_iargc
48ba9a049bSBarry Smith #define getarg_ f90_unix_MP_getarg
49ba9a049bSBarry Smith #endif
50ba9a049bSBarry Smith #if defined(PETSC_USE_NARGS) /* Digital Fortran */
51ba9a049bSBarry Smith #undef iargc_
52ba9a049bSBarry Smith #undef getarg_
53ba9a049bSBarry Smith #define iargc_  NARGS
54ba9a049bSBarry Smith #define getarg_ GETARG
55ba9a049bSBarry Smith #elif defined (PETSC_HAVE_PXFGETARG_NEW) /* cray x1 */
56ba9a049bSBarry Smith #undef iargc_
57ba9a049bSBarry Smith #undef getarg_
58ba9a049bSBarry Smith #define iargc_  ipxfargc_
59ba9a049bSBarry Smith #define getarg_ pxfgetarg_
60ba9a049bSBarry Smith #endif
61ba9a049bSBarry Smith #if defined(PETSC_HAVE_FORTRAN_IARGC_UNDERSCORE) /* HPUX + no underscore */
62ba9a049bSBarry Smith #undef iargc_
63ba9a049bSBarry Smith #undef getarg_
64ba9a049bSBarry Smith #define iargc_   iargc_
65ba9a049bSBarry Smith #define getarg_  getarg_
66ba9a049bSBarry Smith #endif
67ba9a049bSBarry Smith #if defined(PETSC_HAVE_GFORTRAN_IARGC) /* gfortran from gcc4 */
68ba9a049bSBarry Smith #undef iargc_
69ba9a049bSBarry Smith #undef getarg_
70ba9a049bSBarry Smith #define iargc_  _gfortran_iargc
71ba9a049bSBarry Smith #define getarg_ _gfortran_getarg_i4
72ba9a049bSBarry Smith #elif defined(PETSC_HAVE_BGL_IARGC) /* bgl g77 has different external & internal name mangling */
73ba9a049bSBarry Smith #undef iargc_
74ba9a049bSBarry Smith #undef getarg_
75ba9a049bSBarry Smith #define iargc  iargc_
76ba9a049bSBarry Smith #define getarg getarg_
77ba9a049bSBarry Smith #endif
78ba9a049bSBarry Smith 
79ba9a049bSBarry Smith /*
80ba9a049bSBarry Smith     The extra _ is because the f2c compiler puts an
81ba9a049bSBarry Smith   extra _ at the end if the original routine name
82ba9a049bSBarry Smith   contained any _.
83ba9a049bSBarry Smith */
84ba9a049bSBarry Smith #if defined(PETSC_HAVE_FORTRAN_UNDERSCORE_UNDERSCORE)
85ba9a049bSBarry Smith #undef mpi_init_
86ba9a049bSBarry Smith #define mpi_init_             mpi_init__
87ba9a049bSBarry Smith #endif
88ba9a049bSBarry Smith 
89ba9a049bSBarry Smith EXTERN_C_BEGIN
90ba9a049bSBarry Smith extern void PETSC_STDCALL mpi_init_(int*);
91ba9a049bSBarry Smith extern void PETSC_STDCALL petscgetcommoncomm_(PetscMPIInt*);
92ba9a049bSBarry Smith 
93ba9a049bSBarry Smith /*
94ba9a049bSBarry Smith      Different Fortran compilers handle command lines in different ways
95ba9a049bSBarry Smith */
96ba9a049bSBarry Smith #if defined(PETSC_USE_NARGS)
97ba9a049bSBarry Smith extern short __stdcall NARGS();
98ba9a049bSBarry Smith extern void  __stdcall GETARG(short*,char*,int,short *);
99ba9a049bSBarry Smith 
100ba9a049bSBarry Smith #elif defined(PETSC_HAVE_FORTRAN_STDCALL)
101ba9a049bSBarry Smith extern int  PETSC_STDCALL IARGC();
102ba9a049bSBarry Smith extern void PETSC_STDCALL GETARG(int *,char *,int);
103ba9a049bSBarry Smith 
104ba9a049bSBarry Smith #elif defined (PETSC_HAVE_PXFGETARG_NEW)
105ba9a049bSBarry Smith extern int  iargc_();
106ba9a049bSBarry Smith extern void getarg_(int*,char*,int*,int*,int);
107ba9a049bSBarry Smith 
108ba9a049bSBarry Smith #else
109ba9a049bSBarry Smith extern int  iargc_();
110ba9a049bSBarry Smith extern void getarg_(int*,char*,int);
111ba9a049bSBarry Smith /*
112ba9a049bSBarry Smith       The Cray T3D/T3E use the PXFGETARG() function
113ba9a049bSBarry Smith */
114ba9a049bSBarry Smith #if defined(PETSC_HAVE_PXFGETARG)
115ba9a049bSBarry Smith extern void PXFGETARG(int*,_fcd,int*,int*);
116ba9a049bSBarry Smith #endif
117ba9a049bSBarry Smith #endif
118ba9a049bSBarry Smith EXTERN_C_END
119ba9a049bSBarry Smith 
120*e383bbd0SJed Brown #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128)
121ba9a049bSBarry Smith extern MPI_Op MPIU_SUM;
122ba9a049bSBarry Smith EXTERN_C_BEGIN
123ba9a049bSBarry Smith extern void  MPIAPI PetscSum_Local(void*,void *,PetscMPIInt *,MPI_Datatype *);
124ba9a049bSBarry Smith EXTERN_C_END
125ba9a049bSBarry Smith #endif
126ba9a049bSBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
127ba9a049bSBarry Smith EXTERN_C_BEGIN
128ba9a049bSBarry Smith void  MPIAPI PetscSum_Local(void *,void *,PetscMPIInt *,MPI_Datatype *);
129ba9a049bSBarry Smith void  MPIAPI PetscMax_Local(void *,void *,PetscMPIInt *,MPI_Datatype *);
130ba9a049bSBarry Smith void  MPIAPI PetscMin_Local(void *,void *,PetscMPIInt *,MPI_Datatype *);
131ba9a049bSBarry Smith EXTERN_C_END
132ba9a049bSBarry Smith #endif
133ba9a049bSBarry Smith 
134ba9a049bSBarry Smith extern  MPI_Op PetscMaxSum_Op;
135ba9a049bSBarry Smith 
136ba9a049bSBarry Smith EXTERN_C_BEGIN
137ba9a049bSBarry Smith extern void  MPIAPI PetscMaxSum_Local(void*,void *,PetscMPIInt *,MPI_Datatype *);
138ba9a049bSBarry Smith extern PetscMPIInt  MPIAPI Petsc_DelCounter(MPI_Comm,PetscMPIInt,void*,void*);
139ba9a049bSBarry Smith extern PetscMPIInt  MPIAPI Petsc_DelComm(MPI_Comm,PetscMPIInt,void*,void*);
140ba9a049bSBarry Smith EXTERN_C_END
141ba9a049bSBarry Smith 
142ba9a049bSBarry Smith extern PetscErrorCode  PetscOptionsCheckInitial_Private(void);
143ba9a049bSBarry Smith extern PetscErrorCode  PetscOptionsCheckInitial_Components(void);
144ba9a049bSBarry Smith extern PetscErrorCode  PetscInitialize_DynamicLibraries(void);
145ba9a049bSBarry Smith #if defined(PETSC_USE_LOG)
146ba9a049bSBarry Smith extern PetscErrorCode  PetscLogBegin_Private(void);
147ba9a049bSBarry Smith #endif
148ba9a049bSBarry Smith extern PetscErrorCode  PetscMallocAlign(size_t,int,const char[],const char[],const char[],void**);
149ba9a049bSBarry Smith extern PetscErrorCode  PetscFreeAlign(void*,int,const char[],const char[],const char[]);
150ba9a049bSBarry Smith extern int PetscGlobalArgc;
151ba9a049bSBarry Smith extern char **PetscGlobalArgs;
152ba9a049bSBarry Smith 
153ba9a049bSBarry Smith /*
154ba9a049bSBarry Smith     Reads in Fortran command line argments and sends them to
155ba9a049bSBarry Smith   all processors and adds them to Options database.
156ba9a049bSBarry Smith */
157ba9a049bSBarry Smith 
158ba9a049bSBarry Smith PetscErrorCode PETScParseFortranArgs_Private(int *argc,char ***argv)
159ba9a049bSBarry Smith {
160ba9a049bSBarry Smith #if defined (PETSC_USE_NARGS)
161ba9a049bSBarry Smith   short          i,flg;
162ba9a049bSBarry Smith #else
163ba9a049bSBarry Smith   int            i;
164ba9a049bSBarry Smith #endif
165ba9a049bSBarry Smith   PetscErrorCode ierr;
166ba9a049bSBarry Smith   int            warg = 256;
167ba9a049bSBarry Smith   PetscMPIInt    rank;
168ba9a049bSBarry Smith   char           *p;
169ba9a049bSBarry Smith 
170ba9a049bSBarry Smith   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
171ba9a049bSBarry Smith   if (!rank) {
172ba9a049bSBarry Smith #if defined (PETSC_HAVE_IARG_COUNT_PROGNAME)
173ba9a049bSBarry Smith     *argc = iargc_();
174ba9a049bSBarry Smith #else
175ba9a049bSBarry Smith     /* most compilers do not count the program name for argv[0] */
176ba9a049bSBarry Smith     *argc = 1 + iargc_();
177ba9a049bSBarry Smith #endif
178ba9a049bSBarry Smith   }
179ba9a049bSBarry Smith   ierr = MPI_Bcast(argc,1,MPI_INT,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
180ba9a049bSBarry Smith 
181ba9a049bSBarry Smith   /* PetscTrMalloc() not yet set, so don't use PetscMalloc() */
182ba9a049bSBarry Smith   ierr = PetscMallocAlign((*argc+1)*(warg*sizeof(char)+sizeof(char*)),0,0,0,0,(void**)argv);CHKERRQ(ierr);
183ba9a049bSBarry Smith   (*argv)[0] = (char*)(*argv + *argc + 1);
184ba9a049bSBarry Smith 
185ba9a049bSBarry Smith   if (!rank) {
186ba9a049bSBarry Smith     ierr = PetscMemzero((*argv)[0],(*argc)*warg*sizeof(char));CHKERRQ(ierr);
187ba9a049bSBarry Smith     for (i=0; i<*argc; i++) {
188ba9a049bSBarry Smith       (*argv)[i+1] = (*argv)[i] + warg;
189ba9a049bSBarry Smith #if defined (PETSC_HAVE_PXFGETARG_NEW)
190ba9a049bSBarry Smith       {char *tmp = (*argv)[i];
191ba9a049bSBarry Smith       int ilen;
192ba9a049bSBarry Smith       getarg_(&i,tmp,&ilen,&ierr,warg);CHKERRQ(ierr);
193ba9a049bSBarry Smith       tmp[ilen] = 0;
194ba9a049bSBarry Smith       }
195ba9a049bSBarry Smith #elif defined (PETSC_USE_NARGS)
196ba9a049bSBarry Smith       GETARG(&i,(*argv)[i],warg,&flg);
197ba9a049bSBarry Smith #else
198ba9a049bSBarry Smith       /*
199ba9a049bSBarry Smith       Because the stupid #defines above define all kinds of things to getarg_ we cannot do this test
200ba9a049bSBarry Smith       #elif defined(PETSC_HAVE_GETARG)
201ba9a049bSBarry Smith       getarg_(&i,(*argv)[i],warg);
202ba9a049bSBarry Smith       #else
203ba9a049bSBarry Smith          SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get Fortran command line arguments");
204ba9a049bSBarry Smith       */
205ba9a049bSBarry Smith       getarg_(&i,(*argv)[i],warg);
206ba9a049bSBarry Smith #endif
207ba9a049bSBarry Smith       /* zero out garbage at end of each argument */
208ba9a049bSBarry Smith       p = (*argv)[i] + warg-1;
209ba9a049bSBarry Smith       while (p > (*argv)[i]) {
210ba9a049bSBarry Smith         if (*p == ' ') *p = 0;
211ba9a049bSBarry Smith         p--;
212ba9a049bSBarry Smith       }
213ba9a049bSBarry Smith     }
214ba9a049bSBarry Smith   }
215ba9a049bSBarry Smith   ierr = MPI_Bcast((*argv)[0],*argc*warg,MPI_CHAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
216ba9a049bSBarry Smith   if (rank) {
217ba9a049bSBarry Smith     for (i=0; i<*argc; i++) {
218ba9a049bSBarry Smith       (*argv)[i+1] = (*argv)[i] + warg;
219ba9a049bSBarry Smith     }
220ba9a049bSBarry Smith   }
221ba9a049bSBarry Smith   return 0;
222ba9a049bSBarry Smith }
223ba9a049bSBarry Smith 
224ba9a049bSBarry Smith /* -----------------------------------------------------------------------------------------------*/
225ba9a049bSBarry Smith 
226ba9a049bSBarry Smith extern  MPI_Op PetscADMax_Op;
227ba9a049bSBarry Smith extern  MPI_Op PetscADMin_Op;
228ba9a049bSBarry Smith EXTERN_C_BEGIN
229ba9a049bSBarry Smith extern void  MPIAPI PetscADMax_Local(void *,void *,PetscMPIInt *,MPI_Datatype *);
230ba9a049bSBarry Smith extern void  MPIAPI PetscADMin_Local(void *,void *,PetscMPIInt *,MPI_Datatype *);
231ba9a049bSBarry Smith EXTERN_C_END
232ba9a049bSBarry Smith 
233ba9a049bSBarry Smith 
234ba9a049bSBarry Smith EXTERN_C_BEGIN
235ba9a049bSBarry Smith /*
236ba9a049bSBarry Smith     petscinitialize - Version called from Fortran.
237ba9a049bSBarry Smith 
238ba9a049bSBarry Smith     Notes:
239ba9a049bSBarry Smith       Since this is called from Fortran it does not return error codes
240ba9a049bSBarry Smith 
241ba9a049bSBarry Smith */
242ba9a049bSBarry Smith void PETSC_STDCALL petscinitialize_(CHAR filename PETSC_MIXED_LEN(len),PetscErrorCode *ierr PETSC_END_LEN(len))
243ba9a049bSBarry Smith {
244ba9a049bSBarry Smith #if defined (PETSC_USE_NARGS)
245ba9a049bSBarry Smith   short       flg,i;
246ba9a049bSBarry Smith #else
247ba9a049bSBarry Smith   int         i;
248ba9a049bSBarry Smith #if !defined(PETSC_HAVE_PXFGETARG_NEW) && !defined (PETSC_HAVE_PXFGETARG_NEW)
249ba9a049bSBarry Smith   int         j;
250ba9a049bSBarry Smith #endif
251ba9a049bSBarry Smith #endif
252ba9a049bSBarry Smith   int         flag;
253ba9a049bSBarry Smith   PetscMPIInt size;
254ba9a049bSBarry Smith   char        *t1,name[256],hostname[64];
255ba9a049bSBarry Smith   PetscMPIInt f_petsc_comm_world;
256ba9a049bSBarry Smith   PetscInt    nodesize;
257ba9a049bSBarry Smith   PetscBool   flg;
258ba9a049bSBarry Smith 
259ba9a049bSBarry Smith   *ierr = PetscMemzero(name,256); if (*ierr) return;
260ba9a049bSBarry Smith   if (PetscInitializeCalled) {*ierr = 0; return;}
261ba9a049bSBarry Smith 
262ba9a049bSBarry Smith   /* this must be initialized in a routine, not as a constant declaration*/
263ba9a049bSBarry Smith   PETSC_STDOUT = stdout;
264ba9a049bSBarry Smith   PETSC_STDERR = stderr;
265ba9a049bSBarry Smith 
266ba9a049bSBarry Smith   *ierr = PetscOptionsCreate();
267ba9a049bSBarry Smith   if (*ierr) return;
268ba9a049bSBarry Smith   i = 0;
269ba9a049bSBarry Smith #if defined (PETSC_HAVE_PXFGETARG_NEW)
270ba9a049bSBarry Smith   { int ilen,sierr;
271ba9a049bSBarry Smith     getarg_(&i,name,&ilen,&sierr,256);
272ba9a049bSBarry Smith     if (sierr) {
273ba9a049bSBarry Smith       PetscStrncpy(name,"Unknown Name",256);
274ba9a049bSBarry Smith     } else {
275ba9a049bSBarry Smith       name[ilen] = 0;
276ba9a049bSBarry Smith     }
277ba9a049bSBarry Smith   }
278ba9a049bSBarry Smith #elif defined (PETSC_USE_NARGS)
279ba9a049bSBarry Smith   GETARG(&i,name,256,&flg);
280ba9a049bSBarry Smith #else
281ba9a049bSBarry Smith   getarg_(&i,name,256);
282ba9a049bSBarry Smith   /* Eliminate spaces at the end of the string */
283ba9a049bSBarry Smith   for (j=254; j>=0; j--) {
284ba9a049bSBarry Smith     if (name[j] != ' ') {
285ba9a049bSBarry Smith       name[j+1] = 0;
286ba9a049bSBarry Smith       break;
287ba9a049bSBarry Smith     }
288ba9a049bSBarry Smith   }
289ba9a049bSBarry Smith   if (j<0) {
290ba9a049bSBarry Smith     PetscStrncpy(name,"Unknown Name",256);
291ba9a049bSBarry Smith   }
292ba9a049bSBarry Smith #endif
293ba9a049bSBarry Smith   *ierr = PetscSetProgramName(name);
294ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize: Calling PetscSetProgramName()\n");return;}
295ba9a049bSBarry Smith 
296ba9a049bSBarry Smith   /* check if PETSC_COMM_WORLD is initialized by the user in fortran */
297ba9a049bSBarry Smith   petscgetcommoncomm_(&f_petsc_comm_world);
298ba9a049bSBarry Smith   MPI_Initialized(&flag);
299ba9a049bSBarry Smith   if (!flag) {
300ba9a049bSBarry Smith     PetscMPIInt mierr;
301ba9a049bSBarry Smith 
302ba9a049bSBarry Smith     if (f_petsc_comm_world) {(*PetscErrorPrintf)("You cannot set PETSC_COMM_WORLD if you have not initialized MPI first\n");return;}
303ba9a049bSBarry Smith     /* MPI requires calling Fortran mpi_init() if main program is Fortran */
304ba9a049bSBarry Smith     mpi_init_(&mierr);
305ba9a049bSBarry Smith     if (mierr) {
306ba9a049bSBarry Smith       *ierr = mierr;
307ba9a049bSBarry Smith       (*PetscErrorPrintf)("PetscInitialize: Calling Fortran MPI_Init()\n");
308ba9a049bSBarry Smith       return;
309ba9a049bSBarry Smith     }
310ba9a049bSBarry Smith     PetscBeganMPI    = PETSC_TRUE;
311ba9a049bSBarry Smith   }
312ba9a049bSBarry Smith   if (f_petsc_comm_world) { /* User called MPI_INITIALIZE() and changed PETSC_COMM_WORLD */
313ba9a049bSBarry Smith     PETSC_COMM_WORLD = MPI_Comm_f2c(*(MPI_Fint *)&f_petsc_comm_world);
314ba9a049bSBarry Smith   } else {
315ba9a049bSBarry Smith     PETSC_COMM_WORLD = MPI_COMM_WORLD;
316ba9a049bSBarry Smith   }
317ba9a049bSBarry Smith   PetscInitializeCalled = PETSC_TRUE;
318ba9a049bSBarry Smith 
319ba9a049bSBarry Smith   *ierr = PetscErrorPrintfInitialize();
320ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize: Calling PetscErrorPrintfInitialize()\n");return;}
321ba9a049bSBarry Smith   *ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);
322ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize: Setting PetscGlobalRank\n");return;}
323ba9a049bSBarry Smith   *ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);
324ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize: Setting PetscGlobalSize\n");return;}
325*e383bbd0SJed Brown #if defined(PETSC_HAVE_COMPLEX)
326ba9a049bSBarry Smith   /*
327ba9a049bSBarry Smith      Initialized the global variable; this is because with
328ba9a049bSBarry Smith      shared libraries the constructors for global variables
329ba9a049bSBarry Smith      are not called; at least on IRIX.
330ba9a049bSBarry Smith   */
331ba9a049bSBarry Smith   {
332ba9a049bSBarry Smith #if defined(PETSC_CLANGUAGE_CXX)
333*e383bbd0SJed Brown     PetscComplex ic(0.0,1.0);
334ba9a049bSBarry Smith     PETSC_i = ic;
335ba9a049bSBarry Smith #else
336*e383bbd0SJed Brown     PETSC_i = _Complex_I;
337ba9a049bSBarry Smith #endif
338ba9a049bSBarry Smith   }
339ba9a049bSBarry Smith 
340ba9a049bSBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
341*e383bbd0SJed Brown   *ierr = MPI_Type_contiguous(2,MPI_DOUBLE,&MPIU_C_DOUBLE_COMPLEX);
342ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI types\n");return;}
343ba9a049bSBarry Smith   *ierr = MPI_Type_commit(&MPIU_C_DOUBLE_COMPLEX);
344ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI types\n");return;}
345ba9a049bSBarry Smith   *ierr = MPI_Type_contiguous(2,MPI_FLOAT,&MPIU_C_COMPLEX);
346ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI types\n");return;}
347ba9a049bSBarry Smith   *ierr = MPI_Type_commit(&MPIU_C_COMPLEX);
348ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI types\n");return;}
349ba9a049bSBarry Smith   *ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);
350ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI ops\n");return;}
351ba9a049bSBarry Smith #endif
352ba9a049bSBarry Smith 
353ba9a049bSBarry Smith #endif
354ba9a049bSBarry Smith 
355ba9a049bSBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
356ba9a049bSBarry Smith   *ierr = MPI_Type_contiguous(2,MPI_DOUBLE,&MPIU___FLOAT128);
357ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI types\n");return;}
358ba9a049bSBarry Smith   *ierr = MPI_Type_commit(&MPIU___FLOAT128);
359ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI types\n");return;}
360*e383bbd0SJed Brown #if defined(PETSC_HAVE_COMPLEX)
361*e383bbd0SJed Brown   *ierr = MPI_Type_contiguous(4,MPI_DOUBLE,&MPIU___COMPLEX128);
362ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI types\n");return;}
363ba9a049bSBarry Smith   *ierr = MPI_Type_commit(&MPIU___COMPLEX128);
364ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI types\n");return;}
365ba9a049bSBarry Smith #endif
366ba9a049bSBarry Smith   *ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);
367ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI ops\n");return;}
368ba9a049bSBarry Smith   *ierr = MPI_Op_create(PetscMax_Local,1,&MPIU_MAX);
369ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI ops\n");return;}
370ba9a049bSBarry Smith   *ierr = MPI_Op_create(PetscMin_Local,1,&MPIU_MIN);
371ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI ops\n");return;}
372ba9a049bSBarry Smith #endif
373ba9a049bSBarry Smith 
374ba9a049bSBarry Smith   /*
375ba9a049bSBarry Smith        Create the PETSc MPI reduction operator that sums of the first
376ba9a049bSBarry Smith      half of the entries and maxes the second half.
377ba9a049bSBarry Smith   */
378ba9a049bSBarry Smith   *ierr = MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);
379ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI ops\n");return;}
380ba9a049bSBarry Smith 
381ba9a049bSBarry Smith   *ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);
382ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI types\n");return;}
383ba9a049bSBarry Smith   *ierr = MPI_Type_commit(&MPIU_2SCALAR);
384ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI types\n");return;}
385ba9a049bSBarry Smith   *ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);
386ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI types\n");return;}
387ba9a049bSBarry Smith   *ierr = MPI_Type_commit(&MPIU_2INT);
388ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI types\n");return;}
389ba9a049bSBarry Smith   *ierr = MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);
390ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI ops\n");return;}
391ba9a049bSBarry Smith   *ierr = MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);
392ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI ops\n");return;}
393ba9a049bSBarry Smith   *ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelCounter,&Petsc_Counter_keyval,(void*)0);
394ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI keyvals\n");return;}
395ba9a049bSBarry Smith   *ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_InnerComm_keyval,(void*)0);
396ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI keyvals\n");return;}
397ba9a049bSBarry Smith   *ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_OuterComm_keyval,(void*)0);
398ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating MPI keyvals\n");return;}
399ba9a049bSBarry Smith 
400ba9a049bSBarry Smith   /*
401ba9a049bSBarry Smith      PetscInitializeFortran() is called twice. Here it initializes
402ba9a049bSBarry Smith      PETSC_NULL_CHARACTER_Fortran. Below it initializes the PETSC_VIEWERs.
403ba9a049bSBarry Smith      The PETSC_VIEWERs have not been created yet, so they must be initialized
404ba9a049bSBarry Smith      below.
405ba9a049bSBarry Smith   */
406ba9a049bSBarry Smith   PetscInitializeFortran();
407ba9a049bSBarry Smith   PETScParseFortranArgs_Private(&PetscGlobalArgc,&PetscGlobalArgs);
408ba9a049bSBarry Smith   FIXCHAR(filename,len,t1);
409ba9a049bSBarry Smith   *ierr = PetscOptionsInsert(&PetscGlobalArgc,&PetscGlobalArgs,t1);
410ba9a049bSBarry Smith   FREECHAR(filename,t1);
411ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Creating options database\n");return;}
412ba9a049bSBarry Smith   *ierr = PetscOptionsCheckInitial_Private();
413ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Checking initial options\n");return;}
414ba9a049bSBarry Smith #if defined (PETSC_USE_LOG)
415ba9a049bSBarry Smith   *ierr = PetscLogBegin_Private();
416ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize: intializing logging\n");return;}
417ba9a049bSBarry Smith #endif
418ba9a049bSBarry Smith   *ierr = PetscInitialize_DynamicLibraries();
419ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Initializing dynamic libraries\n");return;}
420ba9a049bSBarry Smith 
421ba9a049bSBarry Smith   *ierr = PetscInitializeFortran();
422ba9a049bSBarry Smith   if (*ierr) { (*PetscErrorPrintf)("PetscInitialize:Setting up common block\n");return;}
423ba9a049bSBarry Smith 
424ba9a049bSBarry Smith   *ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);
425ba9a049bSBarry Smith   if (*ierr) { (*PetscErrorPrintf)("PetscInitialize:Getting MPI_Comm_size()\n");return;}
426ba9a049bSBarry Smith   *ierr = PetscInfo1(0,"(Fortran):PETSc successfully started: procs %d\n",size);
427ba9a049bSBarry Smith   if (*ierr) { (*PetscErrorPrintf)("PetscInitialize:Calling PetscInfo()\n");return;}
428ba9a049bSBarry Smith   *ierr = PetscGetHostName(hostname,64);
429ba9a049bSBarry Smith   if (*ierr) { (*PetscErrorPrintf)("PetscInitialize:Getting hostname\n");return;}
430ba9a049bSBarry Smith   *ierr = PetscInfo1(0,"Running on machine: %s\n",hostname);
431ba9a049bSBarry Smith   if (*ierr) { (*PetscErrorPrintf)("PetscInitialize:Calling PetscInfo()\n");return;}
432ba9a049bSBarry Smith   *ierr = PetscOptionsCheckInitial_Components();
433ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Checking initial options\n");return;}
434ba9a049bSBarry Smith 
435ba9a049bSBarry Smith   *ierr = PetscThreadCommInitializePackage(PETSC_NULL);
436ba9a049bSBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:Calling PetscThreadCommInitialize()\n");return;}
437ba9a049bSBarry Smith 
4389248b815SShri Abhyankar #if defined(PETSC_USE_DEBUG)
43993471ed9SShri Abhyankar   PetscThreadLocalRegister(&petscstack); /* Creates petscstack_key if needed */
4409248b815SShri Abhyankar   *ierr = PetscStackCreate();
4413f209d92SBarry Smith   if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:PetscStackCreate()\n");return;}
4429248b815SShri Abhyankar #endif
4439248b815SShri Abhyankar 
444ba9a049bSBarry Smith   *ierr = PetscOptionsGetInt(PETSC_NULL,"-hmpi_spawn_size",&nodesize,&flg);
445ba9a049bSBarry Smith   if (flg) {
446ba9a049bSBarry Smith #if defined(PETSC_HAVE_MPI_COMM_SPAWN)
447ba9a049bSBarry Smith     *ierr = PetscHMPISpawn((PetscMPIInt) nodesize);/* worker nodes never return from here; they go directly to PetscEnd() */
448ba9a049bSBarry Smith     if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:PetscHMPIS-pawn()\n");return;}
449ba9a049bSBarry Smith #else
450c2070d34SBarry Smith     *ierr = PETSC_ERR_SUP;
451c2070d34SBarry Smith     (*PetscErrorPrintf)("PetscInitialize: PETSc built without MPI 2 (MPI_Comm_spawn) support, use -hmpi_merge_size instead");
452c2070d34SBarry Smith     return;
453ba9a049bSBarry Smith #endif
454ba9a049bSBarry Smith   } else {
455ba9a049bSBarry Smith     *ierr = PetscOptionsGetInt(PETSC_NULL,"-hmpi_merge_size",&nodesize,&flg);
456ba9a049bSBarry Smith     if (flg) {
457ba9a049bSBarry Smith       *ierr = PetscHMPIMerge((PetscMPIInt) nodesize,PETSC_NULL,PETSC_NULL);
458ba9a049bSBarry Smith       if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:PetscHMPIMerge()\n");return;}
459ba9a049bSBarry Smith       if (PetscHMPIWorker) { /* if worker then never enter user code */
460ba9a049bSBarry Smith         PetscInitializeCalled = PETSC_TRUE;
461ba9a049bSBarry Smith         *ierr = PetscEnd();
462ba9a049bSBarry Smith       }
463ba9a049bSBarry Smith     }
464ba9a049bSBarry Smith   }
465ba9a049bSBarry Smith 
466ba9a049bSBarry Smith #if defined(PETSC_HAVE_CUDA)
467ba9a049bSBarry Smith   cublasInit();
468ba9a049bSBarry Smith #endif
469ba9a049bSBarry Smith }
470ba9a049bSBarry Smith 
471ba9a049bSBarry Smith void PETSC_STDCALL petscfinalize_(PetscErrorCode *ierr)
472ba9a049bSBarry Smith {
473ba9a049bSBarry Smith #if defined(PETSC_HAVE_SUNMATHPRO)
474ba9a049bSBarry Smith   extern void standard_arithmetic();
475ba9a049bSBarry Smith   standard_arithmetic();
476ba9a049bSBarry Smith #endif
477ba9a049bSBarry Smith   /* was malloced with PetscMallocAlign() so free the same way */
478ba9a049bSBarry Smith   *ierr = PetscFreeAlign(PetscGlobalArgs,0,0,0,0);if (*ierr) {(*PetscErrorPrintf)("PetscFinalize:Freeing args\n");return;}
479ba9a049bSBarry Smith 
480ba9a049bSBarry Smith   *ierr = PetscFinalize();
481ba9a049bSBarry Smith }
482ba9a049bSBarry Smith 
483ba9a049bSBarry Smith void PETSC_STDCALL petscend_(PetscErrorCode *ierr)
484ba9a049bSBarry Smith {
485ba9a049bSBarry Smith #if defined(PETSC_HAVE_SUNMATHPRO)
486ba9a049bSBarry Smith   extern void standard_arithmetic();
487ba9a049bSBarry Smith   standard_arithmetic();
488ba9a049bSBarry Smith #endif
489ba9a049bSBarry Smith 
490ba9a049bSBarry Smith   *ierr = PetscEnd();
491ba9a049bSBarry Smith }
492ba9a049bSBarry Smith 
493ba9a049bSBarry Smith 
494ba9a049bSBarry Smith EXTERN_C_END
495