簡體   English   中英

向量的向量的 OpenMP 並行實例化和修改

[英]OpenMP parallel instantiation and modification of vector of vectors

我正在使用 OpenMP 來並行化 Boost::geometry 多邊形( BPolygon )上的一些聯合操作。 但是,我偶然發現了僅不時發生的非常奇怪的異常(盡管我的 RNG 被播種了相同的種子)所以我想知道我是否忽略了 OpenMP 並行化和容器的一些未定義行為......

該代碼是一個大型且相當復雜的集合的一部分,因此我無法真正提供 MWE,但相關部分如下:

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

級聯聯合 function 是

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

遇到的錯誤類型有:

  • 在代碼的第一部分:
    • 分配container[gid] = std::vector<std::vector<BPolygon>>();
    • munmap_chunk(): invalid pointer (精確位置未知)
  • cascading_union
  • bg::union_中的段錯誤
  • free(): invalid next size (fast) (精確位置未知)
  • malloc(): unaligned tcache chunk detected (精確位置未知)

請讓我知道完整的回溯是否有用。

我想知道,盡管容器應該是線程安全的,但這可能是一個問題,因為我在這里處理的是容器內的可變大小對象。

注意:這些錯誤源於最近的重組,所以我認為多邊形或底層結構沒有問題,因為以前的實現中沒有這些錯誤。

感謝評論,我意識到我關於容器是線程安全的想法是錯誤的(它們可以安全閱讀但不能修改)。

分配和對push_back的調用必須包含在

#pragma omp critical

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM