C#使用MPI进行高性能计算

MPI.NET是用于Microsoft.NET环境的高性能、易于使用的消息传递接口(MPI)实现。mpi是编写在分布式内存系统(如计算集群)上运行的并行程序的事实上的标准,并且得到了广泛的实现。大多数MPI实现为在C、C++和FORTRAN中编写MPI程序提供了支持。mpi.net提供了对所有.net语言(尤其是c)的支持,并包含重要的扩展(例如对象的自动序列化),使构建在群集上运行的并行程序更加容易。

MPI.NET

作者的Github:https://github.com/jmp75/MPI.NET

文本代码:等待上传

创建项目

注意Framework版本不要低于 4.6.2!

20190522202425

安装类库

使用Nuget安装类库:

20190522202450

20190522202559

使用MPI

public void RunMpiAction(string[] args)
{
    using (new MPI.Environment(ref args))
    {
        //切割数据
        int rank = Communicator.world.Rank - 1;
        int ranks = Communicator.world.Size;
        rank += 1;
        if (rank == 0){
            //数据分为groups组
            int groups = 5;
            for (int i = 1; i < ranks; i++)
            {
                Communicator.world.Send(groups, i, i);
            }
        }
        
        if (rank != 0)
        {
            int groups = Communicator.world.Receive<int>(0, rank);
            Console.WriteLine("rank {0} , groups {1}", rank, groups);
            var list = new List<string>() { $"{rank} - a", $"{rank} - b", $"{rank} - c" };
            //rank -= 1;
            Communicator.world.Send(list, 0, rank);
            Console.WriteLine("current rank is {0}", rank);
        }
        
        if (rank == 0){
            var result = new List<string>();
            for (int i = 1; i < ranks; i++)
            {
                var list = Communicator.world.Receive<List<string>>(i, i);
                result.AddRange(list);
            }
            
            //接受结果
            Console.WriteLine("current rank is {0}", rank);
            for (int i = 0; i < result.Count; i++){
                Console.WriteLine(result[i]);
            }
        }
    }
}

运行结果

需要安装msmpi,不要点击调试运行,需要使用命令执行。

20190522203302

原文地址:https://www.cnblogs.com/muxuan/p/11891661.html