hello_image.f90 サンプルは Hello world アプリケーションです。通常の Hello world とは異なり、この Co-Array Fortran プログラムは、ホストマシンで平行して実行される複数のイメージ、またはプロセスにスポーンします。このアプリケーションのソースコードは、次のように単純な Fortran プログラムです。
program hello_image write(*,*) "Hello from image ", this_image(), & "out of ", num_images()," total images" end program hello_image
this_image() および num_images() の関数呼び出しに注目してください。これらは、新しい Fortran 2008 組込み関数です。num_images() 関数は、このプログラムでスポーンされたイメージまたはプロセス数の合計を返します。this_image() 関数は、1 から N の範囲の各イメージの一意の識別子を返します。N はこのプログラムで作成されるイメージの合計数です。
Co-Array Fortran 機能を含むこのサンプルをコンパイルするには、coarray コンパイラー・オプションを使用します。
ifort -coarray hello_image.f90 -o hello_image
hello_image ファイルの実行結果は、システムに搭載されているプロセッサー・コア数によって異なります。
./hello_image
出力結果は次のようになります。
Hello from image 1 out of 8 total images Hello from image 6 out of 8 total images Hello from image 7 out of 8 total images Hello from image 2 out of 8 total images Hello from image 5 out of 8 total images Hello from image 8 out of 8 total images Hello from image 3 out of 8 total images Hello from image 4 out of 8 total images
デフォルトでは、Co-Array Fortran アプリケーションがインテル® コンパイラーでコンパイルされると、ホスト・プラットフォームのプロセッサー・コア数と同じ数のイメージが作成されます。上記は、デュアル・クワッドコアで合計 8 コアを搭載するホストシステムで実行された例です。各イメージは個別にスポーンされたプロセスであり、非同期に実行します。
-coarray オプションと -qopenmp オプションを同時に使用しないでください。Co-Array Fortran 言語拡張と OpenMP* 言語拡張を混在して使用することはできません。