File: bindc_05.f90

package info (click to toggle)
lfortran 0.59.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 56,736 kB
  • sloc: cpp: 168,052; f90: 74,272; python: 17,537; ansic: 7,705; yacc: 2,345; sh: 1,334; fortran: 895; makefile: 37; javascript: 15
file content (56 lines) | stat: -rw-r--r-- 1,587 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module bindc_05_mod
    use iso_c_binding, only: c_int, c_ptr, c_f_pointer, c_null_ptr, c_associated
    implicit none

    interface
        function ax(comm_f) bind(C, name="ax")
            import :: c_int, c_ptr
            integer(c_int), value :: comm_f
            type(c_ptr) :: ax
        end function ax
    end interface

contains

    subroutine MPI_Barrier(comm, ierror)
        integer, intent(in) :: comm
        integer, intent(out), optional :: ierror
        type(c_ptr) :: c_comm
        integer, pointer :: fortran_ptr

        ! Call the C function
        c_comm = ax(comm)
        if (.not. c_associated(c_comm)) then
            print *, "Error: Null pointer returned from C function"
            if (present(ierror)) ierror = 1
            return
        end if

        ! Convert C pointer to Fortran pointer
        call c_f_pointer(c_comm, fortran_ptr)
        print *, "Fortran received value:", fortran_ptr

        ! Verify the value matches what was passed
        if (fortran_ptr /= comm) then
            print *, "Error: Value mismatch"
            if (present(ierror)) ierror = 1
        else
            if (present(ierror)) ierror = 0
        end if
    end subroutine MPI_Barrier
end module bindc_05_mod

program bindc_05
    use bindc_05_mod
    implicit none
    integer, parameter :: MPI_COMM_WORLD = 42  ! Non-zero value
    integer :: ierr

    call MPI_Barrier(MPI_COMM_WORLD, ierr)
    if (ierr == 0) then
        print *, "Test passed successfully"
    else
        print *, "Test failed"
        error stop
    end if
end program bindc_05