在 CUDA 设备代码中使用 std::vector [英] Using std::vector in CUDA device code

查看:18
本文介绍了在 CUDA 设备代码中使用 std::vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:有没有办法在 Cuda 内核中使用向量"类?当我尝试时,我收到以下错误:

The question is that: is there a way to use the class "vector" in Cuda kernels? When I try I get the following error:

error : calling a host function("std::vector<int, std::allocator<int> > ::push_back") from a __device__/__global__ function not allowed

那么有办法在全局部分使用向量吗?我最近尝试了以下方法:

So there a way to use a vector in global section? I recently tried the following:

  1. 创建一个新的 Cuda 项目
  2. 转到项目的属性
  3. 打开 Cuda C/C++
  4. 转到设备
  5. 将代码生成"中的值更改为以下值:计算_20,sm_20

........之后我就可以在我的 Cuda 内核中使用 printf 标准库函数了.

........ after that I was able to use the printf standard library function in my Cuda kernel.

有没有办法以内核代码支持 printf 的方式使用标准库类 vector?这是在内核代码中使用 printf 的示例:

is there a way to use the standard library class vector in the way printf is supported in kernel code? This is an example of using printf in kernel code:

// this code only to count the 3s in an array using Cuda
//private_count is an array to hold every thread's result separately 

__global__ void countKernel(int *a, int length, int* private_count) 
{
    printf("%d
",threadIdx.x);  //it's print the thread id and it's working

    // vector<int> y;
    //y.push_back(0); is there a possibility to do this?

    unsigned int offset  = threadIdx.x * length;
    int i = offset;
    for( ; i < offset + length; i++)
    {
        if(a[i] == 3)
        {
            private_count[threadIdx.x]++;
            printf("%d ",a[i]);
        }
    }   
}

推荐答案

你不能在CUDA中使用STL,但是你可以使用推力库 做你想做的事.否则只需将vector的内容复制到设备上即可正常操作.

You can't use the STL in CUDA, but you may be able to use the Thrust library to do what you want. Otherwise just copy the contents of the vector to the device and operate on it normally.

这篇关于在 CUDA 设备代码中使用 std::vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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