fortran66のブログ

fortran について書きます。

【メモ帳】Fortran 2018 C言語の相互運用 MFE §21.6

MFE §21.6 Accessing Fortran objects

~21.6.4

実行結果

gfortran-9

hp8@HP8:~/f2018$ gfortran-9 c_mfe216.c mfe216.f90
hp8@HP8:~/f2018$ ./a.out
 GCC version 9.1.0
 -mtune=generic -march=x86-64

 x
contiguous
associated

 ip => null()
discontiguous
disassociated

 ip => m(::2)
discontiguous
associated

ifort

hp8@HP8:~/f2018$ icc -c c_mfe216.c
hp8@HP8:~/f2018$ ifort c_mfe216.o mfe216.f90
hp8@HP8:~/f2018$ ./a.out
 Intel(R) Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64,
 Version 19.1.0.056 Pre-Release Beta Build 20190321
 -traceback

 x
contiguous
associated

 ip => null()
discontiguous
disassociated

 ip => m(::2)
discontiguous
associated

ソース・プログラム

Fortran

program test
    use, intrinsic :: iso_fortran_env
    use, intrinsic :: iso_c_binding
    implicit none
    interface
        subroutine iscont(cdesc) bind(c, name = 'iscont')
            use, intrinsic :: iso_c_binding
            type(*), intent(in) :: cdesc(..)
        end subroutine iscont
    end interface

    real :: x(10) = 1.0
    integer, target  :: m(10) = 8
    integer, pointer :: ip(:) => null()

    print *, compiler_version()
    print *, compiler_options()
    print *

    print *, 'x'
    call iscont(x)

    print *, 'ip => null()'
    call iscont(ip)

    print *, 'ip => m(::2)'
    ip => m(::2)
    call iscont(ip)

end program test

C

#include <stdio.h>
#include "ISO_Fortran_binding.h"

int iscont(CFI_cdesc_t *cdesc) {
    if (CFI_is_contiguous(cdesc)) {
        fputs("contiguous\n", stderr);
    } else {
        fputs("discontiguous\n", stderr);
    }

    if (cdesc->base_addr) {
        fputs("associated\n\n", stderr);
    } else {
        fputs("disassociated\n\n", stderr);
    }
}

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

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