gpt4 book ai didi

c++ - Cilk Plus 代码结果取决于 worker 数量

转载 作者:搜寻专家 更新时间:2023-10-31 02:15:16 24 4
gpt4 key购买 nike

我有一小段代码,我想在升级时将其并行化。我一直在使用 Cilk Plus 的 cilk_for 来运行多线程。问题是我会根据 worker 的数量得到不同的结果。

我读到这可能是由于竞争条件引起的,但我不确定代码的具体原因是什么或如何改善它。另外,我意识到 long__float128 对于这个问题来说有点矫枉过正,但在升级中可能是必要的。

代码:

#include <assert.h>
#include "cilk/cilk.h"
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

__float128 direct(const vector<double>& Rpct, const vector<unsigned>& values, double Rbase, double toWin) {
unsigned count = Rpct.size();
__float128 sumProb = 0.0;
__float128 rProb = 0.0;
long nCombo = static_cast<long>(pow(2, count));

// for (long j = 0; j < nCombo; ++j) { //over every combination
cilk_for (long j = 0; j < nCombo; ++j) { //over every combination
vector<unsigned> binary;

__float128 prob = 1.0;
unsigned point = Rbase;

for (unsigned i = 0; i < count; ++i) { //over all the individual events
long exp = static_cast<long>(pow(2, count-i-1));
bool odd = (j/exp) % 2;
if (odd) {
binary.push_back(1);
point += values[i];
prob *= static_cast<__float128>(Rpct[i]);
} else {
binary.push_back(0);
prob *= static_cast<__float128>(1.0 - Rpct[i]);
}
}

sumProb += prob;
if (point >= toWin) rProb += prob;
assert(sumProb >= rProb);
}

//print sumProb
cout << " sumProb = " << (double)sumProb << endl;
assert( fabs(1.0 - sumProb) < 0.01);

return rProb;
}

int main(int argc, char *argv[]) {
vector<double> Rpct;
vector<unsigned> value;

value.assign(20,1);
Rpct.assign(20,0.25);

unsigned Rbase = 22;
unsigned win = 30;

__float128 rProb = direct(Rpct, value, Rbase, win);

cout << (double)rProb << endl;

return 0;
}

export CILK_NWORKERS=1 && ./code.exe 的示例输出:

sumProb = 1

0.101812

export CILK_NWORKERS=4 && ./code.exe 的示例输出:

sumProb = 0.948159

Assertion failed: (fabs(1.0 - sumProb) < 0.01), function direct, file code.c, line 61.

Abort trap: 6

最佳答案

这是因为竞争条件。 cilk_for 是并行算法的实现。如果你想使用parallel for你必须使用independent iteration(独立数据)。这是非常重要的。您必须为您的案例使用 cilk reducer:https://www.cilkplus.org/tutorial-cilk-plus-reducers

关于c++ - Cilk Plus 代码结果取决于 worker 数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38759071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com