Pure Mpi.NET

In this example, we illustrate the use of the Gather, Scatter, and AlltoAll methods on the Comm object.  Gather is used to send objects from all hosts participating in a communication handle to one host.  The root host will receive the objects into array ordered by the hosts' ranks.  Scatter will do the opposite, sending each object in an array to the corresponding host from the root host.

static void MpiProcess(IDictionary<string, Comm> comms)

{

    Comm comm = comms["MPI_COMM_WORLD"];

 

    Console.WriteLine("Cur Rank: {0}", comm.Rank);

 

    int[] gathered = comm.Gather<int>(0, comm.Rank * 5 + 1);

 

    if(gathered != null)

    {

        Console.WriteLine("Gathered at: {0}", comm.Rank);

 

        for(int i = 0; i < gathered.Length; i++)

        {

            Console.WriteLine("Gathered Value: {0}", gathered[i]);

        }

    }

 

    int scattered = comm.Scatter<int>(0, gathered);

    Console.WriteLine("Scattered: {0} to Rank: {1}",

                       scattered, comm.Rank);

 

    int[] sendData = new int[comm.Size];

    for (int i = 0; i < comm.Size; i++ )

    {

        sendData[i] = (comm.Rank * comm.Rank + 1) * (i + 1);

    }

    int[] receiveData = comm.AllToAll<int>(sendData);

    string receiveDataStr = "";

    for (int i = 0; i < comm.Size; i++)

    {

        receiveDataStr += receiveData[i] + " ";

    }

    Console.WriteLine("AlltoAll ReceivedData for {0}: {1}",

                      comm.Rank, receiveDataStr);

}