title: Warp shuffling for CUDA
body: <p>I need to make a warp shuffling that look like this:
  <a href="https://i.stack.imgur.com/DPhqR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DPhqR.jpg" alt="warp shuffling"></a></p>
  
  <p>On this picture, the number of threads is limited to <code>8</code> to make it readable.
  If I read the Nvidia SDK and ptx manual, the shuffle instruction should do the job, specially the <code>shfl.idx.b32 d[|p], a, b, c;</code> ptx instruction.</p>
  
  <p>From the <a href="http://docs.nvidia.com/cuda/parallel-thread-execution/index.html" rel="nofollow noreferrer">manual</a> I read:</p>
  
  <pre><code>Each thread in the currently executing warp will compute a source lane
  index j based on input operands b and c and the mode. If the computed
  source lane index j is in range, the thread will copy the input operand
  a from lane j into its own destination register d;
  </code></pre>
  
  <p>So, providing proper values of <code>b</code> and <code>c</code>, I should be able to do it by writing a function like this (inspired from CUDA SDK <code>__shufl</code> primitive implementation).</p>
  
  <pre><code>  __forceinline__ __device __ float shuffle(float var){
     float ret;
     int srcLane = ???
     int c = ???
     asm volatile ("shfl.idx.b32 %0, %1, %2, %3;" : "=f"(ret) : "f"(var), "r"(srcLane), "r"(c));
    return ret;
  </code></pre>
  
  <p>}</p>
  
  <p>If it is possible, what is the constant for <code>srcLane</code> and <code>c</code>? I am not able to determine them (I am using CUDA 8.0) .</p>
  
  <p>Best,</p>
  
  <p>Timocafe</p>