简体   繁体   English

Cuda :: thrust:使用Device_vector执行紧凑操作

[英]Cuda::thrust: Performing compact operation with Device_vector

I'm still fairly new to Cuda and while a stackoverflow user gave me a descriptive example on how to use thrust::copy_if to compact an array of known size on the host (as I worded my question badly), I've been unable to convert the approach to use device_vectors (to deal with inputted arrays of unknown size on the device). 我对Cuda还是很陌生,而一个stackoverflow用户给了我一个有关如何使用推力::: copy_if在主机上压缩一个已知大小的数组的描述性示例(正如我不好说的话),但我一直无法将方法转换为使用device_vectors(以处理设备上大小未知的输入数组)。

I'm attempting to generate a compacted list of the positions of all the elements in a vector which match a user specified predicate. 我正在尝试生成向量中与用户指定谓词匹配的所有元素的位置的压缩列表。 The working example I was given is: 我得到的工作示例是:

#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;
}

I've tried to modify the code to use device vectors (and compute on the device) as follows: 我试图修改代码以使用设备矢量(并在设备上进行计算),如下所示:

#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*/

I get errors stating that: 我收到错误说明:

class "thrust::device_vector>" has no member "iterator_category" 类“ thrust :: device_vector>”没有成员“ iterator_category”

and: 和:

no instance of overloaded function "thrust::copy_if" matches the argument list 没有重载函数“ thrust :: copy_if”的实例与参数列表匹配

I've been stuck on this for a while and any suggestions on how to correct those errors, or more accurately convert the above sample, would be greatly appreciated. 我已经坚持了一段时间,关于如何纠正这些错误,或更准确地转换上述示例的任何建议,将不胜感激。 My previous question on this matter can be found here: 我在此问题上的先前问题可以在这里找到:

You might want to read the thrust quick start guide . 您可能需要阅读推力快速入门指南

This will get you in trouble: 这会给您带来麻烦:

thrust::host_vector<int> Host_results;

that creates a vector of zero size. 创建零大小的向量。 Later when you do this: 稍后,当您执行此操作时:

thrust::device_vector<int> Device_results = Host_results;

You've created another vector of zero size. 您已经创建了另一个零大小的向量。 Although these will not create compile errors, if you try to use these (eg by copying something into them) without a proper size allocation, you're going to have trouble at run-time. 尽管这些不会产生编译错误,但是如果您尝试在没有适当大小分配的情况下使用这些错误(例如,通过将某些内容复制到它们中),则会在运行时遇到麻烦。

This is also wrong: 这也是错误的:

Host_results = thrust::copy_if(thrust::make_counting_iterator(0), thrust::make_counting_iterator(sizeOfProteinChain), Device_names, Device_results, _1 == soughtElement);

The return value of the thrust::copy_if function is an iterator . thrust::copy_if函数的返回值是一个迭代器 You cannot assign it to a vector. 您不能将其分配给向量。 A vector is not the same as an iterator. 向量与迭代器不同。 Host_results is a vector. Host_results是一个向量。

Not sure what this is: 不知道这是什么:

host_results=device_results;

Do you actually have a variable or vector somewhere that also begins with a lower case h ? 您实际上是否在某个地方也以小写h开头的变量或向量? Because host_results is not the same as Host_results 因为host_resultsHost_results

Here's a complete worked example demonstrating how to do thrust::copy_if on device vectors of arbitrary length: 这是一个完整的工作示例,演示如何在任意长度的设备矢量上执行推力:: 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,
$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM