我有一个结构,并希望将结构对象创建为常量。但是我无法使用cudaMemcpyToSymbol分配值。如何在cuda中为常量结构赋值?
#include <iostream>
using namespace std;
#define N 10
struct CDistance
{
int Magnitude;
int Direction;
};
__constant__ CDistance constBuf;
__global__ void foo( int *results )
{
int tdx = threadIdx.x;
int idx = blockIdx.x * blockDim.x + tdx;
results[idx] = constBuf.Magnitude;
}
// main routine that executes on the host
int main(int argc, char* argv[])
{
int arr[2] = { 16, 2};
int *results_h = new int[N];
int *results_d;
cudaMalloc((void **)&results_d, N*sizeof(int));
cudaMemcpyToSymbol(constBuf.Magnitude, arr[0], N*sizeof(int), 0, cudaMemcpyHostToDevice);
foo <<< 1, 10 >>> ( results_d );
//cudaMemcpy(results_h, results_d, N*sizeof(int), cudaMemcpyDeviceToHost);
for( int i=0; i < N; ++i )
printf("%i ", results_h[i] );
delete(results_h);
}