我正在尝试编写一些简单的程序来学习 NVidia CUDA/Thrust。我是个菜鸟。我想要做的是使用带有自定义谓词的 find_if 。目前我的谓词只是在所有内容上都返回 true,所以我正在尝试获取所有输入。我想最终搜索字符串,在其中我用一些字符串 X 初始化函子,然后让 GPU 找到所有匹配的字符串。
我对这里的几点感到困惑。
我尝试填充一个充满指向我的字符串的指针的 device_vector,然后针对我的 MemCmp 谓词运行它。
首先,device_vector“知道”将我的字符串从主内存复制到GPU内存还是只是复制一个指针值?
其次,在“count = d_inputVector.end() - iter;”这一行 它返回 12 是我的迭代器中的项目数,它是 find_if 的结果。这不是错的吗?如果我尝试 iter - d_inputVector.begin() 返回零,这不会让我到任何地方。
最后,我获取小程序结果的方法是否正确?我是否要使用thrust::copy 将内存复制到host_vector 中,并且最后的循环是否足以查看结果?
非常感谢任何建议。谢谢,
米
struct MemCmp
{
__host__ __device__
bool operator()(char *data)
{
bool rv = false;
rv = true;
return rv;
}
};
....
// I initialize a device_vector and then copy pointers from main memory into the device_vector.
thrust::device_vector<char*> d_inputVector( itemCount );
for( int i=0; i<itemCount; i++ ){
d_inputVector[i] = inputData[i];
}
thrust::device_vector<char*>::iterator iter;
iter = thrust::find_if( d_inputVector.begin(), d_inputVector.end(), MemCmp() );
// this is the count that I think is wrong.
count = d_inputVector.end() - iter;
thrust::host_vector<char*> results( count );
thrust::copy( d_inputVector.begin(), iter, results.begin() );
for( thrust::host_vector<char *>::iterator it = results.begin(); it != results.end(); it++ ){
char* foo = *it;
}