OpenMP(至少2.0)支持某些简单 *** 作的缩减,但不支持max和min。
在下面的示例中,该
reduction子句用于求和,而一个
critical节用于使用线程局部变量无冲突地更新共享变量。
#include <iostream>#include <cmath>int main(){ double sum = 0; uint64_t ii; uint64_t maxii = 0; uint64_t maxii_shared = 0;#pragma omp parallel shared(maxii_shared) private(ii) firstprivate(maxii) {#pragma omp for reduction(+:sum) nowait for(ii=0; ii<10000000000; ++ii) { sum += std::pow((double)ii, 2.0); if(ii > maxii) maxii = ii; }#pragma omp critical { if(maxii > maxii_shared) maxii_shared = maxii; } } std::cerr << "Sum: " << sum << " (" << maxii_shared << ")" << std::endl;}
编辑:一个更清洁的实现:
#include <cmath>#include <limits>#include <vector>#include <iostream>#include <algorithm>#include <tr1/random>// sum the elements of vdouble sum(const std::vector<double>& v){ double sum = 0.0;#pragma omp parallel for reduction(+:sum) for(size_t ii=0; ii< v.size(); ++ii) { sum += v[ii]; } return sum;}// extract the minimum of vdouble min(const std::vector<double>& v){ double shared_min;#pragma omp parallel { double min = std::numeric_limits<double>::max();#pragma omp for nowait for(size_t ii=0; ii<v.size(); ++ii) { min = std::min(v[ii], min); }#pragma omp critical { shared_min = std::min(shared_min, min); } } return shared_min;}// generate a random vector and use sum and min functions.int main(){ using namespace std; using namespace std::tr1; std::tr1::mt19937 engine(time(0)); std::tr1::uniform_real<> unigen(-1000.0,1000.0); std::tr1::variate_generator<std::tr1::mt19937, std::tr1::uniform_real<> >gen(engine, unigen); std::vector<double> random(1000000); std::generate(random.begin(), random.end(), gen); cout << "Sum: " << sum(random) << " Mean:" << sum(random)/random.size() << " Min:" << min(random) << endl;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)