Fortran/並行處理
外觀
< Fortran
並行性包含在 Fortran 2008 標準中。要使用並行功能,Fortran 程式必須在啟用並行的情況下進行編譯。例如,英特爾 ifort 編譯器使用標誌-coarray。
Fortran 使用分隔全域性地址空間 (PGAS) 模型進行並行處理。對於每個處理器,程式都作為程式的單獨重複“映像”執行,每個映像都有其自己的獨立記憶體分割槽。考慮以下程式
program hello
implicit none
write (*,*) 'Hello from ', this_image(), 'of', num_images()
end program hello
內在函式this_image返回正在執行的映像編號,內在函式num_images返回程式的總映像數。如果程式在 4 個處理器上編譯和執行,輸出可能如下所示
Hello from image 1 of 4 Hello from image 4 of 4 Hello from image 2 of 4 Hello from image 3 of 4
請注意,映像是非同步執行的,因此輸出可能不會按 1、2、3 然後 4 的順序顯示。
協陣列是在影像之間傳遞陣列資料的途徑。協陣列與普通陣列一樣,只是它為每個影像添加了額外的協維度。協維度可以使用方括號[]宣告和索引。例如,要宣告一個大小為 10 且協維度大小為 4 的秩為 1 的協陣列
real :: coarr(10)[4]
! Or you can use declaration attributes to do the same thing
real, dimension (10), codimension [4] :: another_coarr
標量變數也可以是協陣列
integer :: scalar[*]
這裡,* 表示可用的最大處理器數。協維度可以像普通維度一樣具有多個軸,但是,協維度的秩限制為 15。在影像之間傳遞資料就像在協維度上索引一樣簡單。
! Set all images to 1
coarr = 1
! Indexing
another_coarr(3)[4] = coarr(3)[3]