并行前缀和输出的问题

时间:2019-05-20 21:09:50

标签: c++ openmp

我正在做前缀和并行读取文件的回车,并使用openmp来并行化计算前缀的过程。我输入了一个1-10的序列,当我执行代码时,它返回的是一些奇怪的数字,而不是55

int main (int argc, char* argv[]){
     if (argc == 2) {
      uint id_thread = omp_get_thread_num();
      uint num_thread = omp_get_num_threads();
          std::vector<uint> values;
          get_values(argv[1], values);
          // Open the output file
          std::ofstream outfile("problem_output");
          // Start processing
          uint n = values.size();
      uint aux = 0;
      uint ans[num_thread+1];
      ans[0] = 0;

      #pragma omp parallel
      {
          uint ans[num_thread+1];
          ans[0] = 0;
          uint prefix = 0;

          #pragma omp parallel for schedule (static) 

          for (uint i = 0; i < n; ++i) {
              prefix += values[i];
              values[i] = prefix;
          }

          ans[num_thread+1] = prefix;

          #pragma omp barrier
          for (uint j = 0; j < (num_thread+1); ++j) {
              aux += ans[j];
          }

      #pragma omp parallel for schedule (static) 
          for (uint i = 0; i < n; ++i){

          values[i] += aux;

      }


          }

          for (uint i = 0; i < n; ++i){
              outfile << values[i] << std::endl;          

      }
          outfile.close(); 
      }
      else {
          std::cout << "Usage: ./prefix-sum problem_input \n";
          }
 }

输出:

134513745
134513750
134513765
134513800
134513870
134513996
134514206
134514536
134515031
134515746

0 个答案:

没有答案