Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As somebody already wrote, you can use <code>MPI_ANY_SOURCE</code> to receive from any source. To send two different kind of data in a single send you can use a <a href="http://www.mpi-forum.org/docs/mpi-11-html/node54.html" rel="nofollow">derived datatype</a>:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include "mpi.h" #define asize 10 typedef struct data_ { int id; float array[asize]; } data; int main() { MPI_Init(NULL,NULL); int rank = -1; int size = -1; MPI_Comm_rank(MPI_COMM_WORLD,&amp;rank); MPI_Comm_size(MPI_COMM_WORLD,&amp;size); data buffer; // Define and commit a new datatype int blocklength [2]; MPI_Aint displacement[2]; MPI_Datatype datatypes [2]; MPI_Datatype mpi_tdata; MPI_Aint startid,startarray; MPI_Get_address(&amp;(buffer.id),&amp;startid); MPI_Get_address(&amp;(buffer.array[0]),&amp;startarray); blocklength [0] = 1; blocklength [1] = asize; displacement[0] = 0; displacement[1] = startarray - startid; datatypes [0] = MPI_INT; datatypes [1] = MPI_FLOAT; MPI_Type_create_struct(2,blocklength,displacement,datatypes,&amp;mpi_tdata); MPI_Type_commit(&amp;mpi_tdata); if (rank == 0) { int count = 0; MPI_Status status; while (count &lt; size-1 ) { // Non-blocking receive printf("Receiving message %d\n",count); MPI_Recv(&amp;buffer,1,mpi_tdata,MPI_ANY_SOURCE,0,MPI_COMM_WORLD,&amp;status); printf("Message tag %d, first entry %g\n",buffer.id,buffer.array[0]); // Counting the received messages count++; } } else { // Initialize buffer to be sent buffer.id = rank; for (int ii = 0; ii &lt; size; ii++) { buffer.array[ii] = 10*rank + ii; } // Send buffer MPI_Send(&amp;buffer,1,mpi_tdata,0,0,MPI_COMM_WORLD); } MPI_Type_free(&amp;mpi_tdata); MPI_Finalize(); return 0; } </code></pre>
 

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