Cuda ::推力:使用Device_vector执行紧凑操作

时间:2015-06-25 19:59:08

标签: c++ cuda

我仍然是Cuda的新手,而stackoverflow用户给了我一个描述性的例子,说明如何使用thrust :: 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*/

我收到错误声明:

  

class“thrust :: device_vector&gt;”没有会员   “的iterator_category”

  

没有重载函数的实例“thrust :: copy_if”匹配   参数列表

我已经坚持了一段时间,对于如何纠正这些错误或更准确地转换上述示例的任何建议都将非常感激。我之前关于此事的问题can be found here:

1 个答案:

答案 0 :(得分:3)

您可能需要阅读thrust quick start guide

这会让你遇到麻烦:

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);

您已经创建了另一个零大小的矢量。虽然这些不会产生编译错误,但如果你试图在没有适当大小分配的情况下使用这些错误(例如通过复制内容),你将在运行时遇到麻烦。

这也是错误的:

thrust::copy_if

Host_results函数的return value迭代器。您无法将其指定给矢量。向量与迭代器不同。 host_results=device_results; 是一个向量。

不确定这是什么:

h

你真的有一个变量或向量,也是以小写host_results开头的吗?因为Host_results$ cat t808.cu #include <thrust/copy.h> #include <thrust/device_vector.h> #include <thrust/iterator/counting_iterator.h> #include <iostream> #define COPY_VAL 7 using namespace thrust::placeholders; int main(){ int objectArray[] = { 1, 11, 7, 2, 7, 23, 6, 6, 9, 11 }; int dsize = sizeof(objectArray)/sizeof(int); int results[dsize]; thrust::device_vector<int> d_obj(objectArray, objectArray+dsize); thrust::device_vector<int> d_res(dsize); int resultCount = thrust::copy_if(thrust::make_counting_iterator(0), thrust::make_counting_iterator(dsize), d_obj.begin(), d_res.begin(), (_1 == COPY_VAL)) - d_res.begin(); thrust::copy(d_res.begin(), d_res.end(), results); std::cout << "resultCount = " << resultCount << std::endl << "results: " << std::endl; thrust::copy(d_res.begin(), d_res.end(), std::ostream_iterator<int>(std::cout, ", ")); std::cout << std::endl; return 0; } $ nvcc -o t808 t808.cu $ ./t808 resultCount = 2 results: 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, $

不同

这是一个完整的工作示例,演示了如何在任意长度的设备向量上执行thrust :: copy_if:

{{1}}
相关问题