c++openmpboost-geometry

OpenMP parallel instantiation and modification of vector of vectors


I'm using OpenMP to parallelize some union operation on Boost::geometry polygons (BPolygon). However I am stumbling over very strange exceptions that only occur from time to time (despite my RNGs being seeded with the same seed) so I wonder whether I'm ignoring some undefined behaviors with OpenMP parallelization and containers...

The code is part of a large and rather complex ensemble so I cannot really provide a MWE but the relevant parts are as follow:

std::unordered_map<size_t, std::vector<std::vector<BPolygon>>> container;

#pragma omp parallel
{
    std::vector<BPolygon> vec_geom;

    #pragma omp for
    for (size_t gid : gids)
    {
        [...]
        auto iter_block, iter_block_end;

        container[gid] = std::vector<std::vector<BPolygon>>();

        while (iter_block != iter_block_end)
        {
            auto iter_node = neurite_it->second->nodes_cbegin();
            auto iter_node_end = neurite_it->second->nodes_cend();

            // store the union in vec_geom
            vec_geom.clear();

            while (iter_node != iter_node_end)
            {
                cascading_union(node_it->second, vec_geom);
                iter_node++;
            }

            // add the union to the container
            container[gid].push_back(vec_geom);

            iter_block++;
        }
    }
}

with the cascading union function being

void cascading_union(TNodePtr n, std::vector<BPolygon> &vec_geom)
{
    auto range = n->segment_range();
    auto seg_it = range.begin();
    auto seg_end = range.end();

    std::vector<BPolygon> vec, vec_tmp;
    BPolygon poly1, poly2;

    // container associated to `range` cannot be modified so
    // we store the first unions in a new vector
    while (seg_it != seg_end)
    {
        poly1 = *((*seg_it).get());

        seg_it++;

        if (seg_it != seg_end)
        {
            poly2 = *((*seg_it).get());

            bg::union_(poly1, poly2, vec_tmp);

            vec.push_back(vec_tmp[0]);

            vec_tmp.clear();
        }
        else
        {
            vec.push_back(poly1);
        }
    }

    stype imax(vec.size()), imax2;

    // cascading union on final vector
    while (imax > 1)
    {
        imax2 = 0;

        for (stype i=0; i < imax; i += 2)
        {
            poly1 = vec[i];

            if (i + 1 < imax)
            {
                poly2 = vec[i + 1];

                bg::union_(poly1, poly2, vec_tmp);

                vec[imax2] = vec_tmp[0];

                vec_tmp.clear();
            }
            else
            {
                vec[imax2] = poly1;
            }

            imax2++;
        }

        vec.resize(imax2);
        imax = imax2;
    }

    if (imax > 0)
    {
        vec_geom.push_back(vec.back());
    }
}

The type of errors that are encounter are:

Please let me know if full backtraces would be useful.

I am wondering whether, though containers are supposed to be thread-safe, it could be an issue with the fact that here I'm dealing with variable size objects inside the container.

Note: these errors stem from a recent restructuring, so I think that there is no issue with the polygons or underlying structures because the errors were absent from previous implementations.


Solution

  • Thank to the comments, I realized that my idea about containers being thread-safe was wrong (well they are safe to read but not to modify).

    Assignments and call to push_back must be wrapped in

    #pragma omp critical