0
votes

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..

1
please detail your configuration: device generation and CUDA framework versionjopasserat

1 Answers

1
votes

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.