fortran66のブログ

fortran について書きます。Amazonのアソシエイトとして収入を得ています。

【メモ帳】mpi_f08 module の mpi module との微妙な違い

mpi_f08

MPI は FORTRAN77 では INCLUDE 文で、Fortran90 では mpi module で常数その他を定義していましたが、その後 MPI 08 が出ておりました。私は 90 用の MPI に双方向通信などを加えて拡充した程度のものだと、ずっと思っていましたが、引数の型が整数から派生型に変わっていたり細かいところで、強い型付け方向に向かっていたようです。昔のプログラムの use mpi を use mpi_f08 に直しただけでは、型が違うと色々怒られたりします。

勉強ついでに、パチェコの本のC言語の例題を MPI 08 利用の Fortran に直しつつ、ついでに coarray fortran でも書いてみようかと、ふと思ったのですが、続けるかわかりません。パチェコの本の本文は C 言語で書かれていますが、FORTRAN77 版がウェブページにあった気がします。

最初の一等易しい例題

Chap. 3

mpi の status の引数型が、 integer から type(MPI_STATUS) に変わっています。

MPI 08

program ex31
    use :: mpi_f08
    implicit none
    integer :: irank, nrank
    integer :: isource, idest, itag, ierr
    character(len = 100) :: message
    type(MPI_STATUS) :: mstatus

    itag = 0
    call mpi_init(ierr)
    call mpi_comm_rank(MPI_COMM_WORLD, irank)
    call mpi_comm_size(MPI_COMM_WORLD, nrank)

    if (irank /= 0) then
        write(message, '(a, i3.2, a)') 'Greetings from process', irank, '!'
        idest = 0
        call mpi_send(message, 100, MPI_CHAR, idest, itag, MPI_COMM_WORLD)
    else
        do isource = 1, nrank - 1
            call mpi_recv(message, 100, MPI_CHAR, isource, itag, MPI_COMM_WORLD, mstatus)
            print *, trim(message)
        end do
    end if

    call mpi_finalize()
end program ex31
(base) dyna@dyna:~/CAF$ mpiifx ex31.f90
(base) dyna@dyna:~/CAF$ mpirun -n 16 ./a.out
 Greetings from process 01!
 Greetings from process 02!
 Greetings from process 03!
 Greetings from process 04!
 Greetings from process 05!
 Greetings from process 06!
 Greetings from process 07!
 Greetings from process 08!
 Greetings from process 09!
 Greetings from process 10!
 Greetings from process 11!
 Greetings from process 12!
 Greetings from process 13!
 Greetings from process 14!
 Greetings from process 15!

coarray fortran

program ex31caf
    implicit none
    integer :: me, ne, isource
    character(len = 100) :: message[*]

    me = this_image()
    ne = num_images()

    if (me /= 1) then
        write(message, '(a, i3.2, a)') 'Greetings from image', me, '!'
        sync images(1)
    else
        do isource = 2, ne
            sync images(isource)
            message = message[isource]
            print *, trim(message)
        end do
    end if
end program ex31caf
(base) dyna@dyna:~/CAF$ ifx -coarray ex31caf.f90
(base) dyna@dyna:~/CAF$ ./a.out
 Greetings from image 02!
 Greetings from image 03!
 Greetings from image 04!
 Greetings from image 05!
 Greetings from image 06!
 Greetings from image 07!
 Greetings from image 08!
 Greetings from image 09!
 Greetings from image 10!
 Greetings from image 11!
 Greetings from image 12!
 Greetings from image 13!
 Greetings from image 14!
 Greetings from image 15!
 Greetings from image 16!