【问题标题】:Cuda::thrust: Performing compact operation with Device_vectorCuda::thrust:使用 Device_vector 执行紧凑操作
【发布时间】: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() 并且谁知道还有什么。这对其他人有什么帮助?为什么不提供简短而完整的代码?
  • 将来会这样做。

标签: c++ cuda


【解决方案1】:

您可能想阅读thrust quick start guide

这会给你带来麻烦:

thrust::host_vector<int> Host_results;

创建一个大小为零的向量。稍后当你这样做时:

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 函数的return value 是一个迭代器。您不能将其分配给向量。向量与迭代器不同。 Host_results 是一个向量。

不确定这是什么:

host_results=device_results;

您是否真的有一个变量或向量也以小写h 开头?因为host_resultsHost_results不一样

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

$ 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,
$

【讨论】:

  • 谢谢,您的代码和该指南都非常有帮助 - 而且我的东西​​正在工作:)。我的实际程序中的变量名充满了内容特定的单词,所以像 Host_results 与主机结果这样的东西对我来说只是非常清晰的编辑。没有设置 Host_results 端是一个错误。我想问一下你是否还在,因为 Host_results 是一个 Host_vector 而 Device_results 是一个设备向量,我想在主机上对我的向量执行几个操作,我不能使用 host_results=device_results;将我的结果放回主机上进行处理?
  • 您阅读过我建议的推力快速入门指南吗?因为我认为你的问题在那里得到了回答。 “如本例所示,= 运算符可用于将 host_vector 复制到 device_vector(反之亦然)。= 运算符还可用于将 host_vector 复制到 host_vector 或 device_vector 到 device_vector” 通常有多种皮肤方法编程时的猫。仅仅因为我已经展示或选择了一种方法并不意味着其他方法是不可能的。我在您使用 = 时指出的问题是所涉及的零长度向量。
  • 我确实在快速入门指南中看到了这一点。我只是希望与您确认最后的细节 - 因为我之前所做的概念性错误导致了这个相当尴尬的第二个问题。
猜你喜欢
  • 2012-02-21
  • 1970-01-01
  • 2011-08-21
  • 2015-09-09
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 2016-11-07
相关资源
最近更新 更多