Actual source code: ex7.c

  1: /*$Id: ex7.c,v 1.33 2001/03/23 23:21:37 balay Exp $*/

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

 7:  #include petscvec.h

  9: /*
 10:   Ugly stuff to insure the function names match between Fortran 
 11:   and C. Sorry, but this is out of our PETSc hands to cleanup.
 12: */
 13: #if defined(PETSC_HAVE_FORTRAN_CAPS)
 14: #define ex7f_ EX7F
 15: #define ex7c_ EX7C
 16: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
 17: #define ex7f_ ex7f
 18: #define ex7c_ ex7c
 19: #endif
 20: EXTERN_C_BEGIN
 21: EXTERN void PETSC_STDCALL ex7f_(Vec *,int*);
 22: EXTERN_C_END

 24: int main(int argc,char **args)
 25: {
 26:   int              ierr,m = 10;
 27:   int              fcomm;
 28:   Vec              vec;

 30:   PetscInitialize(&argc,&args,(char *)0,help);

 32:   /* This function should be called to be able to use PETSc routines
 33:      from the FORTRAN subroutines needed by this program */

 35:   PetscInitializeFortran();

 37:   VecCreate(PETSC_COMM_WORLD,&vec);
 38:   VecSetSizes(vec,PETSC_DECIDE,m);
 39:   VecSetFromOptions(vec);

 41:   /* 
 42:      Call Fortran routine - the use of MPICCommToFortranComm() allows 
 43:      translation of the MPI_Comm from C so that it can be properly 
 44:      interpreted from Fortran.
 45:   */
 46:   MPICCommToFortranComm(PETSC_COMM_WORLD,&fcomm);

 48:   ex7f_(&vec,&fcomm);

 50:   VecView(vec,PETSC_VIEWER_STDOUT_WORLD);
 51:   VecDestroy(vec);
 52:   PetscFinalize();
 53:   return 0;
 54: }

 56: EXTERN_C_BEGIN
 57: void PETSC_STDCALL ex7c_(Vec *fvec,int *fcomm,int* ierr)
 58: {
 59:   MPI_Comm comm;
 60:   int size;

 62:   /*
 63:     Translate Fortran integer pointer back to C and
 64:     Fortran Communicator back to C communicator
 65:   */
 66:   *MPIFortranCommToCComm(*fcomm,&comm);
 67: 
 68:   /* Some PETSc/MPI operations on Vec/Communicator objects */
 69:   *VecGetSize(*fvec,&size);
 70:   *MPI_Barrier(comm);

 72: }
 73: EXTERN_C_END