Actual source code: ex7.c


  2: static char help[] = "Demonstrates calling a Fortran computational routine from C.\n\
  3: Also demonstrates passing  PETSc objects, MPI Communicators from C to Fortran\n\
  4: and from Fortran to C\n\n";

  6: #include <petscvec.h>
  7: /*
  8:   Ugly stuff to insure the function names match between Fortran
  9:   and C. This is out of our PETSc hands to cleanup.
 10: */
 11: #include <petsc/private/fortranimpl.h>
 12: #if defined(PETSC_HAVE_FORTRAN_CAPS)
 13: #define ex7f_ EX7F
 14: #define ex7c_ EX7C
 15: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
 16: #define ex7f_ ex7f
 17: #define ex7c_ ex7c
 18: #endif

 20: PETSC_INTERN void ex7f_(Vec*,int*);

 22: int main(int argc,char **args)
 23: {
 24:   PetscInt m = 10;
 25:   int      fcomm;
 26:   Vec      vec;

 28:   PetscInitialize(&argc,&args,(char*)0,help);
 29:   /* This function should be called to be able to use PETSc routines
 30:      from the FORTRAN subroutines needed by this program */

 32:   PetscInitializeFortran();

 34:   VecCreate(PETSC_COMM_WORLD,&vec);
 35:   VecSetSizes(vec,PETSC_DECIDE,m);
 36:   VecSetFromOptions(vec);

 38:   /*
 39:      Call Fortran routine - the use of MPI_Comm_c2f() allows
 40:      translation of the MPI_Comm from C so that it can be properly
 41:      interpreted from Fortran.
 42:   */
 43:   fcomm = MPI_Comm_c2f(PETSC_COMM_WORLD);

 45:   ex7f_(&vec,&fcomm);

 47:   VecView(vec,PETSC_VIEWER_STDOUT_WORLD);
 48:   VecDestroy(&vec);
 49:   PetscFinalize();
 50:   return 0;
 51: }

 53: PETSC_INTERN void ex7c_(Vec *fvec,int *fcomm,PetscErrorCode *ierr)
 54: {
 55:   MPI_Comm comm;
 56:   PetscInt vsize;

 58:   /*
 59:     Translate Fortran integer pointer back to C and
 60:     Fortran Communicator back to C communicator
 61:   */
 62:   comm = MPI_Comm_f2c(*fcomm);

 64:   /* Some PETSc/MPI operations on Vec/Communicator objects */
 65:   *VecGetSize(*fvec,&vsize);
 66:   *MPI_Barrier(comm);

 68: }

 70: /*TEST

 72:    build:
 73:      depends: ex7f.F
 74:      requires: fortran

 76:    test:
 77:       nsize: 3
 78:       filter: sort -b |grep -v "MPI processes"
 79:       filter_output: sort -b

 81: TEST*/