简体   繁体   English

带条件语句的OpenMP竞争条件

[英]OpenMP race conditions with conditional statement

I'm trying to parallelize a piece of code, I've solved the problem of dispatching in parallel the insertions in a map with a reduction. 我正在尝试并行处理一段代码,我已经解决了通过归约并行分配地图中插入的问题。 But the program gives me a memory error I think related to the conditional check on the map size. 但是该程序给了我一个内存错误,我认为这与对地图大小的条件检查有关。 There is a conceptual error or is it possible to synchronize also that part? 存在概念错误,或者是否也可以同步该部分?

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});
        }
    }

Well, define your own combiner. 好吧,定义自己的组合器。 Make a function called insertwhatever and write something like this: 创建一个名为insertwhatever的函数,并编写如下代码:

    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. 我还是不太了解您到底想做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM