2017-12-19 13 views
0

構造体をパラメータとベクトルのintの数が多いCUDAカーネルに渡す必要があるという大きなコードがあります。私は構造体を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; 
} 
+0

'std :: vector'はデバイスコードでは使用できません。このコードを '.cpp'ファイルでコンパイルしていますか? CUDAデバイスコードは通常、 '.cu'ファイルに属します。 –

+0

はい、CPU上でOpenMPとMPIを使用する大規模なC++コードです。私は、構造体からベクトルを引き出し、それをポインタとして別々に渡さなければならないかもしれないと思っています。 – mll36

答えて

2

C++標準ライブラリの項目は、CUDAデバイスコードでは一般的に/通常は使用できません。ドキュメントのサポートはhereです。

この場合、std::vectorまたはstd::stringのいずれかで問題が発生する可能性があります。考えられる回避策の1つは、通常の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; 

これはもちろんコード内の変更が必要になります。

+0

...それらの配列を固定すること、すなわち 'cudaMallocHost()'を使って割り当てることを検討してください。 – einpoklum

関連する問題