我正在尝试初始化 2D concurrent_hash_map,这是英特尔 TBB 库中可用的容器。编译通过,运行时没有错误。但是,容器中并非所有初始化值都可用,从而导致错误行为。
哈希映射定义为
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());
任何建议或反馈都会很棒。
谢谢。