1module ex43fmodule 2#include <petsc/finclude/petscvec.h> 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 18#include <petsc/finclude/petscvec.h> 19use, intrinsic :: iso_c_binding 20use petscvec 21use ex43fmodule 22implicit none 23! 24! This routine demonstrates how to call a bind C function from Fortran 25Vec v 26PetscErrorCode ierr 27PetscInt five 28! 29! We need to use the same iso_c_binding variable types here or some compilers 30! will see a type mismatch in the call to fillupvector and thus not link 31! 32integer(c_long_long) vaddr 33integer(c_int) err 34 35PetscCallA(PetscInitialize(ierr)) 36PetscCallA(VecCreate(PETSC_COMM_WORLD, v, ierr)) 37five = 5 38PetscCallA(VecSetSizes(v, PETSC_DECIDE, five, ierr)) 39PetscCallA(VecSetFromOptions(v, ierr)) 40! 41! Now call a PETSc routine from Fortran 42! 43! 44vaddr = v%v 45call fillupvector(vaddr, err) 46 47PetscCallA(VecView(v, PETSC_VIEWER_STDOUT_WORLD, ierr)) 48PetscCallA(VecDestroy(v, ierr)) 49PetscCallA(PetscFinalize(ierr)) 50end 51 52!/*TEST 53! 54! build: 55! depends: ex43.c 56! 57! test: 58! 59!TEST*/ 60