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: PetscFunctionBeginUser;
29: PetscCall(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: PetscCall(PetscInitializeFortran());
35: PetscCall(VecCreate(PETSC_COMM_WORLD, &vec));
36: PetscCall(VecSetSizes(vec, PETSC_DECIDE, m));
37: PetscCall(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: PetscCall(VecView(vec, PETSC_VIEWER_STDOUT_WORLD));
49: PetscCall(VecDestroy(&vec));
50: PetscCall(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: *ierr = VecGetSize(*fvec, &vsize);
67: if (*ierr) return;
68: if (MPI_Barrier(comm)) *ierr = PETSC_ERR_MPI;
69: }
71: /*TEST
73: build:
74: depends: ex7f.F
75: requires: fortran
77: test:
78: nsize: 3
79: filter: sort -b |grep -v " MPI process"
80: filter_output: sort -b
82: TEST*/