title: All to All broadcasting implementation and MPI
body: <p>I am learning MPI and currently trying to implement a all to all broadcasting using send and receive operations. I know that number of processors is going to be the power of 2 and I wanted to implement an effective solution for this problem. I structured my thoughts like following.</p>
  <p><a href="https://i.stack.imgur.com/Xax5r.png" rel="nofollow noreferrer">all to all in a balanced binary tree with 8 processors</a></p>
  <p>First, the red copy and exchange operations are done. Then the green operations should be completed and finally, the purple operations would conclude the all to all message sharing.</p>
  <p>Assuming:</p>
  <ul>
  <li>Processor 0 has the message A</li>
  <li>Processor 1 has the message B</li>
  <li>Processor 2 has the message C</li>
  <li>Processor 3 has the message D</li>
  <li>Processor 4 has the message E</li>
  <li>Processor 5 has the message F</li>
  <li>Processor 6 has the message G</li>
  <li>Processor 7 has the message H</li>
  </ul>
  <p>Some of the steps:</p>
  <ol>
  <li>Processor 0 shares its message with Processor 7 and vice versa. So they both have messages A and H.</li>
  <li>Processor 1 shares its message with Processor 6 and vice versa. So they both have messages B and G.</li>
  <li>Processor 2 shares its message with Processor 5 and vice versa. So they both have messages C and F.</li>
  <li>Processor 3 shares its message with Processor 4 and vice versa. So they both have messages D and E.  <strong>(The red operations are done at this step.)</strong></li>
  <li>Processor 0 shares its message with Processor 3 and vice versa. So they both have messages A  H D and E.</li>
  <li>Processor 1 shares its message with Processor 2 and vice versa. So they both have messages B G C and F</li>
  </ol>
  <p>...</p>
  <p>I could not write a satisfying code for finding out the pair given the rank of the current processor. I thought finding out all the possible pairs recursively at the beginning like a look-up table to be used. But I would like to ask if there is a better way I could continue with? Also is the approach correct?</p>