将包含向量的结构传递给CUDA内核

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

我有一个大型代码,我需要将结构传递给CUDA内核,该内核具有大量的参数和向量的整数。我无法弄清楚如何将结构传递给CUDA内核。我已将其复制到设备,但在尝试编译时出现以下错误:

test_gpu.cpp:63:17: error: invalid operands to binary expression ('void (*)(Test)' and 'dim3')
    computeTotal<<dimGrid, dimBlock>>(test_Device);
test_gpu.cpp:63:36: error: invalid operands to binary expression ('dim3' and 'Test *')
    computeTotal<<dimGrid, dimBlock>>(test_Device);

附件是代码的一个小的几乎工作的例子,任何想法?

#include <stdio.h>
#include <stdlib.h>
#include <cuda_runtime_api.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <device_functions.h>
#include <device_launch_parameters.h>
#include <vector>
#include <string>

typedef struct Test{
    int x;
    int y;
    int z;
    std::vector<int> vector;
    std::string string;
}Test;

Test test;

__device__ void addvector(Test test, int i){
    test.x += test.vector[i];
    test.y += test.vector[i+1];
    test.z += test.vector[i+2];
}

__global__ void computeTotal(Test test){
    for (int tID = threadIdx.x; tID < threadIdx.x; ++tID )
    addvector(test, tID);
}

int main()
{
    Test test_Host;
    int vector_size = 512;
    test_Host.x = test_Host.y = test_Host.z = 0;
    for (int i=0; i < vector_size; ++i)
    {
        test_Host.vector.push_back(rand());
    }

    Test* test_Device;
    int size = sizeof(test_Host);
    cudaMalloc((void**)&test_Device, size);
    cudaMemcpy(test_Device, &test_Host, size, cudaMemcpyHostToDevice);

    dim3 dimBlock(16);

    dim3 dimGrid(1);

    computeTotal<<dimGrid, dimBlock>>(test_Device);


    return 0;
}
c struct cuda
1个回答
2
投票

来自C ++标准库的项目通常不会/通常在CUDA设备代码中可用。对此的文档支持是here

对于这种特殊情况,这意味着您可能遇到std::vectorstd::string的问题。一种可能的解决方法是用普通的C风格数组替换它们:

#define MAX_VEC_SIZE 512
#define MAX_STR_SIZE 512

typedef struct Test{
    int x;
    int y;
    int z;
    int vec[MAX_VEC_SIZE];
    char str[MAX_STR_SIZE];
}Test;

这当然需要在代码中的其他地方进行更改。

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