MPI параллельное перемножение матриц - C#

Узнай цену своей работы

Формулировка задачи:

Форумчане, помогите,чем можете.Самой никак не справиться... Нужен код на C# MPI параллельного перемножения матриц (ленточным способом, блочным, или любым другим). Главное,чтобы процессов использовалось не много,а матрицы -большие,размерностью 100 на 100. Уже есть код , в котором каждый процесс вычисляет 1 элемент. Т.е. кол-во процессов=колву элементов. А надо, чтобы после того, как на одном процессе вычислился элемент, сразу начинал вычисляться другой на том же процессе.

Решение задачи: «MPI параллельное перемножение матриц»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MPI;
  6. using System.IO;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             using (new MPI.Environment(ref args))
  15.             {
  16.                 Intracommunicator comm = Communicator.world;
  17.                 int rank = comm.Rank;
  18.                 int size = comm.Size;
  19.                 //double[,] A = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
  20.                 //double[,] B = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
  21.                 double[,] A = SchitIzFaila("a.txt");
  22.                 double[,] B = SchitIzFaila("b.txt");
  23.                 int razmer = A.GetLength(0);
  24.                 double[,] C = new double[razmer, razmer];
  25.                 double[] tempMatrix = new double [size];
  26.                 if (rank == 0)
  27.                 {
  28.                     Console.WriteLine("Matrica A");
  29.                     vyvodvconsol(A);
  30.                     Console.WriteLine("Matrica B");
  31.                     vyvodvconsol(B);
  32.                 }
  33.                 if (rank == 0)
  34.                 {
  35.                     int IndexRank = 1;
  36.                     for (int i = 0; i < razmer; i++)
  37.                     {
  38.                         for (int j = 0; j < razmer; j++)
  39.                         {
  40.                             if (IndexRank % 2 != 0)
  41.                             { int[] koordinata = new int[2] { i, j };
  42.                             comm.Send(koordinata, IndexRank, 0);
  43.                            IndexRank++;
  44.                           // comm.Barrier();
  45.                             }
  46.  
  47.                             //else
  48.                             //{
  49.                             //    int[] koordinata = new int[2] { i, j };
  50.                             //    comm.Send(koordinata, IndexRank, 0);
  51.                             //    IndexRank--;
  52.                             //    comm.Barrier();
  53.                             //}
  54.                         }
  55.                     }
  56.                     comm.Gather(-1, 0, ref tempMatrix);
  57.                     comm.Barrier();
  58.                     C = PoluchMatricuIzPosledovat(tempMatrix, razmer);
  59.                     Console.WriteLine("Matr C");
  60.                     vyvodvconsol(C);
  61.                 }
  62.                 else
  63.                 {
  64.                     int[] koordinata = new int[2];
  65.                     comm.Receive(0, 0, ref koordinata);
  66.  
  67.                     double resultat = Element(PoluchitStroku(koordinata[0], A), PoluchitStolbec(koordinata[1], B));
  68.                     comm.Gather(resultat, 0, ref tempMatrix);
  69.                     comm.Barrier();
  70.  
  71.                 }
  72.  
  73.             }
  74.  
  75.         }
  76.         public static double[,] SchitIzFaila(string adress)
  77.         {
  78.             StreamReader sr = new StreamReader(adress);
  79.             int columns = 0;
  80.             int lines = 0;
  81.             while (!sr.EndOfStream)
  82.             {
  83.                 string temp = sr.ReadLine();
  84.                 columns = temp.Split(' ').Count();
  85.                 lines++;
  86.             }
  87.             double[,] result = new double[lines, columns];
  88.             sr = new StreamReader(adress);
  89.             for (int i = 0; i < result.GetLength(0); i++)
  90.             {
  91.                 string[] temp = sr.ReadLine().Split(' ');
  92.                 for (int j = 0; j < result.GetLength(1); j++)
  93.                 {
  94.                     result[i, j] = Convert.ToDouble(temp[j]);
  95.                 }
  96.             }
  97.                 return result;
  98.            
  99.         }
  100.                
  101.         public static void vyvodvconsol(double[,] matrix)
  102.         {
  103.             Console.WriteLine();
  104.             for (int i = 0; i < matrix.GetLength(0); i++)
  105.             {
  106.                 for (int j = 0; j < matrix.GetLength(1); j++)
  107.                 {
  108.                     Console.Write(matrix[i, j]);
  109.                     Console.Write(" ");
  110.                 }
  111.                 Console.Write('\n');
  112.             }
  113.             Console.WriteLine();
  114.         }
  115.         public static double[] PoluchitStroku(int idLine, double[,] M)
  116.         {
  117.             double[] result = new double[M.GetLength(1)];
  118.             for (int i = 0; i < result.Length; i++)
  119.             {
  120.                 result[i] = M[idLine, i];
  121.             }
  122.             return result;
  123.         }
  124.         public static double[] PoluchitStolbec(int idColumn, double[,] M)
  125.         {
  126.             double[] result = new double[M.GetLength(0)];
  127.             for (int i = 0; i < result.Length; i++)
  128.             {
  129.                 result[i] = M[i,idColumn];
  130.             }
  131.             return result;
  132.         }
  133.         public static double Element(double[] line, double[] column)
  134.         {
  135.             double result = 0;
  136.             for (int i = 0; i < line.Length; i++)
  137.             {
  138.                 result += line[i] * column[i];
  139.             }
  140.             return result;
  141.         }
  142.         public static double[,] PoluchMatricuIzPosledovat(double[] matrix, int size)
  143.         {
  144.             double[,] resmatrix = new double[size, size];
  145.             int matrixIndex = 1;
  146.             for (int i = 0; i < size; i++)
  147.             {
  148.                 for (int j = 0; j < size; j++)
  149.                 {
  150.                     resmatrix[i, j] = matrix[matrixIndex];
  151.                     matrixIndex++;
  152.                 }
  153.             }
  154.             return resmatrix;
  155.         }
  156.     }
  157. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 4.222 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут