fortran66のブログ

fortran について書きます。

【メモ帳】ZMQ helper

stopwatch

github.com

program test
    use m_zmq
    implicit none
    type(c_ptr), pointer :: watch
    integer :: itime

    allocate(watch)
    print *, 'start watch'
    watch = zmq_stopwatch_start()
    call zmq_sleep(1)
    itime = zmq_stopwatch_intermediate(watch)
    print *, 'time=', itime
    call zmq_sleep(2)
    itime = zmq_stopwatch_stop(watch)
    print *, 'time=', itime
    print *, 'stop  watch'
    deallocate(watch)
end program test
 start watch
 time=     1000202
 time=     3001664
 stop  watch

thread

module m_sub
    use, intrinsic :: iso_c_binding
    implicit none
contains
    subroutine test(arg) bind(c, name = 'test')
        implicit none
        type(c_ptr), value :: arg
        integer, pointer :: n
        integer :: i
        call c_f_pointer(arg, n)
        do i = 1, n
            print *, 'thread', i
        end do
    end subroutine test

end module m_sub

program thread
    use :: m_zmq
    use :: m_sub
    implicit none
    type(c_ptr), pointer :: th
    integer, target :: n = 10
    integer:: i
    allocate(th)
    th = zmq_threadstart(c_funloc(test), c_loc(n))
!    call sleep(1)
    do i = 1, n
        print *, 'main  ', i
    end do
    call zmq_threadclose(th)
    deallocate(th)
end program thread
 main             1
 thread           1
 main             2
 thread           2
 main             3
 thread           3
 main             4
 thread           4
 main             5
 thread           5
 main             6
 thread           6
 main             7
 thread           7
 main             8
 thread           8
 main             9
 thread           9
 main            10
 thread          10

Modern Fortran Explained: Incorporating Fortran 2018 (Numerical Mathematics and Scientific Computation)

Modern Fortran Explained: Incorporating Fortran 2018 (Numerical Mathematics and Scientific Computation)