Thrust::device_reference 不能与 printf 一起使用吗?

发布于 2024-12-23 05:23:25 字数 723 浏览 5 评论 0原文

我正在使用推力分区函数将数组划分为偶数和奇数。但是,当我尝试显示设备向量时,它显示随机值。请让我知道错误在哪里。我认为我所做的一切都是正确的。

#include<stdio.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include<thrust/partition.h>

struct is_even
  {
      //const int toCom;
      //is_even(int val):toCom(val){}
    __device__
    bool operator()(const int &x)
    {
      return x%2;
    }
  };

void main(){


    thrust::host_vector<int> H(6);
    for(int i =0 ; i<H.size();i++){
        H[i] = i+1;
    }
    thrust::device_vector<int> D = H;
    thrust::partition(D.begin(),D.end(),is_even());
    for(int i =0 ;i< D.size();i++){
        printf("%d,",D[i]);
    }


    getchar();

}

I am using the thrust partition function to partition array into even and odd numbers. However, when i try to display the device vector, it shows random values. Please let me know where is the error. I think i have done everything correct.

#include<stdio.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include<thrust/partition.h>

struct is_even
  {
      //const int toCom;
      //is_even(int val):toCom(val){}
    __device__
    bool operator()(const int &x)
    {
      return x%2;
    }
  };

void main(){


    thrust::host_vector<int> H(6);
    for(int i =0 ; i<H.size();i++){
        H[i] = i+1;
    }
    thrust::device_vector<int> D = H;
    thrust::partition(D.begin(),D.end(),is_even());
    for(int i =0 ;i< D.size();i++){
        printf("%d,",D[i]);
    }


    getchar();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2024-12-30 05:23:25

您无法通过 printf 的省略号发送 thrust::device_reference(即 D[i] 的结果),因为它是不是 POD 类型。请参阅文档。您的代码将产生一个编译器警告。

首先转换为 int

for(int i = 0; i < D.size(); ++i)
{
  printf("%d,", (int) D[i]);
}

You can't send a thrust::device_reference (i.e., the result of D[i]) through printf's ellipsis because it is not a POD type. See the documentation. Your code will produce a compiler warning to this effect.

Cast to int first:

for(int i = 0; i < D.size(); ++i)
{
  printf("%d,", (int) D[i]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文