title: Sending java Objects over MPI
body: <h2>My story</h2>
  <p>I started writing an mpi-program in <a href="http://www.hpjava.org/mpiJava.html" rel="nofollow noreferrer">mpiJava</a> and managed to write a working program, but for one or another reason it doesn't want to run on the platform I need it to run on (I don't have any permissions to install anything). I'm not sure whether it was necessary (considering I get the same problems now), but I started using <a href="http://www.open-mpi.org/" rel="nofollow noreferrer">open-mpi</a> with its java-bindings. The point of that is that on the platform, open-mpi is available and I thus thought it would automatically work. It didn't work and I'm still trying to find out why (it might just have something to do with the credit-system), but that's not the problem I would like to see solved here.</p>
  <h2>My problem</h2>
  <p>The real problem is that I need to send java objects over MPI and I have no idea how to do it in the right way. In mpiJava I had the advantage, there was a MPI.Object datatype available (it just serialized the objects), but open-mpi doesn't have anything like that. I already visited a <a href="https://stackoverflow.com/questions/14844513/biginteger-library-compatible-with-mpi-message-passing-interface">post on BigInteger</a> and a <a href="https://stackoverflow.com/questions/23201522/how-can-i-pass-long-and-or-unsigned-integers-to-mpi-arguments">post actually providing some answers</a> and of course <a href="http://blogs.cisco.com/performance/java-bindings-for-open-mpi" rel="nofollow noreferrer">the open-mpi reference</a>, but I'm not completely sure now on what's the best approach in my cases.</p>
  <p>I have 3 different objects I would like to send over MPI:</p>
  <ol>
  <li>a classic <code>BigInteger</code></li>
  <li>a classic <code>Object[]</code> with 3 different objects in there</li>
  <li>a less classic self-written class containing a reference to another self-written class</li>
  </ol>
  <p>The self-written classes look like this:</p>
  <pre><code>public class AC implements Iterable&lt;BS&gt;, Comparable&lt;AC&gt;, LE, Serializable {
  
      private static final long serialVersionUID = -530910801257060853L;
      private static AC emptyAC = new AC();
      private static AC emptySetAC = new AC();
      static {emptySetAC.theAC.set(0);}
      
      private BitSet theAC = new BitSet();
      private BS universe = BS.universe();
      ...
      //methods
  }
  
  public class BS implements Iterable&lt;Integer&gt;, Comparable&lt;BS&gt;, Serializable {
  
      private static final long serialVersionUID = 4240724082500295998L;
      private static BS theEmptySet = new BS();
      private static long[] bits;
  
      private long theSet;
      ...
      //methods
  }
  </code></pre>
  <p>The open-mpi code thus far for sending (leaving out the 'trivial' <code>BigInteger</code>) of my list looks like:</p>
  <pre><code>public static void main(String[] args) {
      
      ...
      
      //master code
  
      SortedMap&lt;AC, Long&gt; functions = new TreeMap&lt;&gt;();
      AC u = AC.oneSetAC(BS.universe(n));
      SortedMap&lt;AC, BigInteger&gt; leftIntervalSize = new TreeMap&lt;&gt;();
      MPI.COMM_WORLD.bcast(new Object[]{functions, leftIntervalSize, u}, 3, MPI.Object, 0);
  
      ...
  
      AC[] sendbuf = new AC[1];
      while(iterator.hasnext()) {
          MPI.COMM_WORLD.send(sendbuf, 1, MPI.OBJECT, i, 0);
      }
  
      ...
  
      //worker code
      Object[] buf = new Object[3];
      MPI.COMM_WORLD.bcast(buf, 3, MPI.OBJECT, 0);
  
      ...
      
      AC[] func = new AC[1];
      Status stat = MPI.COMM_WORLD.recv(func, 1, MPI.OBJECT, 0, MPI.ANY_TAG);
      
      ...
  }
  </code></pre>
  <p>Note that <code>MPI.Object</code> is only an inheritance from my mpiJava implementation and those are the lines I need to change properly.</p>
  <h2>Proposed solutions</h2>
  <p>I already had some thoughts about it and I assume I could use the answer from <a href="https://stackoverflow.com/questions/23201522/how-can-i-pass-long-and-or-unsigned-integers-to-mpi-arguments">the post actually providing some answers</a> for the <code>BigInteger</code> if somebody could give me an example of how to use the MPI struct properly. (I really don't understand it completely from <a href="http://blogs.cisco.com/performance/java-bindings-for-open-mpi" rel="nofollow noreferrer">the open-mpi reference</a>. For my own classes and the <code>Object[]</code> on the other hand I could do either of the following:</p>
  <ol>
  <li>Just serialize the bloody objects.</li>
  <li>Use a MPI struct (after I got how to work with it).</li>
  <li>Gamble on the number of bytes needed and send them as bytes. (I would be surprised if this would work).</li>
  <li>Use some other functionality that I'm about to hear about.</li>
  </ol>
  <h2>My question(s)</h2>
  <ol>
  <li>Could anybody give me an example on how to use the MPI struct in java? (maybe applied on one of my examples?)</li>
  <li>Could anyone advise me the preferable strategy in any of the cases (and in case of serialization, point at the pitfalls)?</li>
  </ol>
  <p>I'm sorry for the long post, but I prefer to be clear from the first time.</p>
  <p>Thanks in advance</p>