gpt4 book ai didi

c++ - TBB:初始化 concurrent_hash_map

转载 作者:行者123 更新时间:2023-11-28 07:40:51 26 4
gpt4 key购买 nike

我正在尝试初始化 2D concurrent_hash_map,它是英特尔 TBB 库中可用的容器。编译通过,运行时没有报错。但是,并非所有初始化值都在容器中可用,从而导致不正确的行为。

HashMap 定义为

template<typename K>
struct MyHashCompare {
static size_t hash(const K& key) { return boost::hash_value(key); }
static bool equal(const K& key1, const K& key2) { return (key1 == key2); }
};

typedef concurrent_hash_map<int, int, MyHashCompare<int> > ColMap;
typedef concurrent_hash_map<int, ColMap, MyHashCompare<int> > RowMap;

函数对象定义如下。不正确行为的原因可能源于此吗?

class ColumnInit {
RowMap *const pMyEdgeMap;
public:
void operator()(const blocked_range<size_t>& r) const {
RowMap* pEdgeMap = pMyEdgeMap;
RowMap::accessor accessX;
ColMap::accessor accessY;
for(size_t n1 = r.begin(); n1 != r.end(); n1++)
{
pEdgeMap->insert(accessX, n1);
for(int n2 = 1; n2 <= 64; n2++)
{
int diff = abs((int)n1 - n2);
if ((diff == 8) || (diff == 1))
{
assert((accessX->second).insert(accessY, n2));
accessY->second = -1;
}
else
{
assert((accessX->second).insert(accessY, n2));
accessY->second = 0;
}
}
}
}
ColumnInit(RowMap* pEdgeMap): pMyEdgeMap(pEdgeMap)
{
}
};

函数对象是通过调用 parallel_for 调用的,如下所示:

parallel_for(blocked_range<size_t>(1,64,16), ColumnInit((RowMap*)&mEdges), simple_partitioner());

任何建议或反馈都会很棒。

谢谢。

最佳答案

如果您打算创建一个 64x64 表,请使用 blocked_range(1,65,16) 作为 parallel_for 的第一个参数。原因是一个blocked_range代表一个半开区间,包括下限但不包括上限。

关于c++ - TBB:初始化 concurrent_hash_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15882716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com