OpenMP 中數組約簡
問題:
問題:回答:
OpenMP 的確切支援陣列約簡。有兩種方法可以實現:
方法1:使用「私有」變數
int A[] = {84, 30, 95, 94, 36, 73, 52, 23, 2, 13}; int S[10] = {0}; #pragma omp parallel { int S_private[10] = {0}; #pragma omp for for (int n = 0; n < 10; ++n) { for (int m = 0; m <= n; ++m) { S_private[n] += A[m]; } } #pragma omp critical { for (int n = 0; n < 10; ++n) { S[n] += S_private[n]; } } }
為每個執行緒建立S 的私有副本,並行填充它們,然後在臨界區中將它們合併到S 中:
方法2:使用多維數組
int A[] = {84, 30, 95, 94, 36, 73, 52, 23, 2, 13}; int S[10] = {0}; int *S_private; #pragma omp parallel { const int nthreads = omp_get_num_threads(); const int ithread = omp_get_thread_num(); #pragma omp single { S_private = new int[10 * nthreads]; for (int i = 0; i < (10 * nthreads); i++) S_private[i] = 0; } #pragma omp for for (int n = 0; n < 10; ++n) { for (int m = 0; m <= n; ++m) { S_private[ithread * 10 + n] += A[m]; } } #pragma omp for for (int i = 0; i < 10; i++) { for (int t = 0; t < nthreads; t++) { S[i] += S_private[10 * t + i]; } } } delete[] S_private;
以上是如何在 OpenMP 中執行陣列縮減?的詳細內容。更多資訊請關注PHP中文網其他相關文章!