Параллельные вычисления - C# (179750)
Формулировка задачи:
Нужно использовать параллельные вычисления, при решении данной задачи, пробовал распараллелить циклы, но выдавало не верный результат, код ниже. Буду благодарен любой помощи! (Приводить код с распараллеливанием не вижу смысла, так как он не правильно работает).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication11
{
public class Matrix
{
double[,] matrix;
public int Row { get; protected set; }
public int Column { get; protected set; }
public Matrix(int row, int column)
{
Row = row;
Column = column;
matrix = new double[row, column];
}
public Matrix Multiple(Matrix value)
{
Matrix result = new Matrix(Row, value.Column);
for (int i = 0; i < Row; i++)
for (int j = 0; j < value.Column; j++)
for (int k = 0; k < value.Row; k++)
{
result.matrix[i, j] += matrix[i, k] * value.matrix[k, j];
maxValue = (maxValue < result.matrix[i, j] ? result.matrix[i, j] : maxValue);
}
return result;
}
public double maxValue;
public void Read()
{
for (int i = 0; i < Row; i++)
for (int j = 0; j < Column; j++)
{
Console.Write("Enter element [{0},{1}]: ", i + 1, j + 1);
matrix[i, j] = System.Convert.ToDouble(Console.ReadLine());
}
}
public void Print()
{
for (int i = 0; i < Row; i++)
{
for (int j = 0; j < Column; j++)
Console.Write("{0:f0} ", matrix[i, j]);
Console.WriteLine();
}
}
}
class Program
{
static void Main(string[] args)
{
Matrix vector = new Matrix(1, 4);
Matrix matrix = new Matrix(4, 4);
Console.Clear();
Console.WriteLine("Enter vector");
vector.Read();
Console.WriteLine("\nEnter matrix");
matrix.Read();
Console.Clear();
Matrix result = vector.Multiple(matrix);
Console.WriteLine("\nTask 1 complete!");
Console.WriteLine("\nTask 2 complete!");
Console.WriteLine("\nTask 3 complete!");
Console.WriteLine("Vector:");
vector.Print();
Console.WriteLine("\nMatrix:");
matrix.Print();
Console.WriteLine("\nResult of multiplication:");
result.Print();
Console.WriteLine("\nMax. digit in vector:");
Console.WriteLine(vector.maxValue);
Console.WriteLine("\nEnter any key to exit...");
Console.ReadKey(true);
}
}
}Решение задачи: «Параллельные вычисления»
textual
Листинг программы
using System;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication11
{
public class Matrix
{
double[,] matrix;
public int Row { get; protected set; }
public int Column { get; protected set; }
public Matrix(int row, int column)
{
Row = row;
Column = column;
matrix = new double[row, column];
}
public Matrix Multiple(Matrix value)
{
Matrix result = new Matrix(Row, value.Column);
Parallel.For(0, Row, (i) =>
{
Parallel.For(0, value.Column, (j) =>
{
Parallel.For(0, value.Row, (k) =>
{
result.matrix[i, j] += matrix[i, k] * value.matrix[k, j];
maxValue = (maxValue < result.matrix[i, j] ? result.matrix[i, j] : maxValue);
});
});
});
return result;
}
public double maxValue;
public void Read()
{
for (int i = 0; i < Row; i++)
for (int j = 0; j < Column; j++)
{
Console.Write("Введiть [{0},{1}]: ", i + 1, j + 1);
matrix[i, j] = System.Convert.ToDouble(Console.ReadLine());
}
}
public void Print()
{
for (int i = 0; i < Row; i++)
{
for (int j = 0; j < Column; j++)
Console.Write("{0:f0} ", matrix[i, j]);
Console.WriteLine();
}
}
}
class Program
{
static void Main(string[] args)
{
Matrix vector = new Matrix(1, 4);
Matrix matrix = new Matrix(4, 4);
Console.Clear();
Console.WriteLine("Введiть вектор:");
vector.Read();
Console.WriteLine("\nВведiть матрицю:");
matrix.Read();
Console.Clear();
Matrix result = vector.Multiple(matrix);
Console.WriteLine("Вектор:");
vector.Print();
Console.WriteLine("\nМатриця:");
matrix.Print();
Console.WriteLine("\nРезультат множення:");
result.Print();
Console.WriteLine("\nМаксимальне число в векторi:");
Console.WriteLine(vector.maxValue);
Console.WriteLine("\nВведiть будь-яку клавiшу для виходу...");
Console.ReadKey(true);
}
}
}