开发者

CUDA global memory

this is my code

 #include "stdafx.h"
  #include <iostream>
   using namespace std;

  #define n 10
  __device__ int glMem[n];

  开发者_开发技巧__global__ void initVals()
  {
for(int i=0;i<n;i++)
    glMem[i] = 0;
 }

 __global__ void test(int *out)
{
for(int i=0;i<n;i++)
    out[i] = 10;
}

int main()
{
const size_t sz = size_t(n)*sizeof(int);
initVals<<<1,1>>>();
int *devMem;
cudaMalloc((void **)&devMem, sz);
test<<<1, 1>>>(devMem);
int *hoMem=new int[n];
cudaMemcpy(hoMem, devMem,sz, cudaMemcpyDeviceToHost);

//print
for(int i=0;i<n;i++)
    cout<<hoMem[i]<<endl;
return 0;
}

IN this code I define

glMem

to size n. If I dont know the size earlier hw can I define?? for example I need to define like this.

__device__ int *glMem;

It doesnt work. Please give some code sample..


In that case you need to allocate the memory into the device.

// size of data
unsigned int size_of_glMem = n * sizeof(int);
// allocate device memory for result
int* glMem = NULL;
cudaMalloc( (void**) &glMem, size_of_glMem );

Hope this help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