![](/img/trans.png)
[英]Openmp multithreaded code giving different answer when using multiple threads
[英]Creating threads within a multithreaded for loop using openMP
我是OpenMP的新手,我無法在每個線程循環迭代中創建線程。 我的問題聽起來很天真,請忍受。
#pragma omp parallel private(a,b) shared(f)
{
#pragma omp for
for(...)
{
//some operations
// I want to parallelize the code in italics along within in the multi threaded for loop
*int x=func1(a,b);*
*int val1=validate(x);*
*int y=func2(a,b);*
*int val2=validate(y);*
}
}
在for循環中,所有線程都忙於循環迭代,因此沒有資源可以並行執行迭代中的內容。 如果工作平衡良好,您將不會獲得任何更好的性能。
如果很難/不可能通過並行的方式很好地平衡工作。 您可以嘗試在循環中生成任務,然后執行工作后記。 但是請注意任務生成的開銷。
#pragma omp parallel private(a,b) shared(f)
{
#pragma omp for nowait
for(...)
{
//some operations
#pragma omp task
{
int x=func1(a,b);
int val1=validate(x);
}
#pragma omp task
{
int y=func2(a,b);
int val2=validate(y);
}
}
// wait for all tasks to be finished (implicit at the end of the parallel region (here))
#pragma omp taskwait
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.