小编Otá*_*ima的帖子

OpenMP 返回错误结果

下面的代码应该使用 4 个线程来计算 0 到 1000 之间所有数字的总和。它应该返回 499500,但在每次执行时返回不同的值。

#include <omp.h>
#include <iostream>

using namespace std;

int main (int argc, char *argv[])
{
        int nthreads, i, tid;
        float total;

        #pragma omp parallel num_threads(4) 
        {
            tid = omp_get_thread_num();
            if (tid == 0) {
                nthreads = omp_get_num_threads();
                cout << "Número de threads = " << nthreads <<endl;
            }
           #pragma omp barrier

           total = 0.0;
           #pragma omp for schedule(dynamic,10) private(i)
           for (i=0; i<1000; i++)
               total = total + i*1.0;
        } 
        cout << "Total = …
Run Code Online (Sandbox Code Playgroud)

c++ parallel-processing performance multithreading openmp

2
推荐指数
1
解决办法
108
查看次数