【发布时间】:2014-10-30 22:56:59
【问题描述】:
在并行 for 循环中计算最大减少量时,在循环执行期间的中间时间,最大减少量变量的值是多少?它是仅针对特定线程的最大值还是所有线程的最大值?
我问的原因是我想使用循环内的当前最大值来执行计算,并且我希望它是所有线程的当前最大值,而不仅仅是正在执行循环的线程。
例如:
#include <stdio.h>
#include <omp.h>
int main(int argc, char *argv[]) {
double randomarray[10];
//initialize the random array
double outputarray[10];
double currentmax = 0;
#pragma omp parallel for reduction(max:currentmax)
for( i=0;i<10; i++) {
if(randomarray[i] > currentmax)
{
currentmax = randomarray[i];
}
output[i]=randomarray[i]/currentmax;
// is this current max for the currently
// executing thread or all threads?
}
}
【问题讨论】:
-
你当前的代码太简单了,内存带宽受限。
-
@Zboson 我有一个特定的问题,我正在尝试解决在计算全局最大值之前我需要知道当前最大值的地方;因此,您建议做两遍与我要解决的问题无关。如果您知道不受“内存带宽限制”的更好解决方案,请分享。
标签: c++ multithreading max openmp reduction