2012-07-18 75 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

相关问题