示例#1
0
        static int[] GetDataBatch(
            [NotNull] Communicator world,
            [NotNull] ILogger logger,
            [NotNull] IDataManager manager,
            int processors
            )
        {
            int[] result = null;
            int   rank   = world.Rank;

            logger.Log($"Process {rank} decided to get initial data batch");
            if (rank == 0)
            {
                var batches = manager.Provide(processors);
                logger.Log("Process 0 created data batches");
                for (int index = 0; index < processors; index += 1)
                {
                    logger.Log($"Process 0 sends initial data batch to process {index}");
                    var batchToSend = batches[index];
                    if (index == 0)
                    {
                        result = batchToSend;
                    }
                    else
                    {
                        world.Send <int[]>(batchToSend, index, 0);
                    }
                }
            }
            else
            {
                logger.Log($"Process {rank} decided to receive initial batch");
                result = world.Receive <int[]>(0, 0);
            }

            if (result == null)
            {
                throw new InvalidOperationException($"Process {rank} was given null data batch");
            }

            logger.Log($"Process {rank} is done receiving batch. Synchronizing...");
            // This barrier could have been safely removed
            // but is left for debugging purposes
            world.Barrier();
            logger.Log($"Process {rank} finished receiving initial batch");
            return(result);
        }
示例#2
0
    static void TestCancellation(Communicator comm)
    {
        int datum         = comm.Rank;
        int expectedDatum = (comm.Rank + comm.Size - 1) % comm.Size;

        int[]    intArrayRecvBuffer = new int[expectedDatum + 1];
        string[] strArrayRecvBuffer = new string[expectedDatum + 1];

        int[]    intArraySendBuffer = new int[comm.Rank + 1];
        string[] strArraySendBuffer = new string[comm.Rank + 1];
        for (int i = 0; i <= comm.Rank; ++i)
        {
            intArraySendBuffer[i] = i;
            strArraySendBuffer[i] = i.ToString();
        }

        // Test cancellation of receive requests
        Request[] requests = new Request[4];
        requests[0] = comm.ImmediateReceive <int>(Communicator.anySource, 0);
        requests[1] = comm.ImmediateReceive <string>(Communicator.anySource, 1);
        requests[2] = comm.ImmediateReceive(Communicator.anySource, 2, intArrayRecvBuffer);
        requests[3] = comm.ImmediateReceive(Communicator.anySource, 3, strArrayRecvBuffer);

        // Cancel all of these requests.
        requests[0].Cancel();
        requests[1].Cancel();
        requests[2].Cancel();
        requests[3].Cancel();

        // Check that the requests were actually cancelled
        for (int i = 0; i < 4; ++i)
        {
            if (!requests[0].Wait().Cancelled)
            {
                System.Console.Error.WriteLine("error: cancelled receive request "
                                               + i.ToString() + " not marked as cancelled");
                comm.Abort(1);
            }
        }

        comm.Barrier();
    }
示例#3
0
    static void TestCancellation(Communicator comm)
    {
        int datum = comm.Rank;
        int expectedDatum = (comm.Rank + comm.Size - 1) % comm.Size;
        int[] intArrayRecvBuffer = new int[expectedDatum + 1];
        string[] strArrayRecvBuffer = new string[expectedDatum + 1];

        int[] intArraySendBuffer = new int[comm.Rank + 1];
        string[] strArraySendBuffer = new string[comm.Rank + 1];
        for (int i = 0; i <= comm.Rank; ++i)
        {
            intArraySendBuffer[i] = i;
            strArraySendBuffer[i] = i.ToString();
        }

        // Test cancellation of receive requests
        Request[] requests = new Request[4];
        requests[0] = comm.ImmediateReceive<int>(Communicator.anySource, 0);
        requests[1] = comm.ImmediateReceive<string>(Communicator.anySource, 1);
        requests[2] = comm.ImmediateReceive(Communicator.anySource, 2, intArrayRecvBuffer);
        requests[3] = comm.ImmediateReceive(Communicator.anySource, 3, strArrayRecvBuffer);

        // Cancel all of these requests.
        requests[0].Cancel();
        requests[1].Cancel();
        requests[2].Cancel();
        requests[3].Cancel();

        // Check that the requests were actually cancelled
        for (int i = 0; i < 4; ++i)
        {
            if (!requests[0].Wait().Cancelled)
            {
                System.Console.Error.WriteLine("error: cancelled receive request " 
                    + i.ToString() + " not marked as cancelled");
                comm.Abort(1);
            }
        }

        comm.Barrier();
    }
