【发布时间】:2015-06-26 04:25:38
【问题描述】:
我对 Cuda 还很陌生,虽然 stackoverflow 用户给了我一个关于如何使用推力::copy_if 在主机上压缩已知大小的数组的描述性示例(因为我的问题措辞很糟糕),但我'一直无法转换使用 device_vectors 的方法(处理设备上未知大小的输入数组)。
我正在尝试生成向量中与用户指定的谓词匹配的所有元素的位置的压缩列表。我得到的工作示例是:
#include <thrust/copy.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/functional.h>
#include <iostream>
using namespace thrust::placeholders;
int main()
{
const int N = 10;
int objectArray[N] = { 1, 11, 7, 2, 7, 23, 6, 6, 9, 11 };
int results[N]={0};
int* end = thrust::copy_if(thrust::make_counting_iterator(0), thrust::make_counting_iterator(N), objectArray, results, _1 == 7);
thrust::copy(results, results+N, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl << "result count = " << end-results << std::endl;
return 0;
}
我尝试修改代码以使用设备向量(并在设备上计算),如下所示:
#include <thrust/copy.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/functional.h>
#include <iostream>
using namespace thrust::placeholders;
int soughtElement=7;
reader.open("Numeric_1a40Coords.txt");
reader >> sizeOfProteinChain; //This returns the size of the input
reader.close();
thrust::host_vector<int> Host_names(sizeOfProteinChain);
thrust::host_vector<int> Host_results;
ImportNumericNameValues("Numeric_1a40Coords.txt", Host_names); //This populates the vector with "sizeOfProteinChain" number of elements
thrust::device_vector<int> Device_names = Host_Names;
thrust::device_vector<int> Device_results = Host_results;
Host_results = thrust::copy_if(thrust::make_counting_iterator(0), thrust::make_counting_iterator(sizeOfProteinChain), Device_names, Device_results, _1 == soughtElement);
host_results=device_results;
for (int i=0;i<sizeOfProteinChain;i++)
cout<< host_results[i]<<" ";
cout<<endl;
/*Not sure how to get the resulting number of compacted position elements with device vectors instead of pointer arrays*/
我收到错误说明:
类“thrust::device_vector>”没有成员 “迭代器类别”
和:
没有重载函数“thrust::copy_if”的实例与 参数列表
我在这方面已经坚持了一段时间,如果有任何关于如何纠正这些错误或更准确地转换上述示例的建议,我们将不胜感激。我之前关于这件事的问题can be found here:
【问题讨论】:
-
你得到了一个很好的代码,可以复制、编译和运行。在您的代码中,您选择删除
int main()并且谁知道还有什么。这对其他人有什么帮助?为什么不提供简短而完整的代码? -
将来会这样做。