Min и Max в MPI - C#
Формулировка задачи:
программа находит минимальный элемент в каждой строке матрицы, далее из минимальных, находит максимальный
Проблемы:
1. не правильно находит max и min
2. Проблема с выводом, когда выводим max и в каком столбце и строке он был найден( не работает правильно из-за пункта 1)
где ошибки, заранее огромное спасибо
class Program
{
static void Main(string[] args)
{
using (new MPI.Environment(ref args))
{
Intracommunicator comm = Communicator.world;
Stopwatch sWatch = new Stopwatch();
int x = 0, x1 = 0, n = 0;
string W = "";
int Num = comm.Rank, Size = comm.Size;
if (Num == 0)
{
Console.WriteLine("Введите ширину матрицы:");
x1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите количество строк матрицы:");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Диапазон случайных чисел 0,n:");
n = Convert.ToInt32(Console.ReadLine());
}
comm.Broadcast(ref x, 0);
comm.Broadcast(ref n, 0);
comm.Broadcast(ref x1, 0);
int[,] mas = new int[x, x1];
if (Num == 0)
{
Random rand = new Random();
if (x <= 10 && x1 <= 10)
{
Console.WriteLine("Заданная матрица:");
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < x1; j++)
{
mas[i, j] = rand.Next(1, n);
if (x <= 10 && x1 <= 10)
{
Console.Write(mas[i, j] + " ");
}
}
if (x <= 10 && x1 <= 10)
{
Console.WriteLine();
}
}
Console.WriteLine();
}
sWatch.Start();
comm.Broadcast(ref mas, 0);
int f = x / Size;
for (int i = f * Num; i < f * (Num + 1); i++)
{
int min = n + 1;
for (int j = 0; j < x1; j++)
{
if (mas[i, j] < min)
{
min = mas[i, j];
}
}
W = W + " " + Convert.ToString(min);
}
string M;
if (Num == 0) comm.Send(W, 1, 0);
else
{
if (Num != Size - 1)
{
M = comm.Receive<string>(Num - 1, 0) + W;
comm.Send<string>(M, Num + 1, 0);
}
}
comm.Barrier();
if (Num == Size - 1)
{
sWatch.Stop();
String M2 = comm.Receive<string>(Num - 1, 0) + W;
Console.WriteLine("Минимальные значения");
Console.Write(M2);
Console.WriteLine();
Console.WriteLine("Максимальное значение {0}", M2.Max());
int max = M2.Max();
int maxx=0;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < x1; j++)
{
if (mas[i, j] == max)
{
maxx = max;
Console.WriteLine("строка: {0}", i + 1);
Console.WriteLine("столбец: {0}", j + 1);
}
if (maxx == max)
{
break;
}
}
if (maxx == max)
{
break;
}
}
Console.WriteLine();
Console.Write("Нахождение минимальных значений заняло {0} мсек", sWatch.ElapsedMilliseconds.ToString());
}
}
Console.WriteLine();
Console.ReadLine();
}
}
}Решение задачи: «Min и Max в MPI»
textual
Листинг программы
int x1 = 10;
int y1 = 10;
int xmin = 0;
int xmax = 0;
rowHandle = e.RowHandle;
for (int i = 0; i < x1; i++)
{
for (int y = 0; y < y1; y++)
{
xmin = mas[i,j];
xmax = mas[i, j];
if (xmin > mas[i, j])
{
xmin = mas[i, j];
}
if (xmax > mas[i, j])
{
xmax = mas[i, j];
}
}
}