Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your usage case is a truly awful idea, and I wouldn't recommend that design pattern to my worst enemy. Leaving aside the merits of the code for a moment, as I hinted in comments, you can achieve the thread local variable scoping you desire by encapsulating the __device__ functions and variables they rely on in a structure, like this:</p> <pre><code>struct folly { int id; int variable1; int variable2; int variable3; int variable4; __device__ folly(int _id) { id = _id; variable1 = 3; variable2 = 5; variable3 = 8; 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; } }; __global__ void kernel(int *dev_a, int *dev_b, int *dev_c) { int id = threadIdx.x + blockIdx.x * blockDim.x; folly do_calc(id); dev_c[id] = do_calc.deviceFunction1(); } </code></pre> <p>Also note that CUDA supports C++ style pass by reference, so any one of the device functions you have written in the second piece of code you posted could easily be written like this:</p> <pre><code>__device__ int deviceFunction3(int &amp; variable1, int &amp; variable2, int &amp; variable3, int &amp; variable4) { variable1 += 8; variable4 += 7; variable2 += 1; variable3 += 2; return variable1 + variable2 + variable3; } </code></pre> <p>which is far cleaner and easier to read. </p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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