如何在cuda中设置常量结构中的值

时间:2014-06-12 04:18:57

标签: cuda

我有一个结构,并希望将结构对象创建为常量。但是我无法使用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);
}

0 个答案:

没有答案