我正在尝试执行并行版本STL remove_if。我所做的是在全局内存中创建一个计数器,并让每个线程在一个元素上工作。如果该元素不等于该键,那么它将被复制到结果数组中,其索引由计数器通过原子添加确定。有没有更好的选择来避免频繁的原子操作?并行移除阵列中的元素
我发现,推力库还具有的remove_if,但我觉得对位于源代码“推力\详细\后端\ CPP \ remove.h”目录很困惑:
template<typename ForwardIterator,
typename InputIterator,
typename Predicate>
ForwardIterator remove_if(ForwardIterator first,
ForwardIterator last,
InputIterator stencil,
Predicate pred)
{
// advance iterators until pred(*stencil) is true or we reach the end of input
while(first != last && !bool(pred(*stencil)))
{
++first;
++stencil;
}
if(first == last)
return first;
// result always trails first
ForwardIterator result = first;
++first;
++stencil;
while(first != last)
{
if(!bool(pred(*stencil)))
{
*result = *first;
++result;
}
++first;
++stencil;
}
return result;
}
这不是按顺序执行元素删除吗?
感谢您的任何建议!
我忘了提及您可以参与Thrust用户社区的活动:http://groups.google.com/group/thrust-users – ArchaeaSoftware
感谢您的建议。使用前缀和是一个好主意! –