Actual source code: ex7.c
1: static char help[] = "Demonstrates calling a Fortran computational routine from C.\n\
2: Also demonstrates passing PETSc objects, MPI Communicators from C to Fortran\n\
3: and from Fortran to C\n\n";
5: #include <petscvec.h>
6: /*
7: Ugly stuff to insure the function names match between Fortran
8: and C. This is out of our PETSc hands to cleanup.
9: */
10: #if defined(PETSC_HAVE_FORTRAN_CAPS)
11: #define ex7f_ EX7F
12: #define ex7c_ EX7C
13: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
14: #define ex7f_ ex7f
15: #define ex7c_ ex7c
16: #endif
18: PETSC_INTERN void ex7f_(Vec *, int *);
20: int main(int argc, char **args)
21: {
22: PetscInt m = 10;
23: int fcomm;
24: Vec vec;
26: PetscFunctionBeginUser;
27: PetscCall(PetscInitialize(&argc, &args, NULL, help));
28: /* This function should be called to be able to use PETSc routines
29: from the FORTRAN subroutines needed by this program */
31: PetscCall(PetscInitializeFortran());
33: PetscCall(VecCreate(PETSC_COMM_WORLD, &vec));
34: PetscCall(VecSetSizes(vec, PETSC_DECIDE, m));
35: PetscCall(VecSetFromOptions(vec));
37: /*
38: Call Fortran routine - the use of MPI_Comm_c2f() allows
39: translation of the MPI_Comm from C so that it can be properly
40: interpreted from Fortran.
41: */
42: fcomm = MPI_Comm_c2f(PETSC_COMM_WORLD);
44: ex7f_(&vec, &fcomm);
46: PetscCall(VecView(vec, PETSC_VIEWER_STDOUT_WORLD));
47: PetscCall(VecDestroy(&vec));
48: PetscCall(PetscFinalize());
49: return 0;
50: }
52: PETSC_INTERN void ex7c_(Vec *fvec, int *fcomm, PetscErrorCode *ierr)
53: {
54: MPI_Comm comm;
55: PetscInt vsize;
57: /*
58: Translate Fortran integer pointer back to C and
59: Fortran Communicator back to C communicator
60: */
61: comm = MPI_Comm_f2c(*fcomm);
63: /* Some PETSc/MPI operations on Vec/Communicator objects */
64: *ierr = VecGetSize(*fvec, &vsize);
65: if (*ierr) return;
66: if (MPI_Barrier(comm)) *ierr = PETSC_ERR_MPI;
67: }
69: /*TEST
71: build:
72: depends: ex7f.F
73: requires: fortran
75: test:
76: nsize: 3
77: filter: sort -b |grep -v " MPI process"
78: filter_output: sort -b
80: TEST*/