Actual source code: ex7.c
petsc-3.13.6 2020-09-29
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: T*/
     15:  #include <petsc/private/fortranimpl.h>
     16: #if defined(PETSC_HAVE_FORTRAN_CAPS)
     17: #define ex7f_ EX7F
     18: #define ex7c_ EX7C
     19: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
     20: #define ex7f_ ex7f
     21: #define ex7c_ ex7c
     22: #endif

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

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

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

     37:   PetscInitializeFortran();

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

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

     50:   ex7f_(&vec,&fcomm);

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

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

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

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

     73: }

     75: /*TEST

     77:    build:
     78:      depends: ex7f.F
     79:      requires: fortran

     81:    test:
     82:       nsize: 3
     83:       filter: sort -b |grep -v "MPI processes"
     84:       filter_output: sort -b

     86: TEST*/