“警告:对于简单推力程序,通过省略号传递的非POD类类型]

问题描述 投票:1回答:1

尽管阅读了关于同一类型问题的许多答案,但我仍无法找到解决方案。我已经编写了以下代码来实现推力程序。程序执行简单的复制和显示操作。

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


int main(void)
{
// H has storage for 4 integers
  thrust::host_vector<int> H(4);
  H[0] = 14;
  H[1] = 20;
  H[2] = 38;
  H[3] = 46;

  // H.size() returns the size of vector H
  printf("\nSize of vector : %d",H.size());
  printf("\nVector Contents : ");
  for (int i = 0; i < H.size(); ++i) {
      printf("\t%d",H[i]);
  }

  thrust::device_vector<int> D = H;
  printf("\nDevice Vector Contents : ");
  for (int i = 0; i < D.size(); i++) {
      printf("%d",D[i]);         //This is where I get the warning.
  }

  return 0;
}
c++11 thrust
1个回答
2
投票

Thrust实现了某些操作,以方便在主机代码中使用device_vector的元素,但这显然不是其中之一。

有许多解决此问题的方法。以下代码演示了3种可能的方法:

  1. D[i]明确复制到主机变量,并且推力具有为此定义的适当方法。
  2. 打印输出之前将推力device_vector复制回host_vector
  3. 使用推力:: copy直接将device_vector的元素复制到流中。

代码:

#include <stdio.h>
#include <iostream>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/copy.h>


int main(void)
{
// H has storage for 4 integers
  thrust::host_vector<int> H(4);
  H[0] = 14;
  H[1] = 20;
  H[2] = 38;
  H[3] = 46;

  // H.size() returns the size of vector H
  printf("\nSize of vector : %d",H.size());
  printf("\nVector Contents : ");
  for (int i = 0; i < H.size(); ++i) {
      printf("\t%d",H[i]);
  }

  thrust::device_vector<int> D = H;
  printf("\nDevice Vector Contents : ");
//method 1
  for (int i = 0; i < D.size(); i++) {
      int q = D[i];
      printf("\t%d",q);
  }
  printf("\n");
//method 2
  thrust::host_vector<int> Hnew = D;
  for (int i = 0; i < Hnew.size(); i++) {
      printf("\t%d",Hnew[i]);
  }
  printf("\n");
//method 3
  thrust::copy(D.begin(), D.end(), std::ostream_iterator<int>(std::cout, ","));
  std::cout << std::endl;

  return 0;
}

注意,对于此类方法,推力正在生成各种设备->主机复制操作,以便于在主机代码中使用device_vector。这会影响性能,因此您可能要对大型矢量使用定义的复制操作。

© www.soinside.com 2019 - 2024. All rights reserved.