fortran66のブログ

fortran について書きます。

【寝言】ペンス副大統領の反中演説ようやく!?

ペンス演説第二段

当初、天安門事件30周年の日、六月四日にする予定で、その後延び延びになっていた、マイク・ペンス米副大統領の反中演説が、十月一日の中共建国記念日に向けて行われるようです。

www.youtube.com

去年十月にもハドソン研究所で画期的な演説をしていますので、期待が持てます。はやく完全なる封じ込めを宣言して欲しいです。

fortran66.hatenablog.com

fortran66.hatenablog.com

www.sankei.com

f:id:fortran66:20190907232343j:plain
マイク・ペンス

 【ワシントン=黒瀬悦成】米ホワイトハウスのミラー副大統領副報道官は6日、ペンス副大統領が中国に関する政策演説を今秋、ワシントン市内の政策研究機関「ウィルソン・センター」で行うことを明らかにした。中国の習近平体制による新疆ウイグル自治区ででの人権抑圧や香港情勢などで習体制を批判する内容になるとみられ、貿易や安全保障に加え、人権・民主化問題でも中国に全面的圧力を加えていく立場を鮮明に打ち出す考えだ。

 ペンス氏の演説は当初、6月に予定されていたが、同月末にトランプ大統領と中国の習近平国家主席との首脳会談を控え、米中の貿易交渉が一つの山場を迎えていたため、交渉進展を優先させる思惑から延期された経緯がある。

 ペンス氏は昨年10月にも政策研究機関「ハドソン研究所」で、米政権による中国との「全面対決」を宣言する演説を行っており、今度の演説はその「第2弾」に位置づけられる。

 ペンス氏は、最近では香港情勢に関し、中国が米国と貿易問題で合意したいのであれば、1984年の中英共同宣言に基づく香港の自治権を尊重すべきだと指摘し、中国が抗議デモを武力鎮圧すれば「合意は困難となる」と警告した。

 一方、ポンペオ国務長官は6日、中西部カンザス州の大学で講演し、今月中旬からニューヨークで始まる国連総会の場で、中国による新疆ウイグル自治区での住民弾圧に関し、各国に「中国糾弾」を呼びかけていく考えを明らかにした。

 ポンペオ氏は、中国当局自治区住民らに対する処遇は「今世紀の世界における最悪の汚点になる可能性がある」と指摘し、「米国は彼らに自由がもたらされることを望む。ことは彼らの基本的かつ誰にも奪うことのできない権利に関わる問題だ」と強調した。

【メモ帳】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)

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

Modern Fortran Explained - 2018 §21.5 C descriptors

暑いので適当w interface に type(*) :: x(..) を使ったのが新奇。 

attribute がうまく取れていないのが謎。

実行結果

gfortran-9/gcc-9

hp8@HP8:~/f2018$ gfortran-9 c_mfe215b.c mfe215b.f90
hp8@HP8:~/f2018$ ./a.out
 -mtune=generic -march=x86-64

other
rank=0
lowerbound0 extent0 spacing0
CFI_type_double

other
rank=2
lowerbound0 extent10 spacing4
CFI_type_float

other
rank=0
lowerbound0 extent10 spacing4
CFI_type_double

other
rank=1
lowerbound0 extent100 spacing4
CFI_type_float

other
rank=0
lowerbound0 extent100 spacing4
CFI_type_float

STOP normal termination

ifort/icc

hp8@HP8:~/f2018$ vi mfe215b.f90
hp8@HP8:~/f2018$ icc -c   c_mfe215b.c
hp8@HP8:~/f2018$ ifort   c_mfe215b.o mfe215b.f90
hp8@HP8:~/f2018$ ./a.out


other
rank=0
lowerbound13 extent1886220131 spacing1
CFI_type_double

other
rank=2
lowerbound0 extent10 spacing4
CFI_type_float

other
rank=0
lowerbound469778721 extent-604196248 spacing26577600
CFI_type_double

other
rank=1
lowerbound0 extent100 spacing4
CFI_type_float

other
rank=0
lowerbound0 extent0 spacing0
CFI_type_float

normal termination

ソース・プログラム

fortran

program test
    use, intrinsic :: iso_fortran_env
    use, intrinsic :: iso_c_binding
    implicit none
    interface
        subroutine cfi_members(string) bind(c, name = 'cfi_members')
            use, intrinsic :: iso_c_binding
            type(*) :: string(..)
        end subroutine cfi_members
    end interface

    real(kind=kind(1.0d0)), target :: a
    real :: b(10, 10)
    real(kind=kind(1.0d0)), pointer :: p
    real, allocatable :: x(:), y
    integer :: i, j

    print *, compiler_options()
    print *

    a = 1.0
    call cfi_members(a)

    forall(i=1:10, j=1:10) b(i, j) = i + j
    call cfi_members(b)

    p => a
    call cfi_members(p)

    x = [(i, i = 1, 100)]
    call cfi_members(x)

    call cfi_members(y)

    stop 'normal termination'
