TBB: initialize concurrent_hash_map

TBB: initialize concurrent_hash_map

本文关键字:hash map concurrent initialize TBB      更新时间:2023-10-16

我正在尝试初始化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());

任何建议或反馈都将是伟大的。

谢谢。

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