0

好吧,标题很好地总结了它。我通过剖析来到了这个丑陋的地方。我通常使用 Open MP 2.0,但据我所知,使用 OMP2.0 并不容易。我正在使用 Concurrency::parallel_for ,但我似乎无法让它工作。

所以我有一个名为unallocated的 Obj 对象列表,如果它们满足某个标准,我想将其删除。由于我无法在循环本身中并行删除它们,因此我尝试将迭代器存储到另一个名为preallocated的序列中未分配的相应位置,稍后我将使用该序列调用 list::erase() 函数串行循环。

这是我到目前为止所拥有的:

// (std::list<Obj> unallocated is already created and full of Obj objects)

typedef std::list<Obj>::iterator ItUnallocated;

concurrency::concurrent_vector<ItUnallocated> preallocated;

// We can now do a parallelised search through all remaining elments of unallocated to 
//check if the cell pointer collides with any other unallocated cells, which will be added to
// the preallocated sequence.
concurrency::parallel_for
    (
    unallocated.begin(),
    unallocated.end(), 
    [&](ItUnallocated const it) 
    {
        if (Criterion(*it)) // check if the criterion is met
        {
            // criterion is met! Add the iterator
            preallocated.push_back(it);
        }
    }
);
// run through each element of *preallocated* in serial and call unallocated.erase() for each of the elements.

编译这个,我得到以下错误:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\ppl.h(3447): error C2440: '' : cannot convert from 'int' to 'std::_List_iterator<_Mylist>'

我在这里做错了什么(除了我创造了一个可憎的事实)。请富有成效的批评;我知道这很可怕,我们很乐意考虑更好的解决方案!

注意:我使用的是一个列表,因为它的向量在最后一部分太慢了(运行预分配并删除迭代器位置)。

4

1 回答 1

2

parallel_for只接受整数类型作为边界。您可以保留这个想法并在索引上使用vectoraparallel_for循环:

typedef std::vector<Obj>::iterator ItUnallocated;
std::vector<Obj> unallocated;
concurrency::concurrent_vector<ItUnallocated> preallocated;

concurrency::parallel_for<size_t>
    (
    0,
    unallocated.size(),
    [&](size_t index)
    {
        if(Criterion(unallocated[index]))
            preallocated.push_back(unallocated.begin() + index);
    }
);

另请注意,不是concurrency仅限 Microsoft。您可能想要使用非常相似的Intel thread Building Blocks library,它是可移植的。

于 2013-07-02T08:05:32.780 回答