2014-04-14 173 views
1

我在OpenMP中創建了幾個任務,但由於某些原因,這些任務由同一個線程執行。該代碼具有以下模式:OpenMP任務 - 阻止特定線程執行任務的方式?

#pragma omp parallel num_threads(n_threads) 

#pragma omp single 
while(!found){ 

      /.... operations with k, e, y, dist, u, step 

      #pragma omp task firstprivate(k, e, y, dist, u, step) 
      process(k, e, y, dist, u, step, h, out); 

} 

是否有強迫OpenMP運行時系統分配的任務的所有線程的任何方式,但它創建的任務嗎?

感謝

編輯:

void process(int a[]){ 
    printf("Thread %d processing\n", omp_get_thread_num()); 
    /* Heavy Operations (used sleep to simulate them)*/ 
    sleep(10); 
} 

int main(){ 
    int a[10]; 
    int i, j; 
    #pragma omp parallel num_threads(4) 
    { 
     #pragma omp single 
     { 
      for (j = 0; j < 10; j++) { 
       /* Compute a */ 
       for (i = 0; i < 10; i++) { 
        a[i] += 1; 
       } 
       #pragma omp task firstprivate(a) 
       process(a); 
      } 
      printf("Thread %d finished preprocessing\n", omp_get_thread_num()); 
     } 
    } 
} 

輸出:

Thread 1 processing 
Thread 1 processing 
Thread 1 processing 
Thread 1 processing 
Thread 1 processing 
Thread 1 processing 
Thread 1 processing 
Thread 1 processing 
Thread 1 processing 
Thread 1 processing 
Thread 1 finished preprocessing 

回答

0

是不是同樣的問題在您的其他職位(Task scheduling points of OpenMP tasks

這個問題的答案其中一個是:不,您無法控制OpenMP實施如何分配任務t o平行團隊的OpenMP線程。

我可以建議你發佈一段實際編譯的代碼嗎?然後,更容易檢查爲什麼代碼不像你認爲它應該表現的那樣行事。

由OP添加的示例代碼,這是我的機器上的行爲:

$> gcc -fopenmp omp.c 
$> ./a.out 
Thread 0 finished preprocessing 
Thread 0 processing 
Thread 3 processing 
Thread 2 processing 
Thread 1 processing 
Thread 0 processing 
Thread 3 processing 
Thread 2 processing 
Thread 1 processing 
Thread 0 processing 
Thread 3 processing 
$> gcc --version 
gcc (SUSE Linux) 4.7.2 20130108 [gcc-4_7-branch revision 195012] 
Copyright (C) 2012 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

$> icc -openmp omp.c 
$> ./a.out 
Thread 0 finished preprocessing 
Thread 0 processing 
Thread 3 processing 
Thread 2 processing 
Thread 1 processing 
Thread 0 processing 
Thread 3 processing 
Thread 2 processing 
Thread 1 processing 
Thread 0 processing 
Thread 3 processing 
mklemm-mobl2-vm:~/tmp [0]> icc -V 
Intel(R) C Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 14.0.1.106 Build 20131008 
Copyright (C) 1985-2013 Intel Corporation. All rights reserved. 

什麼編譯您使用的?

乾杯, -michael

+0

這不完全相同的問題。我添加了一個我正在使用的代碼的簡單版本,它是輸出。它應該編譯沒有問題。正如您所看到的,線程1正在執行每個任務,並且只有在計算完所有任務後才退出循環。 – fc67

+0

我使用的是gcc 4.8.2。我正在用這種方式編譯gcc -fopenmp -O2 test.c -o test.o. – fc67