如何使用&quot #pragma op paralallel for''正确地获取利润以填充阵列QVECTOR
我正在学习C ++,并尝试通过图表制作自己的计算器。在这一部分中,当我尝试用坐标填充数组时。我想使用多线程使其更快。但是当我这样做时,代码开始工作得更糟,速度较慢。我该如何解决问题,我在做什么错? 它根本不起作用
omp_set_num_threads(2);
#pragma omp parallel for
for (current = start; current < finish; current ++) {
Calc b(a.get_string());
double y_value = b.parsing((double)current/1000);
#pragma omp critical
{
if (y_value > yL || y_value < yR) {
x->push_back((double)current/1000);
y->push_back(y_value);
}
}
顺便说一句,如果没有“ #pragma op critical” x和y,这是QVECTOR, 。 请尝试尽可能简单地表达自己,因为我只是在学习。非常感谢!
I'm learning c++ and trying to make my own calculator with charting. In this part, when I try to fill an array with coordinates. I want to make it faster using multithreading. But when I did so, the code began to work much worse, slower. How can I solve the problem what am I doing wrong? By the way, it doesn't work at all without "#pragma omp critical"
omp_set_num_threads(2);
#pragma omp parallel for
for (current = start; current < finish; current ++) {
Calc b(a.get_string());
double y_value = b.parsing((double)current/1000);
#pragma omp critical
{
if (y_value > yL || y_value < yR) {
x->push_back((double)current/1000);
y->push_back(y_value);
}
}
x and y this is QVector.
Please try to express yourself as simply as possible because I'm just learning. Thank you all very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然不需要关键部分,因为条件
y_value&gt; yl || y_value&lt; yr
可以并行完成,假设yl
是yr
剩下的未修改,并且因为x
andy
可以用大小完成启动
初始化,然后您可以在向量上执行直接访问,例如x [current-start] =(double)Current/1000;
。另外,请注意,使用“ private> private(current)”在并行指令中使用当前
私有。The critical section is certainly not needed because the condition
y_value > yL || y_value < yR
can be done in parallel assumingyL
areyR
left unmodified and becausex
andy
can be initialized with the sizefinish-start
and you can then perform direct accesses on the vector likex[current-start] = (double)current/1000;
. Also, please note that it is probably better to putcurrent
private using the clauseprivate(current)
in the parallel directive.