Cuda/Thrust:remove_if不会改变device_vector.size()?



我有一个相当简单的cuda问题,似乎应该是一个直接的操作:根据第二个布尔数组的值从1个数组中删除元素。我采取的步骤是:

  1. 创建一个与处理后的输入数组相同的bool的device_vector。
  2. 调用内核,该内核将设置一些元素从(1)到true
  3. call remove_if在输入数组上使用谓词使用(2)的处理阵列。
  4. 对于设置为true的布尔数组中的每个值,请从输入数组中删除相应的元素。

我看到的是输入阵列没有更改,我不确定为什么?

struct EntryWasDeleted
{
    __device__ __host__
    bool operator()(const bool ifDeleted)
    { return true; }
};
    //This array has about 200-300 elements
    //thrust::device_vector<SomeStruct> & arrayToDelete
    thrust::device_vector<bool>* deletedEntries = 
        new thrust::device_vector<bool>(arrayToDelete.size(), false);
    cuDeleteTestEntries<<<grid, block>>>( thrust::raw_pointer_cast(arrayToDelete.data()), countToDelete, heapAccess, thrust::raw_pointer_cast(deletedEntries->data()));
    cudaDeviceSynchronize();
    thrust::remove_if(arrayToDelete.begin(), arrayToDelete.end(), deletedEntries->begin(), EntryWasDeleted());     
    //I am expecting testEntries to have 0 elements
    thrust::host_vector<SomeStruct> testEntries = arrayToDelete;
    for( int i = 0; i<testEntries.size(); i++)
    { printf( "%d", testEntries[i].someValue); }

在此样本中,我总是在谓词中返回进行测试。但是,当我这样做时:testEntries =删除并输出成员。我可以验证已删除的特定词被适当地填充三重和伪造。

我的期望是TestEntries具有0个元素。但这不是,我会收到一个输出,好像emove_if无需做任何事情。IE:输出显示输入数组中的所有元素。我不确定为什么?是否有一种特定方法可以从device_vector中删除元素?

,因此您需要捕获从remove_if

返回的迭代器
thrust::device_vector<SomeStruct>::iterator endIterator = 
   thrust::remove_if(arrayToDelete.begin(), arrayToDelete.end(), 
deletedEntries->begin(), EntryWasDeleted());    

然后,当您将数据复制回主机时,而不是在主机和设备之间使用推力默认分配运算符:

thrust::host_vector<SomeStruct> testEntries(arrayToDelete.begin(),endIterator);

作为使用原始阵列的旁注通常更有效。就像您可以将结构的索引存储在数组中并在这些索引上操作吗?

最新更新