C#MPI 第二课 MPI 发送结构体
来源:互联网 发布:mp3铃声制作软件 编辑:程序博客网 时间:2024/04/28 01:03
using System;using MPI;namespace MPIHello{ /// <summary> /// 测试发送结构体 /// </summary> class TestSendStructProgram : IProgram { /// <summary> /// 测试发送的结构 /// </summary> 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 /// <summary> /// 自增 /// </summary> public void Increase() { V1++; V2++; V3++; V4++; } public int V1; public int V2; public int V3; public int V4; } #region Implementation of IProgram /// <summary> /// 应用程序入口点 /// </summary> /// <param name="args">入口参数</param> 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<TestStruct>(Communicator.anySource, 0); msg.Increase(); //输出收到的信息 Console.WriteLine("Rank " + comm.Rank + " received message \"" + msg.ToString() + "\"."); } else { //令n程序接收上一进程发送的数据然后发给下一进程 var msg = comm.Receive<TestStruct>(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 }}