Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting up an MPI program!");

            var dataFilePath = args[0];

            // Start up MPI
            // We can wrap the MPI helper library in a using statement so that we can instantiate the MPI connection
            // and have MPI_Finalize automatically called when we are done.
            using (var mpi = new Mpi(args))
            {
                // How big is the cluster, and which node are we?
                int worldSize = mpi.GetWorldSize();
                int worldRank = mpi.GetWorldRank();

                Console.WriteLine($"Rank: {worldRank}, Size: {worldSize}");

                // Do a quick allreduce to show that everything works
                int sum = 0;
                mpi.AllReduce(worldRank, ref sum);
                Console.WriteLine($"AllReduce Rank-Sum: {sum}");

                // Now let's give a little demo showing how we can cooperatively build a model.
                Console.WriteLine("Training a linear model....");
                TrainLinearModel(dataFilePath, (uint)worldRank, out float[] biasAndWeights);

                var averageBiasAndWeights = new float[biasAndWeights.Length];
                mpi.AllReduce(biasAndWeights, averageBiasAndWeights);
                for (int i = 0; i < averageBiasAndWeights.Length; i++)
                {
                    averageBiasAndWeights[i] /= (float)worldSize;
                }

                Console.WriteLine($"Rank-{worldRank}: bias={biasAndWeights[0]} weight[0]={biasAndWeights[1]} | bias={averageBiasAndWeights[0]} weight[0]={averageBiasAndWeights[1]}");
            }
        }