Cuda 推力装置迭代器不工作
我不知道为什么下面的代码不是输出1,2而是一些随机数Cuda 推力装置迭代器不工作,cuda,gpu,thrust,Cuda,Gpu,Thrust,我不知道为什么下面的代码不是输出1,2而是一些随机数 #include <thrust/set_operations.h> #include <thrust/device_vector.h> #include <ostream> int main() { int a[]= { 1,2,3,4,5,6}; int b[] = {1,2,8}; int *ga, *gb,*gr; cudaMalloc((void**)&
#include <thrust/set_operations.h>
#include <thrust/device_vector.h>
#include <ostream>
int main() {
int a[]= { 1,2,3,4,5,6};
int b[] = {1,2,8};
int *ga, *gb,*gr;
cudaMalloc((void**)&ga, 6* sizeof(int));
cudaMalloc((void**)&gb, 3* sizeof(int));
cudaMalloc((void**)&gr, 3* sizeof(int));
cudaMemcpy(ga, a, 6 * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(gb, b, 3 * sizeof(int), cudaMemcpyHostToDevice);
thrust::device_ptr<int> end;
thrust::device_ptr<int> gaptr(ga);
thrust::device_ptr<int> gbptr(gb);
thrust::device_ptr<int> grptr(gr);
end = thrust::set_intersection(gaptr, gaptr+6, gbptr, gbptr+3,grptr);
printf("%d ", *grptr);
grptr++;
printf("%d ", *grptr);
getchar();
return 0;
}
此外,如何使用begin和end1对结果数组中的所有值进行迭代您正在尝试使用设备向量的迭代器对整数数组进行迭代。这是不可能的。指针就像数组的迭代器,可以使用运算符++前进指针,并使用*访问指针指向的值。您可以直接使用grptr,而不是尝试创建迭代器 就是这样:
std::cout << *grptr << " ";
grptr++;
std::cout << *grptr << std::endl;
在类推力::设备_引用的初始描述中,他们给出了一个printf示例,说明解决方案是强制转换。您可以在此处查看我尝试了您的解决方案,但仍然得到相同的输出。输出为85984256和85984260。从外观上看,这些看起来像是内存地址。我已经编辑了在原始Postal中运行的修改后的代码。由于某些原因,我需要使用printf
template<typename T> __host__ __device__ reference thrust::device_ptr<T>::operator*(void) const
printf("%d ", (int)*grptr);
grptr++;
printf("%d ", (int)*grptr);