ナビゲーション:前へ 上へ 次へ
ナビゲーション:前へ 上へ 次へ
6 sections指示構文
お互いに依存関係のない処理をそれぞれ別のスレッドで並列に実行させたいような場合に sections指示構文が利用可能です。sections指示構文は以下のような書式を持ちます。
!$omp sections [指示節[[,] 指示節]...]
[!$omp section]
構造化ブロック
[!$omp section
構造化ブロック]
...
!$omp end sections [nowait]
※注意点としてはsections指示構文は複数形のsが語末に付きますが、
同指示構文内のそれぞれの部分は単数形のsection(sが付かない)が用いられます。
sections指示構文内の各sectionはそれぞれ別のスレッドで並列実行されます。 以下にその実行イメージ図を示します。
6.1 ☆演習課題:sections指示構文
下記に示すプログラムは2つの1次元配列(aとb)をそれぞれソートするプログラムです。 OpenMPのsections指示構文を利用して配列aと配列bのソート処理をそれぞれ別のスレッドで行うように書き換えて下さい。
[課題のコード]
program kadaiSections
implicit none
integer, parameter :: N = 10000
real,allocatable :: a(:), b(:)
allocate( a(N), b(N) )
call random_number(a)
call random_number(b)
call sort(a)
call sort(b)
print '("first 4 numbers in a : ",4f10.7)', a(1:4)
print '("first 4 numbers in b : ",4f10.7)', b(1:4)
contains
subroutine sort(x)
real x(:), tmp
integer i, j
do i = 1, N - 1
do j = i + 1, N
if (x(i)>x(j)) then
tmp = x(i)
x(i) = x(j)
x(j) = tmp
end if
end do
end do
end subroutine sort
end program
[実行出力例 - (小さい順になっていればOK)] first 4 numbers in a : 0.0000230 0.0003234 0.0003267 0.0004096 first 4 numbers in b : 0.0000900 0.0000905 0.0001305 0.0004416解答例:kadaiSections.f90
ナビゲーション:前へ 上へ 次へ
