sel*_*cuk 5 c c++ parallel-processing openmp
它是否适合包含函数调用的并行循环,或者是在内部进行基本操作的循环的更方便的并行化.
例如,它是否适合放置并行化指令如下?
main(){
..
#omp paralel ..
for (i=0;i<100;i++){
a[i] = foo(&datatype , ...);
...
}
..
}
int foo(datatype *a,...){
//doing complex operations here
//calling other functions etc.
}
Run Code Online (Sandbox Code Playgroud)
感谢Will Richard和Phkahler,评论很有帮助,我将深入了解rchrd建议的那本书.但是在一天结束之前,我希望能够创建一个现有的C代码(实际上是一个保持在程序顶部的大循环),如果可能的话,用openMP进行并行化.
在这一点上,我需要一些帮助,使至少部分循环并行化.为了简单起见,我不仅可以使整个循环内容变得简单,而且只能让它的一部分并行工作
for(i to N){
work1() --(serial)
work2() --(serial)
Work3() --( PARALLEL)
work4() --(serial)
}
//does it make sense adding critical sections except work3
#omp parallel for private(Ptr)
for(i to N){
#omp single
{
work1() --(serial)
work2() --(serial)
}
Work3(Ptr) --( PARALLEL)
#omp single
{
work4() --(serial)
}
}
Run Code Online (Sandbox Code Playgroud)
需要知道三个信息:
如果您的任务需要很长时间(几秒钟或更长时间)并且可以将其分解为独立的部分(有时通过重构,例如通过划分为作业并在组合之前收集每个作业的结果),那么值得尝试将其并行化。
轮廓!