C#MPI 第一课 MPI HelloWord



using System;
using MPI;

namespace MPIHello
{
    /// 
    /// HelloMpi程序
    /// 
    class HelloMpiProgram : IProgram
    {
        #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)
                {
                    //令0进程发送数据然后接收数据
                    comm.Send("Rosie", 1, 0);

                    // receive the final message 
                    var msg = comm.Receive(Communicator.anySource, 0);

                    //输出收到的信息
                    Console.WriteLine("Rank " + comm.Rank + " received message \"" + msg + "\".");
                }
                else
                {
                    //令n程序接收上一进程发送的数据然后发给下一进程
                    var msg = comm.Receive(comm.Rank - 1, 0);

                    //输入收到的消息
                    Console.WriteLine("Rank " + comm.Rank + " received message \"" + msg + "\".");

                    //发送给下一个进程
                    comm.Send(msg + ", " + comm.Rank, (comm.Rank + 1) % comm.Size, 0);
                }
                Console.WriteLine(Communicator.world.Rank);
            }
        }

        #endregion

        
    }
}


你可能感兴趣的:(C#)