end program test

C

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

void cfi_members(CFI_cdesc_t *string) {
    CFI_attribute_t attrib = string->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 = string->rank;
    printf("rank=%d\n", (int)nrank);

    CFI_dim_t *dim = string->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);

    CFI_type_t ctype = string->type;
    switch (ctype) {
        case CFI_type_int:
//        case CFI_type_int32_t:
            printf("CFI_type_int\n");
            break;
        case CFI_type_Bool:
            printf("CFI_type_Bool\n");
            break;
        case CFI_type_float:
            printf("CFI_type_float\n");
            break;
        case CFI_type_double:
//        case CFI_type_long_double:
            printf("CFI_type_double\n");
            break;
        case CFI_type_cptr:
            printf("CFI_type_cptr\n");
            break;
//        case CFI_type_int64_t:
//        case CFI_type_long:
//        case CFI_type_long_long:
//        case CFI_type_size_t:
//        case CFI_type_intmax_t:
//        case CFI_type_intptr_t:
//          case CFI_type_ptrdiff_t:

        case CFI_type_short:
//        case CFI_type_int16_t:
            printf("CFI_type_short\n");
            break;
        case CFI_type_int8_t:
//        case CFI_type_signed_char:
            printf("CFI_type_signed_char\n");
            break;
// undefined        case CFI_type_least8_t:
//        case CFI_type_least16_t:
//        case CFI_type_least32_t:
//        case CFI_type_least64_t:
//        case CFI_type_fast8_t:
//        case CFI_type_fast16_t:
//        case CFI_type_fast32_t:
//        case CFI_type_fast64_t:
        case CFI_type_struct:
            printf("CFI_type_struct\n");
            break;
        case CFI_type_other:
            printf("CFI_type_other\n");
            break;
        case CFI_type_float_Complex:
            printf("CFI_type_float_Complex\n");
            break;
        case CFI_type_double_Complex:
//        case CFI_type_long_double_Complex:
            printf("CFI_type_double_Complex\n");
            break;
        case CFI_type_char:
            printf("CFI_type_char\n");
            break;
        default:
            printf("case default\n");
    }



    printf("\n");
}

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

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

【寝言】覗き魔 google 通販履歴貯めてるw その他

Googleの「自分のデータをダウンロード」

お盆休みに、google の「自分のデータをダウンロード」を、ほんの一部だけ見てみたのですが、通販での買い物履歴が json データになってたまっていてムカつきました。amazon などは別メアドなのに、スパムフィルター代わりに gmail を通していたら、メールの中身を読んでいて amazon の買い物記録もバッチシまとめられていました。ヤマジュンのホモマンガとか。

labaq.com

ウホッ!!いい男たち~ヤマジュン・パーフェクト

ウホッ!!いい男たち~ヤマジュン・パーフェクト

緊急地震速報

緊急地震速報の「ギュッ!ギュッ! 地震が来ます。 ギュッ!ギュッ! 地震が来ます。」というのを聞くと、いつも魔法陣グルグルの魔物を知らせる人形を思い出します。

f:id:fortran66:20190907012959p:plain
魔法陣グルグル

EMOTION the Best 魔法陣グルグル DVD-BOX 1

EMOTION the Best 魔法陣グルグル DVD-BOX 1

月と木星

今日は上弦くらいの月の下のすぐ近くに木星が見えていました。とてもきれいでした。

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

Modern Fortran Explained - 2018 §21.4 assumed character length

Fortran の文字列は文字の配列ではなく、文字列長などの付加的な情報を持っています。その情報は、C言語側で Fortran 2018 が用意したヘッダーファイルを include することで、構造体の要素として利用できるようになります。下の例で文字列長を利用しています。

c言語側で ISO_Fortran_binding.h というヘッダーファイルを読み込む必要がありますが、うまく include ディレクトリに配置してあるようで、特にコンパイル時の付加的なオプション指定は必要ないようです。

ソース・プログラム

fortran

program test
    use, intrinsic :: iso_fortran_env
    use, intrinsic :: iso_c_binding
    implicit none
    interface
        subroutine err_msg(string) bind(c, name = 'err_msg')
            use, intrinsic :: iso_c_binding
            character(len=*, kind = c_char) :: string
        end subroutine err_msg
    end interface

    character(*), parameter :: text = '***fortran characters***'

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

    call err_msg(text)

    call err_msg('ABEND: abnormal end')

    stop 'normal termination'
end program test

C

MFE のテキストでは iso_fortran_binding.h とすべて小文字になっていますが、一部大文字にしないと読んでくれませんでした。 「ISO_Fortran_binding.h」

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

