C#MPI第2課MPI送信構造体

2276 ワード

using System;
using MPI;

namespace MPIHello
{
    /// 
    ///  
    /// 
    class TestSendStructProgram : IProgram
    {
        /// 
        ///  
        /// 
        struct TestStruct
        {
            #region Overrides of ValueType

            public override string ToString()
            {
                return string.Format("v1:{0},v2:{1},v3:{2},v4:{3}",V1,V2,V3,V4);
            }

            #endregion
            /// 
            ///  
            /// 
            public void Increase()
            {
                V1++; V2++; V3++; V4++;
            }

            public int V1;
            public int V2;
            public int V3;
            public int V4;
        }

        #region Implementation of IProgram
        
        /// 
        ///  
        /// 
        ///  
        public void Entrance(string[] args)
        {
            // MPI 
            using (new MPI.Environment(ref args))
            {
                // Communicator
                var comm = Communicator.world;

                if (0 == comm.Rank)
                {
                    var obj = new TestStruct();
                    // 0 
                    comm.Send(obj, 1, 0);

                    // receive the final message 
                    var msg = comm.Receive(Communicator.anySource, 0);
                    msg.Increase();
                    // 
                    Console.WriteLine("Rank " + comm.Rank + " received message \"" + msg.ToString() + "\".");
                }
                else
                {
                    // n 
                    var msg = comm.Receive(comm.Rank - 1, 0);
                    // 
                    msg.Increase();
                    // 
                    Console.WriteLine("Rank " + comm.Rank + " received message \"" + msg + "\".");

                    // 
                    comm.Send(msg, (comm.Rank + 1) % comm.Size, 0);
                }
                Console.WriteLine(Communicator.world.Rank);
            }
        }

        #endregion
    }
}