Date: 2026-07-30T21:14:35+00:00

Actual source code: ex43f.F90

      1: #include <petsc/finclude/petscvec.h>
      2: module ex43fmodule
      3:   use, intrinsic :: iso_c_binding
      4:   implicit none
      5:   interface
      6:     subroutine fillupvector(vaddr, err) bind(C, name='fillupvector')
      7: !
      8: !     We need to use iso_c_binding variables or otherwise we get compiler warnings
      9: !     Warning: Variable 'vaddr' at (1) is a dummy argument of the BIND(C)
     10: !              procedure 'fillupvector' but may not be C interoperable
     11: !
     12:       use, intrinsic :: iso_c_binding
     13:       integer(c_long_long) vaddr
     14:       integer(c_int) err
     15:     end subroutine fillupvector
     16:   end interface
     17: end module

     19: program ex43f
     20:   use, intrinsic :: iso_c_binding
     21:   use petscvec
     22:   use ex43fmodule
     23:   implicit none
     24: !
     25: !  This routine demonstrates how to call a bind C function from Fortran
     26:   Vec v
     27:   PetscErrorCode ierr
     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: !
     32:   integer(c_long_long) vaddr
     33:   integer(c_int) err

     35:   PetscCallA(PetscInitialize(ierr))
     36:   PetscCallA(VecCreate(PETSC_COMM_WORLD, v, ierr))
     37:   PetscCallA(VecSetSizes(v, PETSC_DECIDE, 5_PETSC_INT_KIND, ierr))
     38:   PetscCallA(VecSetFromOptions(v, ierr))
     39: !
     40: !     Now call a PETSc routine from Fortran
     41: !
     42: !
     43:   vaddr = v%v
     44:   call fillupvector(vaddr, err)

     46:   PetscCallA(VecView(v, PETSC_VIEWER_STDOUT_WORLD, ierr))
     47:   PetscCallA(VecDestroy(v, ierr))
     48:   PetscCallA(PetscFinalize(ierr))
     49: end program ex43f

     51: !/*TEST
     52: !
     53: !   build:
     54: !     depends: ex43.c
     55: !
     56: !   test:
     57: !
     58: !TEST*/