void err_msg(CFI_cdesc_t *string) {
    char *p = string->base_addr;
    for (int len = string->elem_len; len > 0; len--) putc(*p++, stderr);
    putc('\n', stderr);
}

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

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

実行結果

gfortran-9 はバグがあります。 他の例題を見ているうち、なんとなくこういう仕様の気がしてきたw

ifort/icc は通ります。

gfortran-9

fortran の文字列は文字の配列ではないので、c 言語との互換の場合に文字の配列に制限するというが残ってしまっているようです。

hp8@HP8:~/f2018$ gfortran-9 mfe214.f90 c_mfe214.o -Wall
mfe214.f90:6:33:

    6 |         subroutine err_msg(string) bind(c, name = 'err_msg')
      |                                 1
Error: Character argument ‘string’ at (1) must be length 1 because procedure ‘err_msg’ is BIND(C)

icc/ifort

「ABEND: abnormal end」というのは冗談です。

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

***fortran characters***
ABEND: abnormal end
normal termination

【寝言】「支那」という語について

宋代の『翻訳名義集』に現れた支那

国会図書館デジタルライブラリ『翻訳名義集 : 支那撰述. 巻第3』17 コマ

dl.ndl.go.jp 国立国会図書館オンライン | National Diet Library Online

「脂那」こっちの字面の方が、下水油で脂ぎった支那料理のようで、お似合いですね。

一に支那と云う。此れ文物国を云う。すなわち此の方を是れ衣冠文物の地として賛美するなり。

つまり「支那」は美称w

f:id:fortran66:20190905233005j:plain
翻訳名義集 : 支那撰述. 巻第3

正史に現れた「支那

fortran66.hatenablog.com

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

Modern Fortran Explained - 2018

Fortran2018 で導入された Fortran と C の interoperability について、少しづつ試してゆきたいと思います。 MFE にはコード断片しかないので、残りを適当に埋めて、実行可能な形にして試していみることにします。

§ 21.2 Fortran optinal 引数と C 言語の通用

C 言語の関数 strtod (string to double) をインターフェースだけ書いて呼び出します。C 言語には文法上 optional 引数は無いのですが、null を送ることで optional 引数を実現しているようです。組み込みライブラリ関数 strtod はそのような仕組みを用いているようです。

参照: C言語関数辞典 - strtod

strtod は第一引数に文字列を与えると、その文字列先頭部を倍精度の数値として解釈して倍精度実数として返します。第二引数は optional で、ポインタを与えると、数値として解釈できなくなったところから文字列を返します。

ソース・プログラム

ポインタを Fortran 文字列にするのがこれでいいのかどうか。

module test_m
    implicit none
    interface
        function strtod(string, final) bind(c, name='strtod')
            use, intrinsic :: iso_c_binding
            character(kind=c_char), intent(in) :: string(*)
            type(c_ptr), optional, intent(in) :: final
            real(c_double) :: strtod
        end function strtod
    end interface
end module test_m


program main
    use, intrinsic :: iso_c_binding
    use test_m
    implicit none
    real(c_double) :: x, y
    type(c_ptr) :: yfinal
    character(80), pointer :: text

    x = strtod("3.1415926e2"//c_null_char)
    y = strtod("3.1415926d2"//c_null_char, yfinal)

    print *, x
    print *, y
    call c_f_pointer(yfinal, text)
    print *, text(:index(text, c_null_char) - 1)
end program main

実行結果

3.1415926e2 は e2 が 102 と解釈されましたが、3.1415926d2 は d2 の部分が解釈されずに吐き出されました。

gfortran 7,8,9

hp8@HP8:~/f2018$ gfortran-7 strtod.F90
hp8@HP8:~/f2018$ ./a.out
   314.15926000000002
   3.1415926000000001
 d2

ifort 19 文字列でない・・・

   314.159260000000
   3.14159260000000

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

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

C から Fortran の optional 引数を使う場合

ソース・プログラム

module test_m
    implicit none
contains
    subroutine fortran_note(main, minor) bind(c, name='fortran_note')
        use, intrinsic :: iso_c_binding
        integer(c_int), value :: main
        integer(c_int), optional :: minor
        if (present(minor)) then
            print '(1x, "Note ", I0, ".", I0)', main, minor
        else
            print '(1x, "Note ", I0)', main
        end if
    end subroutine fortran_note
end module test_m
void fortran_note(int main, int* minor);

int main(void) {
    fortran_note(10, (int *)0);

    int subnote = 3;
    fortran_note(10, &subnote);

    return 0;
}

実行結果

gfortran 7,8,9

hp8@HP8:~/f2018$ gfortran c_note.c note.f90
hp8@HP8:~/f2018$ ./a.out
 Note 10
 Note 10.3

ifort/icc 適せつなリンクの仕方が分からない・・・

icc からは fortran I/O ランタイムが呼べず、ifort では main が無いと言われる・・・