xref: /petsc/src/vec/vec/tutorials/ex43f.F90 (revision d52a580b706c59ca78066c1e38754e45b6b56e2b)
1#include <petsc/finclude/petscvec.h>
2module ex43fmodule
3  use, intrinsic :: iso_c_binding
4  interface
5    subroutine fillupvector(vaddr, err) bind(C, name='fillupvector')
6!
7!     We need to use iso_c_binding variables or otherwise we get compiler warnings
8!     Warning: Variable 'vaddr' at (1) is a dummy argument of the BIND(C)
9!              procedure 'fillupvector' but may not be C interoperable
10!
11      use, intrinsic :: iso_c_binding
12      integer(c_long_long) vaddr
13      integer(c_int) err
14    end subroutine fillupvector
15  end interface
16end module
17
18use, intrinsic :: iso_c_binding
19use petscvec
20use ex43fmodule
21implicit none
22!
23!  This routine demonstrates how to call a bind C function from Fortran
24Vec v
25PetscErrorCode ierr
26PetscInt five
27!
28!     We need to use the same iso_c_binding variable types here or some compilers
29!     will see a type mismatch in the call to fillupvector and thus not link
30!
31integer(c_long_long) vaddr
32integer(c_int) err
33
34PetscCallA(PetscInitialize(ierr))
35PetscCallA(VecCreate(PETSC_COMM_WORLD, v, ierr))
36five = 5
37PetscCallA(VecSetSizes(v, PETSC_DECIDE, five, ierr))
38PetscCallA(VecSetFromOptions(v, ierr))
39!
40!     Now call a PETSc routine from Fortran
41!
42!
43vaddr = v%v
44call fillupvector(vaddr, err)
45
46PetscCallA(VecView(v, PETSC_VIEWER_STDOUT_WORLD, ierr))
47PetscCallA(VecDestroy(v, ierr))
48PetscCallA(PetscFinalize(ierr))
49end
50
51!/*TEST
52!
53!   build:
54!     depends: ex43.c
55!
56!   test:
57!
58!TEST*/
59