<font class="Apple-style-span" face="'courier new', monospace">Sorry, the last message scrambled the code, try again...<br><br></font><div><div><font class="Apple-style-span" face="'courier new', monospace">#include <sys/types.h></font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">#include <unistd.h></font></div><div><font class="Apple-style-span" face="'courier new', monospace">#include <iostream></font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">#include "mpi.h"</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">using namespace std;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">const int BUFSIZE = 1000;</font></div><div><font class="Apple-style-span" face="'courier new', monospace">const int NWORKER = 2;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">// -------------------------------------------------------------------------------------------------</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">static void mpi_send(int dst, const string& s, MPI_Comm comm)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">{</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> char buf[BUFSIZE];</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> strcpy(buf, s.c_str());</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> MPI_Send(buf, s.size(), MPI_CHAR, dst, 0, comm);</font></div><div><font class="Apple-style-span" face="'courier new', monospace">}</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">// -------------------------------------------------------------------------------------------------</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">static string mpi_recv(int src, MPI_Comm comm)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">{</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> char buf[BUFSIZE];</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> MPI_Status status;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> MPI_Recv(buf, BUFSIZE, MPI_CHAR, src, MPI_ANY_TAG, comm, &status);</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> int count;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> MPI_Get_count(&status, MPI_CHAR, &count);</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> buf[count] = 0;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> return buf;</font></div><div><font class="Apple-style-span" face="'courier new', monospace">}</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></div><div><font class="Apple-style-span" face="'courier new', monospace">// -------------------------------------------------------------------------------------------------</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></div><div><font class="Apple-style-span" face="'courier new', monospace">main(int argc, char **argv)</font></div><div><font class="Apple-style-span" face="'courier new', monospace">{</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace"> MPI_Init(&argc, &argv);</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> MPI_Comm parent;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> MPI_Comm_get_parent(&parent);</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div>
<font class="Apple-style-span" face="'courier new', monospace"> // Master</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> if (parent == MPI_COMM_NULL) {</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace"> MPI_Comm intercom = MPI_COMM_NULL;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> while (1) {</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> cout << "Enter: ";</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> string s;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> cin >> s;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> if (s == "start") {</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>if (intercom != MPI_COMM_NULL) {</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span> cout << "already started" << endl;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span> continue;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>cout << "spawn " << argv[0] << endl;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, NWORKER, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercom, MPI_ERRCODES_IGNORE);</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>continue;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> }</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> if (s == "stop") {</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>if (intercom == MPI_COMM_NULL) {</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span> cout << "worker not running" << endl;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span> continue;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>for (int w = 0; w < NWORKER; w++) {</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span> mpi_send(w, s, intercom);</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> <span class="Apple-tab-span" style="white-space:pre">        </span>MPI_Comm_disconnect(&intercom);</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>intercom = MPI_COMM_NULL;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>continue;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> }</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> if (s == "exit") {</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>if (intercom != MPI_COMM_NULL) {</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span> cout << "need to stop before exit" << endl;</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span> continue;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>break;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> }</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> if (intercom == MPI_COMM_NULL) {</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>cout << "need to start" << endl;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>continue;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> }</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> for (int w = 0; w < NWORKER; w++) {</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>mpi_send(w, s, intercom);</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>string t = mpi_recv(w, intercom);</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>cout << "worker " << w << " returned " << t << endl;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> }</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> }</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> }</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace"> // Worker</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> if (parent != MPI_COMM_NULL) {</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> while (1) {</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> string s = mpi_recv(0, parent);</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> if (s == "stop") {</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>MPI_Comm_disconnect(&parent);</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>break;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> }</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> mpi_send(0, s, parent);</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> }</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> }</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace"> MPI_Finalize();</font></div><div><font class="Apple-style-span" face="'courier new', monospace">}</font></div>
</div><div><br></div>