c++ - 将结构的动态数组传递给 GPU 内核

标签 c++ cuda structure dynamic-memory-allocation

我尝试将我的动态结构数组传递给内核,但它不起作用。我得到 - “段错误(核心已转储)”

我的代码 - 已编辑

#include <stdio.h>
#include <stdlib.h>

struct Test {
    unsigned char *array;
};

__global__ void kernel(Test *dev_test) {
}

int main(void) {

    int n = 4;
    int size = 5;
    unsigned char *array[size];
    Test *dev_test;

    //   allocate for host
    Test *test = (Test*)malloc(sizeof(Test)*n);
    for(int i = 0; i < n; i++)
    test[i].array =  (unsigned char*)malloc(size);


    //  fill data
    for(int i=0; i<n; i++) {
        unsigned char temp[] = { 'a', 'b', 'c', 'd' , 'e' };
        memcpy(test[i].array, temp, size);
    }

    //  allocate for gpu
    cudaMalloc((void**)&dev_test, n * sizeof(Test));
    for(int i=0; i < n; i++) {
        cudaMalloc((void**)&(array[i]), size * sizeof(unsigned char));
        cudaMemcpy(&(dev_test[i].array), &(array[i]), sizeof(unsigned char *), cudaMemcpyHostToDevice);
    }

    kernel<<<1, 1>>>(dev_test);

    return 0;
}

我应该如何正确分配 gpu 内存并将数据复制到该内存?

最佳答案

您需要为结构成员array 分配内存。

Test *test = malloc(sizeof(Test)*n);
for(int i = 0; i < n; i++)   
    test[i]->array =  malloc(size);  

我建议阅读 this answer处理此修复后的其他问题。

关于c++ - 将结构的动态数组传递给 GPU 内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30055147/

相关文章:

cuda - 如何自定义 nvidia-smi 的输出以显示 PID 用户名?

c - 参数类型错误

c++ - 逗号在数组和结构初始化中的意义是什么?

c++ - 将 SFINAE 条件移到最左侧以便于阅读

c# - Visual Studio 项目平台相关引用

返回 Template 类的函数中的 C++ 空尖括号

c - 嵌套结构中的指针

c++ - 在 Boost ASIO 服务器中处理生命周期

将字符数组从主机复制到设备后,CUDA: "Stack Overflow or Breakpoint Hit"和未指定的启动失败错误

cuda - cudaEventElapsedTime() 的精度是多少?