Note that there are some explanatory texts on larger screens.

plurals
  1. POCuda single-thread scoped variables
    primarykey
    data
    text
    <p>Is it possible to make cuda use single-thread scoped variables (register or local memory) that are declared outside a function?</p> <p>Most of my device functions needs to use the same variables.</p> <p>Instead of passing the same variables as parameters to all my device funcitons, I would like to declare the variables outside the functions.</p> <p>Is that possible?</p> <p>My compute capacity is 1.2.</p> <p>EDIT: An example:</p> <pre><code>__device__ __local__ int id; __device__ __local__ int variable1 = 3; __device__ __local__ int variable2 = 5; __device__ __local__ int variable3 = 8; __device__ __local__ int variable4 = 8; // __device__ int deviceFunction3() { variable1 += 8; variable4 += 7; variable2 += 1; variable3 += id; return variable1 + variable2 + variable3; } __device__ int deviceFunction2() { variable3 += 8; variable1 += deviceFunction3(); variable4 += deviceFunction3(); return variable3 + variable4; } __device__ int deviceFunction1() { variable1 += id; variable4 += 2; variable2 += deviceFunction2(); variable3 += variable2 + variable4; return variable1 + variable2 + variable3 + variable4; } // Kernel __global__ void kernel(int *dev_a, int *dev_b, int *dev_c) { id = get_id(); dev_c[id] = deviceFunction1(); } </code></pre> <p>The 3 device functions needs to manipulate the same variables. Each variable is calculated dependently for each thread. To my understanding, I cannot use the above code, because I cannot declare the variables so that they are local to each thread.</p> <p>What I have to do instead is to declare all variables inside the kernel function, and then pass pointers to the variables to all the other functions:</p> <pre><code>__device__ int deviceFunction3(int* id,int* variable1,int* variable2,int* variable3,int* variable4) { *variable1 += 8; *variable4 += 7; *variable2 += 1; *variable3 += 2; return *variable1 + *variable2 + *variable3; } __device__ int deviceFunction2(int* id,int* variable1,int* variable2,int* variable3,int* variable4) { *variable3 += 8; *variable1 += deviceFunction3(id,variable1,variable2,variable3,variable4); *variable4 += deviceFunction3(id,variable1,variable2,variable3,variable4); return *variable3 + *variable4; } __device__ int deviceFunction1(int* id,int* variable1,int* variable2,int* variable3,int* variable4) { *variable1 += *id; *variable4 += 2; *variable2 += deviceFunction2(id,variable1,variable2,variable3,variable4); *variable3 += *variable2 + *variable4; return *variable1 + *variable2 + *variable3 + *variable4; } // Kernel __global__ void kernel(int *dev_a, int *dev_b, int *dev_c) { int id = get_id(); int variable1 = 3; int variable2 = 5; int variable3 = 8; int variable4 = 8; dev_c[id] = deviceFunction1(&amp;id,&amp;variable1,&amp;variable2,&amp;variable3,&amp;variable4); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload