コード サンプルでは、外側のループのみが並列です。内側のループで印刷omp_get_thread_num()
してテストすると、特定の に対して、スレッド番号が同じであることがわかりますi
(もちろん、このテストは、実行が異なれば結果が異なるため、決定的なものではなく実証的なものです)。たとえば、次のようにします。
#include <stdio.h>
#include <omp.h>
#define dimension 4
int main() {
#pragma omp parallel for
for (int i = 0; i < dimension; i++)
for (int j = 0; j < dimension; j++)
printf("i=%d, j=%d, thread = %d\n", i, j, omp_get_thread_num());
}
私は得る:
i=1, j=0, thread = 1
i=3, j=0, thread = 3
i=2, j=0, thread = 2
i=0, j=0, thread = 0
i=1, j=1, thread = 1
i=3, j=1, thread = 3
i=2, j=1, thread = 2
i=0, j=1, thread = 0
i=1, j=2, thread = 1
i=3, j=2, thread = 3
i=2, j=2, thread = 2
i=0, j=2, thread = 0
i=1, j=3, thread = 1
i=3, j=3, thread = 3
i=2, j=3, thread = 2
i=0, j=3, thread = 0
コードの残りの部分については、新しい質問に詳細を記載することをお勧めします (小さなサンプルからは判断が難しい場合がありますprivate(j)
) j
。上記の例では、自動的にプライベートになります。diff
サンプルには見られない変数だと思います。また、ループ変数i
は自動的にプライベートになります (バージョン 2.5 仕様から- 3.0 仕様と同じ)。
for または parallel for コンストラクトの for ループ内のループ反復変数は、そのコンストラクト内でプライベートです。
編集:上記のすべては、あなたと私が示したコードに対して正しいですが、次のことに興味があるかもしれません. OpenMP バージョン 3.0 (たとえば、 gcc バージョン 4.4で使用できますが、バージョン 4.3 では使用できません) の場合collapse
、コードをそのまま記述できる節がありますが、
#pragma omp parallel for collapse (2)
両方の for ループを並列化します (仕様を参照)。
編集: OK、gcc 4.5.0 をダウンロードして上記のコードを実行しcollapse (2)
ましたが、次の出力を取得するために使用し、内部ループが並列化されたことを示しています。
i=0, j=0, thread = 0
i=0, j=2, thread = 1
i=1, j=0, thread = 2
i=2, j=0, thread = 4
i=0, j=1, thread = 0
i=1, j=2, thread = 3
i=3, j=0, thread = 6
i=2, j=2, thread = 5
i=3, j=2, thread = 7
i=0, j=3, thread = 1
i=1, j=1, thread = 2
i=2, j=1, thread = 4
i=1, j=3, thread = 3
i=3, j=1, thread = 6
i=2, j=3, thread = 5
i=3, j=3, thread = 7
ここのコメント(「回避策」を検索) は、両方のループを並列化したい場合のバージョン 2.5 の回避策にも関連していますが、上記のバージョン 2.5 の仕様は非常に明示的です (セクションA.35の非準拠の例を参照してください) 。 .