Actual source code: ex7.c

petsc-master 2017-04-26
Report Typos and Errors

  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. Sorry, but this is out of our PETSc hands to cleanup.
 10: */
 11: /*T
 12:    Concepts: vectors^fortran-c;
 13:    Processors: n
 14:    depends: ex7f.F
 15: T*/
 16:  #include <petsc/private/fortranimpl.h>
 17: #if defined(PETSC_HAVE_FORTRAN_CAPS)
 18: #define ex7f_ EX7F
 19: #define ex7c_ EX7C
 20: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
 21: #define ex7f_ ex7f
 22: #define ex7c_ ex7c
 23: #endif

 25: PETSC_EXTERN void PETSC_STDCALL ex7f_(Vec*,int*);

 27: int main(int argc,char **args)
 28: {
 30:   PetscInt       m = 10;
 31:   int            fcomm;
 32:   Vec            vec;

 34:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 35:   /* This function should be called to be able to use PETSc routines
 36:      from the FORTRAN subroutines needed by this program */

 38:   PetscInitializeFortran();

 40:   VecCreate(PETSC_COMM_WORLD,&vec);
 41:   VecSetSizes(vec,PETSC_DECIDE,m);
 42:   VecSetFromOptions(vec);

 44:   /*
 45:      Call Fortran routine - the use of MPI_Comm_c2f() allows
 46:      translation of the MPI_Comm from C so that it can be properly
 47:      interpreted from Fortran.
 48:   */
 49:   fcomm = MPI_Comm_c2f(PETSC_COMM_WORLD);

 51:   ex7f_(&vec,&fcomm);

 53:   VecView(vec,PETSC_VIEWER_STDOUT_WORLD);
 54:   VecDestroy(&vec);
 55:   PetscFinalize();
 56:   return ierr;
 57: }

 59: PETSC_EXTERN void PETSC_STDCALL ex7c_(Vec *fvec,int *fcomm,PetscErrorCode *ierr)
 60: {
 61:   MPI_Comm comm;
 62:   PetscInt vsize;

 64:   /*
 65:     Translate Fortran integer pointer back to C and
 66:     Fortran Communicator back to C communicator
 67:   */
 68:   comm = MPI_Comm_f2c(*fcomm);

 70:   /* Some PETSc/MPI operations on Vec/Communicator objects */
 71:   *VecGetSize(*fvec,&vsize);
 72:   *MPI_Barrier(comm);

 74: }