CUDA 推力库:如何创建整数的 host_vectors 的 host_vector? [英] CUDA Thrust library: How can I create a host_vector of host_vectors of integers?

查看:40
本文介绍了CUDA 推力库:如何创建整数的 host_vectors 的 host_vector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C++ 中,为了创建一个包含 10 个整数向量的向量,我将执行以下操作:

In C++ in order to create a vector that has 10 vectors of integers I would do the following:

  std::vector< std::vector<int> > test(10);

因为我认为 Thrust 使用与 STL 相同的逻辑,所以我尝试这样做:

Since I thought Thrust was using the same logic with the STL I tried doing the same:

  thrust::host_vector< thrust::host_vector<int> > test(10);

但是我遇到了太多令人困惑的错误.我试着做:

However I got too many confusing errors. I tried doing:

  thrust::host_vector< thrust::host_vector<int> > test;

并且它起作用了,但是我无法向该向量添加任何内容.做

and it worked, however I can't add anything to this vector. Doing

  thrust::host_vector<int> temp(3);
  test.push_back(temp);

会给我同样的错误(太多了,无法粘贴在这里).

would give me the same errors(too many to paste them here).

一般来说,在使用 Thrust 时,使用 host_vector 和 STL 的 vector 有区别吗?

Also generally speaking when using Thrust, does it make a difference between using host_vector and the STL's vector?

提前致谢

推荐答案

Thrust 的容器仅设计用于 POD(纯旧数据)类型.无法通过在推力中实例化向量的向量"来创建多维向量,主要是由于GPU端的限制,无法在设备代码路径中传递和使用.

Thrust's containers are only designed for POD (plain old data) types. It isn't possible to create multidimensional vectors by instantiating "vectors of vectors" in thrust, mostly because of the limitations on the GPU side which make it impossible to pass and use in the device code path.

C++ 标准库类型和算法与那些 STL 派生模型的推力主机实现之间存在一定程度的兼容性,但是当您想要同时使用主机和设备库后端时,您确实应该坚持使用主机向量.

There is some level of compatibility between C++ standard library types and algorithms and the thrust host implementation of those STL derived models, but you should really stick with host vectors when you want to work both with the host and device library back ends.

这篇关于CUDA 推力库:如何创建整数的 host_vectors 的 host_vector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