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;

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

 33:   PetscInitializeFortran();

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

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

 46:   ex7f_(&vec, &fcomm);

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

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

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

 65:   /* Some PETSc/MPI operations on Vec/Communicator objects */
 66:   *VecGetSize(*fvec, &vsize);
 67:   *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 process"
 79:       filter_output: sort -b

 81: TEST*/