fortran66のブログ

fortran について書きます。

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

MFE §21.6.5 Allocatable objects

C 側で Fortran の allocatable 変数を deallocate/allocate します。

これを使えば、ZMQ の zmq_msg_init_data も出来ると思います。

fortran66.hatenablog.com

実行結果

gfortran-9/gcc-9

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

 x
allocatable
rank=2
lowerbound1 extent5 spacing4
allocated
dealloca retcode 0
unallocated
alloca retcode 0
 T           4           5
           0           1
           3           5
   1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000       1.00000000

icc/ifort

hp8@HP8:~/f2018$ icc -c mfe2165.c
hp8@HP8:~/f2018$ ifort mfe2165.o mfe2165.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


 x
allocatable
rank=2
lowerbound1 extent5 spacing4
allocated
dealloca retcode 0
unallocated
alloca retcode 0
 T           4           5
           0           1
           3           5
   1.000000       1.000000       1.000000       1.000000       1.000000
   1.000000       1.000000       1.000000       1.000000       1.000000
   1.000000       1.000000       1.000000       1.000000       1.000000
   1.000000       1.000000       1.000000       1.000000       1.000000

ソース・プログラム

program test
    use, intrinsic :: iso_fortran_env
    use, intrinsic :: iso_c_binding
    implicit none
    interface
        subroutine alloc(cdesc) bind(c, name = 'alloc')
            use, intrinsic :: iso_c_binding
            real, intent(in out), allocatable :: cdesc(..)
        end subroutine alloc
    end interface

    real, allocatable :: x(:, :)

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

    print *, 'x'

    allocate(x(5, 5))
    call alloc(x)
    print *, allocated(x), shape(x)
    print *, lbound(x)
    print *, ubound(x)
    x = 1.0
    print *, x
    deallocate(x)

end program test
#include <stdio.h>
#include "ISO_Fortran_binding.h"

int alloc(CFI_cdesc_t *cdesc) {
    CFI_attribute_t attrib = cdesc->attribute;
    if (attrib == CFI_attribute_allocatable) {printf("allocatable\n");};
    if (attrib == CFI_attribute_pointer    ) {printf("pointer\n");};
    if (attrib == CFI_attribute_other      ) {printf("other\n");};



    CFI_rank_t nrank = cdesc->rank;
    printf("rank=%d\n", (int)nrank);

    CFI_dim_t *dim = cdesc->dim;
    CFI_index_t lowerbound = dim->lower_bound;
    CFI_index_t extent     = dim->extent;
    CFI_index_t sm         = dim->sm;
    printf("lowerbound%d extent%d spacing%d\n", (int) lowerbound, (int) extent, (int) sm);


    if (cdesc->base_addr) {
        fputs("allocated\n", stderr);
        int iret = CFI_deallocate(cdesc);
        printf("dealloca retcode %d\n", iret);
    }

    fputs("unallocated\n", stderr);
    CFI_index_t lo[] = {0, 1};
    CFI_index_t hi[] = {3, 5};
    size_t len = 0;
    int iret = CFI_allocate(cdesc, lo, hi, len);
    printf("alloca retcode %d\n", iret);

}

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

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