如何使用&quot #pragma op paralallel for''正确地获取利润以填充阵列QVECTOR

发布于 2025-02-10 02:26:08 字数 646 浏览 5 评论 0原文

我正在学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2025-02-17 02:26:08

当然不需要关键部分,因为条件y_value&gt; yl || y_value&lt; yr可以并行完成,假设ylyr剩下的未修改,并且因为x and y可以用大小完成启动初始化,然后您可以在向量上执行直接访问,例如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 assuming yL are yR left unmodified and because x and y can be initialized with the size finish-start and you can then perform direct accesses on the vector like x[current-start] = (double)current/1000;. Also, please note that it is probably better to put current private using the clause private(current) in the parallel directive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文