Search code examples
c++multithreadingloopsopenmpmultimap

Maps and openMP parallel dispatching


I'm trying to parallelize a piece of code but there are some problems, the critical part for the insertion in a shared map create a bottleneck in my execution. There is a way to parallelize the insertion?

Second problem regards how to iterate with openMP on a maps a predefined number of times. Here is the code, I'm using g++-5 on Clion.

else if (PERF_ROWS <= MAX_ROWS && function_switch == false)
{
    int array_dist_perf[PERF_ROWS];
    int array_dist[MAX_ROWS];

    multimap<float, int> temp_map;

    #pragma omp parallel for schedule(dynamic) private(array_dist_perf) shared(temp_map)
    for (int i = 0; i < MAX_COLUMNS; i++)
    {
        if (i % PERF_CLMN == 1) continue;

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

        float av = mean(PERF_ROWS, array_dist_perf);

        float temp_score = score_func(av);

        #pragma omp critical(map_update)
        temp_map.insert({temp_score, i});
    }

    map<float,int>::reverse_iterator rit;

    int iter = 0;

    #pragma omp parallel for schedule(dynamic) private(array_dist) shared(iter)
    for (rit=temp_map.rbegin(); rit!=temp_map.rend(); rit++)
    {
        int s = rit->second;

        for (int k = 0; k < MAX_ROWS; k++)
        {
            array_dist[k] = abs(input[k] - input_matrix[k][s]);
        }

        float av_real = mean(MAX_ROWS, array_dist);

        float score_real = score_func(av_real);

        rank_function(score_real, s);

        #pragma omp atomic
        iter++;

        if (iter == THRESHOLD_NUM)

            break;
    }
}

Solution

  • In Open MP 4 (which should be supperted by g++5) it is possible to define your own reduction. I dont want to make some unneccesarry duplication, so please look at this answer: C++ OpenMP Parallel For Loop - Alternatives to std::vector

    If i understand your problem correct, you can use order and schedule(static, chuncksize) where chunksize is the number of iterations a thread gets.