具有条件语句的OpenMP竞争条件

时间:2016-04-14 18:59:46

标签: c++ conditional openmp race-condition reduction

我试图并行化一段代码,我已经解决了并行调度地图中插入的问题。但该程序给我一个内存错误,我认为与地图大小的条件检查有关。存在概念上的错误,或者是否可以同步该部分?

if (PERF_ROWS == MAX_ROWS)
{
    int array_dist[PERF_ROWS];

    #pragma omp declare reduction (merge : std::multimap<float, int> : omp_out.insert(omp_in.begin(),omp_in.end()))

    #pragma omp parallel for schedule(dynamic) reduction(merge: ranking_map) private(array_dist)
    for (int i = 0; i < MAX_COLUMNS; i++)
    {
        if (i % PERF_CLMN == 1) continue;

        for (int j = 0; j < PERF_ROWS; j++)
        {
            array_dist[j] = abs(input[j] - input_matrix[j][i]);
        }

        float av = mean(PERF_ROWS, array_dist);

        float score = score_func(av);

        //cout<<score<<" "<<av<<endl;

        //#pragma omp critical(rank_func)
        //rank_function(score, i);

        multimap<float,int>::iterator it = ranking_map.begin();

        if (ranking_map.size() < NUM_RES)
        {
            ranking_map.insert({score, i});
        }

        else if (score > it -> first)
        {
            ranking_map.erase(it);
            ranking_map.insert({score, i});
        }
    }

1 个答案:

答案 0 :(得分:0)

Well, define your own combiner. Make a function called insertwhatever and write something like this:

    void insertwhatever(std::multimap<float, int>& a, std::multimap<float, int>&b)
     {
     for(auto iterb : b)
     {
      if(a.size() < NUM_RES)
      {
       a.insert(iterb);
      }
      else if(....)
      {
      (dont know what you want to do here)
      }
     }
    }

Then change the reduction in

#pragma omp declare reduction (merge : std::multimap<float, int> : insertwhatever(omp_out,omp_in))

I am not perfectly sure but i think this should work. Still i don't really understand what exeactly you are trying to do.