推力设备迭代器不工作
我不知道为什么下面的代码不输出 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**)&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 迭代结果数组中的所有值
I do not know why the below code is not outputting 1,2 but some random numbers
#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;
}
Moreover , how to use begin and end1 to iterate over all the values in the result array
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试使用指向 device_vector 的迭代器来迭代整数数组。那是不可能的。指针就像数组的迭代器,您可以使用运算符 ++ 前进指针并使用 * 访问它指向的值。您可以直接使用 grptr,而不是尝试创建迭代器。
这有效:
其他注意事项,如果您包含 则不要使用 printf 。保持一致并使用 cout。
另外,如果你真的想尝试推力,你可以使用实际的推力向量而不是创建数组,手动复制它们并将它们包装在设备指针中(除非你试图学习推力和 cuda 运行时 api 之间的交互操作)。
编辑:
我尝试了您编辑的代码,确实 printf 不起作用,而 cout 起作用。问题是,thrust::device_ptr 是一个重载一元运算符 * 的类,其返回类型是 Throw::device_reference。
从文档中:
要让它正确打印所需的值,您只需将其强制转换为 int:
在类 Thrust::device_reference 的初始描述中,他们给出了一个示例,其中 printf 说明解决方案是强制转换。您可以在此处查看 http://wiki.thrust.googlecode.com/hg/ html/classthrust_1_1device__reference.html
You are trying to iterate over an array of ints with an iterator to a device_vector. That is not possible. A pointer is like an iterator for arrays in the sense that you can advance the pointer with operator ++ and access the value it points to with *. You can use the grptr directly instead of trying to create an iterator.
Just this works:
Other notes, don't use printf if you are including . Be consistent and use cout.
Also if you really want to try thrust you could be doing that using actual thrust vectors instead of creating arrays, copying them manually and wrapping them in device pointers (unless you are trying to learn inter operation between thrust and the cuda runtime api).
edit:
I tried your edited code and indeed printf does not work while cout works. The thing is, thrust::device_ptr is a class which has the unary operator* overloaded and its return type is a thrust::device_reference.
From the documentation:
To have it correctly print the desired value you can just cast it to int:
In the initial description of the class thrust::device_reference they give an example with printf stating the solution is a cast. You can check it here http://wiki.thrust.googlecode.com/hg/html/classthrust_1_1device__reference.html