defining a variable on the device

hi everyone, i just started learning cuda…
i want to define a variable on the device memory so that i can use in my kernal

so my host function contains … the following line

int main(int argc, char* argv)
{
////////////
device int correctBool = 1 ;
/////////////////////
}

i got the following error while compilng…

                [b]error[/b]: a function scope variable cannot be declared with "__device__" inside a host function
               [b] error[/b] : a "__device__" variable declaration is not allowed inside a function body

can anybody help me out with this…
it may be the most trivial thing for u guys…
but i’m stuck…
thnx in advance…

You cannot directly use device identifier from host function. Make it as

int main(int argc, char* argv)
{
////////////
int* correctBool;
/////////////////////

  cudamalloc((void**)&correctBool, sizeof(int));

}

And then use cudamemcpy() to copy the value to device memory.

thanks a lot. it worked.

but even for static data do we need to use cudaMalloc()

or is there anyway around

For values which won’t change, declare them constant at file scope. For variables that could change, you can declare global memory symbols at file scope and read their addresses and update their values via the symbol runtime api functions. Both are discussed in the programming guide,

Yes there is:

__device__ int correctBool;

int main(int argc, char* argv[])

{ 

	correctBool = 1;

}

EDIT: Of course this does not work. I’ve just answered too quickly without really looking at the problem.

You sure that will work?

I think that won’t work.

device int correctBool;

int main(int argc, char* argv)
{
correctBool = 12; // This assignment should not work for a variable allocated in device memory, but now it is working.

cudaMemset(&correctBool, 0, sizeof(int));     //Memset will fail
if (cudaSuccess != cudaGetLastError())
{
    printf("Memset Failed\n");
}

}

Which means ‘correctBool’ is not getting allocated in device memory using this method.

Err… no. :whistling:

Apparently I should take a bit more time when answering these questions. :confused: