2012-07-18 72 views
2

基本上我需要主線程繼續根據一些全局變量的值進行一些操作,這些全局變量可以由輔助線程編輯(在某些選定的時間間隔)。是這樣的:OpenMP將一個函數的執行分配給一個線程?

vector<int> mySharedVar; 

void secondaryThreadFunction() { 
    Do some operations 
    And update mySharedVar if necessarily 
} 

int main() { 
int count = 0; 
while(true) { 
    count++; 

    if (count%100) { //> Each 100 iterations run a parallel thraed 
     //> RUN secondaryThreadFunction in a separateThread 
    } 

    this is the main thread that based its operation on mySharedVar 
} 
} 

這是OpenMP的命令與secondaryThreadFunction();運行單個並行線程?

是否有比這任何其他更好的辦法:

#pragma omp parallel num_threads(2) 
    { 
     int i = omp_get_thread_num(); 

     if (i == 0){ 
      mainThread(); 
     } 
     if (i == 1 || omp_get_num_threads() != 2){ 
      secondaryThreadFunction(); 
     } 
    } 
+1

我想是OpenM P部分是少一點hacky。 – Mysticial 2012-07-18 09:47:35

+0

@Mysticial:嗨,大聲笑,我從你的答案採取該示例:D http://stackoverflow.com/questions/7876156/openmp-run-two-functions-in-parallel-each-by-half-of-thread-無論如何,你可以給我看一個樣本嗎? :D – dynamic 2012-07-18 09:49:53

+0

哈哈,這個問題似乎有一個很好的例子:http://stackoverflow.com/questions/2770911/how-does-the-sections-directive-in-openmp-distribute-work – Mysticial 2012-07-18 09:52:10

回答

1

這裏是我想出了:

#include <omp.h> 
#include <unistd.h> 
#include <vector> 
#include <iostream> 

std::vector<int> mySharedVar(10); 

void secondaryThreadFunction() { 
    mySharedVar[5]++; 
} 

int main() { 
    int i = 0 ; 

#pragma omp parallel sections shared(mySharedVar) private(i) 
    { 
#pragma omp section 
    //main thread 
    { 
     while(mySharedVar[5] < 10) { 
     std::cout << "main: "; 
     for(i=0; i < mySharedVar.size(); ++i){ 
      std::cout << mySharedVar[i] << " "; 
     } 
     std::cout << std::endl; 
     usleep(1.e5); // wait 0.1 seconds 
     } 
    } 
#pragma omp section 
    { 
     while(mySharedVar[5] < 10) { 
     secondaryThreadFunction(); 
     usleep(3.e5); // wait 0.3 seconds 
     } 
    } 
    } 
} 

編譯和運行g++ -fopenmp test_omp_01.cc && ./a.out

輸出:

main: 0 0 0 0 0 1 0 0 0 0 
main: 0 0 0 0 0 1 0 0 0 0 
main: 0 0 0 0 0 1 0 0 0 0 
main: 0 0 0 0 0 2 0 0 0 0 
main: 0 0 0 0 0 2 0 0 0 0 
main: 0 0 0 0 0 2 0 0 0 0 
main: 0 0 0 0 0 3 0 0 0 0 
main: 0 0 0 0 0 3 0 0 0 0 
main: 0 0 0 0 0 3 0 0 0 0 
main: 0 0 0 0 0 4 0 0 0 0 
main: 0 0 0 0 0 4 0 0 0 0 
main: 0 0 0 0 0 4 0 0 0 0 
main: 0 0 0 0 0 5 0 0 0 0 
main: 0 0 0 0 0 5 0 0 0 0 
main: 0 0 0 0 0 5 0 0 0 0 
main: 0 0 0 0 0 6 0 0 0 0 
main: 0 0 0 0 0 6 0 0 0 0 
main: 0 0 0 0 0 6 0 0 0 0 
main: 0 0 0 0 0 7 0 0 0 0 
main: 0 0 0 0 0 7 0 0 0 0 
main: 0 0 0 0 0 7 0 0 0 0 
main: 0 0 0 0 0 8 0 0 0 0 
main: 0 0 0 0 0 8 0 0 0 0 
main: 0 0 0 0 0 8 0 0 0 0 
main: 0 0 0 0 0 9 0 0 0 0 
main: 0 0 0 0 0 9 0 0 0 0 
main: 0 0 0 0 0 9 0 0 0 0 
+0

謝謝+1!是否需要將'shared(mySharedVar)'和'private(i)'? – dynamic 2012-07-18 22:20:09

+0

是的。編譯器如何知道你不希望兩個線程都增加'i'? – steffen 2012-07-19 05:01:01

+0

你可以把'int i = 0;'放在使用它的線程中,而不需要放置'private(i)'?關於共享(mySharedVar)是否需要? mySharedVar是一個全局變量,所以編譯器應該知道它是共享的 – dynamic 2012-07-19 10:16:24

相關問題