我正在寻找一种并行调用函数的方法。
例如,如果我有4个线程,我希望每个线程都以自己的线程id作为参数调用相同的函数。
由于该参数,没有线程可以处理相同的数据。
#pragma omp parallel
{
for(int p = 0; p < numberOfThreads; p++)
{
if(p == omp_get_thread_num())
parDF(p);
}
}
线程0应该运行parDF(0)
线程1应运行parDF(1)
线程2应该运行parDF(2)
线程3应该运行parDF(3)
这一切应该同时完成......
这(显然)不起作用,但是进行并行函数调用的正确方法是什么?
编辑:实际代码(这可能是太多的信息......但它被要求......)
从调用parDF()的函数:
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel
{
numberOfThreads = omp_get_num_threads();
//split nodeQueue
#pragma omp master
{
splitNodeQueue(numberOfThreads);
}
int tid = omp_get_thread_num();
//printf("Hello World from thread = %d\n", tid);
#pragma omp parallel for private(tid)
for(int i = 0; i < numberOfThreads; ++i)
{
parDF(tid, originalQueueSize, DFlevel);
}
}
parDF功能:
bool Tree::parDF(int id, int originalQueueSize, int DFlevel)
{
double possibilities[20];
double sequence[3];
double workingSequence[3];
int nodesToExpand = originalQueueSize/omp_get_num_threads();
int tenthsTicks = nodesToExpand/10;
int numPossibilities = 0;
int percentage = 0;
list<double>::iterator i;
list<TreeNode*>::iterator n;
cout << "My ID is: "<< omp_get_thread_num() << endl;
while(parNodeQueue[id].size() > 0 and parNodeQueue[id].back()->depth == DFlevel)
{
if(parNodeQueue[id].size()%tenthsTicks == 0)
{
cout << endl;
cout << percentage*10 << "% done..." << endl;
if(percentage == 10)
{
percentage = 0;
}
percentage++;
}
//countStartPoints++;
depthFirstQueue.push_back(parNodeQueue[id].back());
numPossibilities = 0;
for(i = parNodeQueue[id].back()->content.sortedPoints.begin(); i != parNodeQueue[id].back()->content.sortedPoints.end(); i++)
{
for(int j = 0; j < deltas; j++)
{
if(parNodeQueue[id].back()->content.doesPointExist((*i) + delta[j]))
{
for(int k = 0; k <= numPossibilities; k++)
{
if(fabs((*i) + delta[j] - possibilities[k]) < 0.01)
{
goto pointAlreadyAdded;
}
}
possibilities[numPossibilities] = ((*i) + delta[j]);
numPossibilities++;
pointAlreadyAdded:
continue;
}
}
}
// Out of the list of possible points. All combinations of 3 are added, building small subtrees in from the node.
// If a subtree succesfully breaks the lower bound, true is returned.
for(int i = 0; i < numPossibilities; i++)
{
for(int j = 0; j < numPossibilities; j++)
{
for(int k = 0; k < numPossibilities; k++)
{
if( k != j and j != i and i != k)
{
sequence[0] = possibilities[i];
sequence[1] = possibilities[j];
sequence[2] = possibilities[k];
//countSeq++;
if(addSequence(sequence, id))
{
//successes++;
workingSequence[0] = sequence[0];
workingSequence[1] = sequence[1];
workingSequence[2] = sequence[2];
parNodeQueue[id].back()->workingSequence[0] = sequence[0];
parNodeQueue[id].back()->workingSequence[1] = sequence[1];
parNodeQueue[id].back()->workingSequence[2] = sequence[2];
parNodeQueue[id].back()->live = false;
succesfulNodes.push_back(parNodeQueue[id].back());
goto nextNode;
}
else
{
destroySubtree(parNodeQueue[id].back());
}
}
}
}
}
nextNode:
parNodeQueue[id].pop_back();
}
答案 0 :(得分:4)
这就是你想要的吗?
<强> Live On Coliru 强>
#include <omp.h>
#include <cstdio>
int main()
{
int nthreads, tid;
#pragma omp parallel private(tid)
{
tid = ::omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0) {
nthreads = ::omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and terminate */
}
输出:
Hello World from thread = 0
Number of threads = 8
Hello World from thread = 4
Hello World from thread = 3
Hello World from thread = 5
Hello World from thread = 2
Hello World from thread = 1
Hello World from thread = 6
Hello World from thread = 7
答案 1 :(得分:3)
有两种方法可以达到你想要的效果:
正是你描述它的方式:每个线程都用它自己的线程id启动函数:
#pragma omp parallel
{
int threadId = omp_get_thread_num();
parDF(threadId);
}
并行块启动与系统报告它支持的线程数一样多的线程,并且每个线程都执行该块。由于它们在threadId中不同,因此它们将处理不同的数据。要强制启动更多线程,可以在编译指示中添加numthreads(100)
或其他内容。
执行所需操作的正确方法是使用并行块。
#pragma omp parallel for
for (int i=0; i < numThreads; ++i) {
parDF(i);
}
这样,循环的每次迭代(i的值)都被分配给执行它的线程。因为有许多迭代将并行运行,因为有可用的线程。
方法1.不是很通用,并且效率低下,因为你必须拥有你想要的函数调用一样多的线程。方法2.是解决问题的规范(正确)方法。
答案 2 :(得分:3)
你应该这样做:
#pragma omp parallel private(tid)
{
tid = omp_get_thread_num();
parDF(tid);
}
我认为它很直接。