示例#1
0
        private static void CalculateInOrder(int size)
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            InitializeInputData(size);
            _finalResult = MultiplicationHelper.Multiply(_matrix, _vector);
            stopWatch.Stop();
            Console.WriteLine($"Time elapsed: {stopWatch.Elapsed} ms");
        }
示例#2
0
        private static void CalculateInParallel(ref string[] args, int size)
        {
            MPI.Environment.Run(ref args, comm =>
            {
                var stopWatch = new Stopwatch();
                stopWatch.Start();

                InitializeProcessCommonData(size, comm.Size);
                InitializeRowDistributionData(size, comm.Size);

                if (comm.Rank == 0)
                {
                    InitializeInputData(size);
                    //PrintInput();
                    foreach (var process in _rowProcessDistribution.Keys)
                    {
                        var rowsToProcess = _rowProcessDistribution[process];
                        var rowsArray     = GetMatrixRowsList(rowsToProcess);
                        comm.Send(rowsArray, process, (int)Tags.MatrixRequest);
                        comm.Send(_vector.ToList(), process, (int)Tags.VectorRequest);
                    }
                }
                else
                {
                    if (_rowProcessDistribution.ContainsKey(comm.Rank))
                    {
                        var rowsArray             = comm.Receive <List <int> >(0, (int)Tags.MatrixRequest);
                        var vector                = comm.Receive <List <int> >(0, (int)Tags.VectorRequest).ToArray();
                        var separateRows          = GetSeparateRowsFromList(rowsArray, size);
                        var multiplicationResults = new List <int>();
                        foreach (var row in separateRows)
                        {
                            multiplicationResults.Add(MultiplicationHelper.Multiply(row, vector));
                        }

                        _localResult = multiplicationResults.ToArray();
                    }
                }
                comm.GatherFlattened(_localResult, 0, ref _finalResult);
                if (comm.Rank == 0)
                {
                    _finalResult = _finalResult.Skip(_rowsPerProcessCount).Take(size).ToArray();
                    stopWatch.Stop();
                    //PrintFinalOutput();
                    Console.WriteLine($"Time elapsed: {stopWatch.Elapsed} ms");
                }
            });
        }