1 /* 2 This file contains Fortran stubs for PetscInitialize and Finalize. 3 */ 4 5 /* 6 This is to prevent the Cray T3D version of MPI (University of Edinburgh) 7 from stupidly redefining MPI_INIT(). They put this in to detect errors 8 in C code,but here I do want to be calling the Fortran version from a 9 C subroutine. 10 */ 11 #define T3DMPI_FORTRAN 12 #define T3EMPI_FORTRAN 13 14 #include <petsc/private/fortranimpl.h> 15 16 #if defined(PETSC_HAVE_FORTRAN_CAPS) 17 #define petscinitializef_ PETSCINITIALIZEF 18 #define petscfinalize_ PETSCFINALIZE 19 #define petscend_ PETSCEND 20 #define iargc_ IARGC 21 #define getarg_ GETARG 22 #define mpi_init_ MPI_INIT 23 #define petscgetcomm_ PETSCGETCOMM 24 #define petsccommandargumentcount_ PETSCCOMMANDARGUMENTCOUNT 25 #define petscgetcommandargument_ PETSCGETCOMMANDARGUMENT 26 #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 27 #define petscinitializef_ petscinitializef 28 #define petscfinalize_ petscfinalize 29 #define petscend_ petscend 30 #define mpi_init_ mpi_init 31 #define iargc_ iargc 32 #define getarg_ getarg 33 #define petscgetcomm_ petscgetcomm 34 #define petsccommandargumentcount_ petsccommandargumentcount 35 #define petscgetcommandargument_ petscgetcommandargument 36 #endif 37 38 #if defined(PETSC_HAVE_NAGF90) 39 #undef iargc_ 40 #undef getarg_ 41 #define iargc_ f90_unix_MP_iargc 42 #define getarg_ f90_unix_MP_getarg 43 #endif 44 #if defined(PETSC_USE_NARGS) /* Digital Fortran */ 45 #undef iargc_ 46 #undef getarg_ 47 #define iargc_ NARGS 48 #define getarg_ GETARG 49 #elif defined(PETSC_HAVE_PXFGETARG_NEW) /* cray x1 */ 50 #undef iargc_ 51 #undef getarg_ 52 #define iargc_ ipxfargc_ 53 #define getarg_ pxfgetarg_ 54 #endif 55 #if defined(PETSC_HAVE_FORTRAN_IARGC_UNDERSCORE) /* HPUX + no underscore */ 56 #undef iargc_ 57 #undef getarg_ 58 #define iargc_ iargc_ 59 #define getarg_ getarg_ 60 #endif 61 62 #if defined(PETSC_HAVE_FORTRAN_GET_COMMAND_ARGUMENT) /* Fortran 2003 */ 63 #undef iargc_ 64 #undef getarg_ 65 #define iargc_ petsccommandargumentcount_ 66 #define getarg_ petscgetcommandargument_ 67 #elif defined(PETSC_HAVE__GFORTRAN_IARGC) /* gfortran from gcc4 */ 68 #undef iargc_ 69 #undef getarg_ 70 #define iargc_ _gfortran_iargc 71 #define getarg_ _gfortran_getarg_i4 72 #elif defined(PETSC_HAVE_BGL_IARGC) /* bgl g77 has different external & internal name mangling */ 73 #undef iargc_ 74 #undef getarg_ 75 #define iargc iargc_ 76 #define getarg getarg_ 77 #endif 78 79 /* 80 The extra _ is because the f2c compiler puts an 81 extra _ at the end if the original routine name 82 contained any _. 83 */ 84 #if defined(PETSC_HAVE_FORTRAN_UNDERSCORE_UNDERSCORE) 85 #define mpi_init_ mpi_init__ 86 #endif 87 88 #if defined(PETSC_HAVE_MPIUNI) 89 #if defined(mpi_init_) 90 #undef mpi_init_ 91 #if defined(PETSC_HAVE_FORTRAN_CAPS) 92 #define mpi_init_ PETSC_MPI_INIT 93 #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 94 #define mpi_init_ petsc_mpi_init 95 #elif defined(PETSC_HAVE_FORTRAN_UNDERSCORE_UNDERSCORE) 96 #define mpi_init_ petsc_mpi_init__ 97 #endif 98 #else /* mpi_init_ */ 99 #define mpi_init_ petsc_mpi_init_ 100 #endif /* mpi_init_ */ 101 #endif /* PETSC_HAVE_MPIUNI */ 102 103 PETSC_EXTERN void mpi_init_(int*); 104 PETSC_EXTERN void petscgetcomm_(PetscMPIInt*); 105 106 /* 107 Different Fortran compilers handle command lines in different ways 108 */ 109 #if defined(PETSC_HAVE_FORTRAN_GET_COMMAND_ARGUMENT) /* Fortran 2003 - same as 'else' case */ 110 PETSC_EXTERN int iargc_(void); 111 PETSC_EXTERN void getarg_(int*,char*,int); 112 #elif defined(PETSC_USE_NARGS) 113 PETSC_EXTERN short __stdcall NARGS(); 114 PETSC_EXTERN void __stdcall GETARG(short*,char*,int,short *); 115 116 #elif defined(PETSC_HAVE_PXFGETARG_NEW) 117 PETSC_EXTERN int iargc_(); 118 PETSC_EXTERN void getarg_(int*,char*,int*,int*,int); 119 120 #else 121 PETSC_EXTERN int iargc_(); 122 PETSC_EXTERN void getarg_(int*,char*,int); 123 /* 124 The Cray T3D/T3E use the PXFGETARG() function 125 */ 126 #if defined(PETSC_HAVE_PXFGETARG) 127 PETSC_EXTERN void PXFGETARG(int*,_fcd,int*,int*); 128 #endif 129 #endif 130 131 PETSC_EXTERN PetscErrorCode PetscMallocAlign(size_t,PetscBool,int,const char[],const char[],void**); 132 PETSC_EXTERN PetscErrorCode PetscFreeAlign(void*,int,const char[],const char[]); 133 PETSC_INTERN int PetscGlobalArgc; 134 PETSC_INTERN char **PetscGlobalArgs; 135 136 /* 137 Reads in Fortran command line arguments and sends them to 138 all processors. 139 */ 140 141 PetscErrorCode PETScParseFortranArgs_Private(int *argc,char ***argv) 142 { 143 #if defined(PETSC_USE_NARGS) 144 short i,flg; 145 #else 146 int i; 147 #endif 148 int warg = 256; 149 PetscMPIInt rank; 150 char *p; 151 152 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 153 if (rank == 0) { 154 #if defined(PETSC_HAVE_IARG_COUNT_PROGNAME) 155 *argc = iargc_(); 156 #else 157 /* most compilers do not count the program name for argv[0] */ 158 *argc = 1 + iargc_(); 159 #endif 160 } 161 PetscCallMPI(MPI_Bcast(argc,1,MPI_INT,0,PETSC_COMM_WORLD)); 162 163 /* PetscTrMalloc() not yet set, so don't use PetscMalloc() */ 164 PetscCall(PetscMallocAlign((*argc+1)*(warg*sizeof(char)+sizeof(char*)),PETSC_FALSE,0,0,0,(void**)argv)); 165 (*argv)[0] = (char*)(*argv + *argc + 1); 166 167 if (rank == 0) { 168 PetscCall(PetscMemzero((*argv)[0],(*argc)*warg*sizeof(char))); 169 for (i=0; i<*argc; i++) { 170 (*argv)[i+1] = (*argv)[i] + warg; 171 #if defined (PETSC_HAVE_FORTRAN_GET_COMMAND_ARGUMENT) /* same as 'else' case */ 172 getarg_(&i,(*argv)[i],warg); 173 #elif defined(PETSC_HAVE_PXFGETARG_NEW) 174 {char *tmp = (*argv)[i]; 175 int ilen; 176 PetscCallFortranVoidFunction(getarg_(&i,tmp,&ilen,&ierr,warg)); 177 tmp[ilen] = 0;} 178 #elif defined(PETSC_USE_NARGS) 179 GETARG(&i,(*argv)[i],warg,&flg); 180 #else 181 /* 182 Because the stupid #defines above define all kinds of things to getarg_ we cannot do this test 183 #elif defined(PETSC_HAVE_GETARG) 184 getarg_(&i,(*argv)[i],warg); 185 #else 186 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get Fortran command line arguments"); 187 */ 188 getarg_(&i,(*argv)[i],warg); 189 #endif 190 /* zero out garbage at end of each argument */ 191 p = (*argv)[i] + warg-1; 192 while (p > (*argv)[i]) { 193 if (*p == ' ') *p = 0; 194 p--; 195 } 196 } 197 } 198 PetscCallMPI(MPI_Bcast((*argv)[0],*argc*warg,MPI_CHAR,0,PETSC_COMM_WORLD)); 199 if (rank) { 200 for (i=0; i<*argc; i++) (*argv)[i+1] = (*argv)[i] + warg; 201 } 202 return 0; 203 } 204 205 /* -----------------------------------------------------------------------------------------------*/ 206 207 PETSC_INTERN PetscErrorCode PetscPreMPIInit_Private(); 208 209 PETSC_INTERN PetscErrorCode PetscInitFortran_Private(PetscBool readarguments,const char *filename,PetscInt len) 210 { 211 char *tmp = NULL; 212 213 PetscFunctionBegin; 214 PetscCall(PetscInitializeFortran()); 215 if (readarguments) { 216 PetscCall(PETScParseFortranArgs_Private(&PetscGlobalArgc,&PetscGlobalArgs)); 217 if (filename != PETSC_NULL_CHARACTER_Fortran) { /* FIXCHAR */ 218 while ((len > 0) && (filename[len-1] == ' ')) len--; 219 PetscCall(PetscMalloc1(len+1,&tmp)); 220 PetscCall(PetscStrncpy(tmp,filename,len+1)); 221 } 222 PetscCall(PetscOptionsInsert(NULL,&PetscGlobalArgc,&PetscGlobalArgs,tmp)); 223 PetscCall(PetscFree(tmp)); /* FREECHAR */ 224 } 225 PetscFunctionReturn(0); 226 } 227 228 /* 229 petscinitialize - Version called from Fortran. 230 231 Note: 232 Since this is called from Fortran it does not return error codes 233 234 */ 235 PETSC_EXTERN void petscinitializef_(char* filename,char* help,PetscBool *readarguments,PetscErrorCode *ierr,PETSC_FORTRAN_CHARLEN_T len,PETSC_FORTRAN_CHARLEN_T helplen) 236 { 237 int j,i; 238 #if defined (PETSC_USE_NARGS) 239 short flg; 240 #endif 241 int flag; 242 char name[256] = {0}; 243 PetscMPIInt f_petsc_comm_world; 244 245 if (PetscInitializeCalled) {*ierr = 0; return;} 246 i = 0; 247 #if defined (PETSC_HAVE_FORTRAN_GET_COMMAND_ARGUMENT) /* same as 'else' case */ 248 getarg_(&i,name,sizeof(name)); 249 #elif defined (PETSC_HAVE_PXFGETARG_NEW) 250 { int ilen,sierr; 251 getarg_(&i,name,&ilen,&sierr,256); 252 if (sierr) PetscStrncpy(name,"Unknown Name",256); 253 else name[ilen] = 0; 254 } 255 #elif defined(PETSC_USE_NARGS) 256 GETARG(&i,name,256,&flg); 257 #else 258 getarg_(&i,name,256); 259 #endif 260 /* Eliminate spaces at the end of the string */ 261 for (j=sizeof(name)-2; j>=0; j--) { 262 if (name[j] != ' ') { 263 name[j+1] = 0; 264 break; 265 } 266 } 267 if (j<0) PetscStrncpy(name,"Unknown Name",256); 268 269 /* check if PETSC_COMM_WORLD is initialized by the user in fortran */ 270 petscgetcomm_(&f_petsc_comm_world); 271 MPI_Initialized(&flag); 272 if (!flag) { 273 PetscMPIInt mierr; 274 275 if (f_petsc_comm_world) {(*PetscErrorPrintf)("You cannot set PETSC_COMM_WORLD if you have not initialized MPI first\n");return;} 276 277 *ierr = PetscPreMPIInit_Private(); if (*ierr) return; 278 mpi_init_(&mierr); 279 if (mierr) { 280 *ierr = mierr; 281 (*PetscErrorPrintf)("PetscInitialize: Calling Fortran MPI_Init()\n"); 282 return; 283 } 284 PetscBeganMPI = PETSC_TRUE; 285 } 286 if (f_petsc_comm_world) PETSC_COMM_WORLD = MPI_Comm_f2c(*(MPI_Fint*)&f_petsc_comm_world); /* User called MPI_INITIALIZE() and changed PETSC_COMM_WORLD */ 287 else PETSC_COMM_WORLD = MPI_COMM_WORLD; 288 289 *ierr = PetscInitialize_Common(name,filename,help,PETSC_TRUE,*readarguments,(PetscInt)len); 290 if (*ierr) {(*PetscErrorPrintf)("PetscInitialize:PetscInitialize_Common\n");return;} 291 } 292 293 PETSC_EXTERN void petscfinalize_(PetscErrorCode *ierr) 294 { 295 #if defined(PETSC_HAVE_SUNMATHPRO) 296 extern void standard_arithmetic(); 297 standard_arithmetic(); 298 #endif 299 /* was malloced with PetscMallocAlign() so free the same way */ 300 *ierr = PetscFreeAlign(PetscGlobalArgs,0,0,0);if (*ierr) {(*PetscErrorPrintf)("PetscFinalize:Freeing args\n");return;} 301 302 *ierr = PetscFinalize(); 303 } 304 305 PETSC_EXTERN void petscend_(PetscErrorCode *ierr) 306 { 307 #if defined(PETSC_HAVE_SUNMATHPRO) 308 extern void standard_arithmetic(); 309 standard_arithmetic(); 310 #endif 311 312 *ierr = PetscEnd(); 313 } 314