[英]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.