示例#4
0
        static void MPIMatrixMultiple(Communicator comm, string groupName)
        {
            var fileOffset = 2 * sizeof(int);
            var matrixManager = new MatrixManager();
            var matrixA = matrixManager.FileInitialization("A");
            var matrixB = matrixManager.FileInitialization("B");

            //var matrixA = new Matrix(11, 2);
            //var matrixB = new Matrix(2, 4);
            //Console.WriteLine("{0} {1} {2}", matrixA.Rows, matrixA.Columns, matrixB.Columns);
            //matrixManager.SimpleInitialization(matrixA);
            //matrixManager.SimpleInitialization(matrixB);

            var rowsA = matrixA.Rows;
            var columnA = matrixA.Columns;
            var columnB = matrixB.Columns;

            if (comm.Rank == 0)
            {
                int numberOfSlaves = comm.Size - 1;
                var start = DateTime.Now;

                int rowsPerSlave = rowsA / numberOfSlaves;
                int remainingRows = rowsA % numberOfSlaves;
                int offsetRow = 0;

                var b = matrixB.Data;

                ((Intracommunicator)comm).Broadcast(ref b, 0);

                for (var destination = 1; destination <= numberOfSlaves; destination++)
                {
                    int rows = (destination <= remainingRows) ? rowsPerSlave + 1 : rowsPerSlave;

                    comm.Send(offsetRow, destination, (int)MessageType.FromMaster);
                    comm.Send(rows, destination, (int)MessageType.FromMaster);

                    var temp = new double[rows][];

                    for (var i = 0; i < rows; i++)
                    {
                        temp[i] = matrixA.Data[offsetRow];
                    }

                    comm.Send(temp, destination, (int)MessageType.FromMaster);
                    offsetRow = offsetRow + rows;
                }

                using (var fs = new FileStream(groupName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
                {
                    byte[] buff = BitConverter.GetBytes(rowsA);
                    fs.Write(buff, 0, sizeof(int));

                    buff = BitConverter.GetBytes(columnB);
                    fs.Write(buff, 0, sizeof(int));
                }

                comm.Barrier();

                DateTime end = DateTime.Now;
                Console.WriteLine("Group name: {0} with size = {1}", groupName, comm.Size);
                Console.Write("\n\n");
                Console.Write(end - start);
            }
            else
            {
                double[][] b = null;

                ((Intracommunicator)comm).Broadcast<double[][]>(ref b, 0);

                const int source = 0;
                var offsetRow = comm.Receive<int>(source, (int)MessageType.FromMaster);
                var rows = comm.Receive<int>(source, (int)MessageType.FromMaster);
                var a = comm.Receive<double[][]>(source, (int)MessageType.FromMaster);

                var c = new double[rows][];

                for (var i = 0; i < rows; i++)
                {
                    c[i] = new double[columnB];
                }

                //Console.WriteLine("{0} {1} {2}", columnB, rows, columnA);

                using (var fs = new FileStream(groupName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
                {
                    fs.Seek(sizeof (double)*offsetRow*columnB + fileOffset, SeekOrigin.Begin);
                    for (var k = 0; k < columnB; k++)
                    {
                        for (var i = 0; i < rows; i++)
                        {
                            c[i][k] = 0.0;
                            for (var j = 0; j < columnA; j++)
                            {
                                c[i][k] = c[i][k] + a[i][j] * b[j][k];
                            }

                            //Console.WriteLine("{0} {1:0.###} {2} {3} {4} {5}", comm.Rank, c[i][k], sizeof(double), offsetRow, i, k);
                            byte[] buff = BitConverter.GetBytes(c[i][k]);
                            fs.Write(buff, 0, sizeof(double));
                        }
                    }
                }

                comm.Barrier();
            }
        }