c#——矩阵并行计算练习实验

熟悉和掌握利用Parallel类完成数据并行编程的程序设计方法。

编写一个WPF应用程序,利用数据并行计算两个矩阵(M×N和N×P)的乘积,得到一个M×P的矩阵。

具体要求
(1)在代码中用多任务通过调用某方法实现矩阵并行运算,在调用的参数中分别传递M、N、P的大小。
(2)程序中至少要测试3次有代表性的不同大小的矩阵运算,并显示其并行运行用时。

MainWindow.xaml

<Window x:Class="shiyan3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:shiyan3"
        mc:Ignorable="d"
        Title="利用Parallel.For实现矩阵相乘" Height="450" Width="800">
    <DockPanel>
        <Border DockPanel.Dock="Bottom" Background="Beige" Padding="0 5 0 5">
            <Button x:Name="startBtn" Content="开始" HorizontalAlignment="Center" Padding="20 0 20 0" Click="startBtn_Click"/>
        </Border>
        <ScrollViewer>
            <StackPanel>
                <TextBlock x:Name="tb" TextWrapping="Wrap"/>
            </StackPanel>
        </ScrollViewer>
    </DockPanel>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace shiyan3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Stopwatch stopwatch = new Stopwatch();
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void startBtn_Click(object sender, RoutedEventArgs e)
        {
            long time1 = await Task.Run(() => Multiply(200, 18, 27));
            tb.Text = string.Format("测试1(矩阵1:200×18,矩阵2:18×27),用时:{0}毫秒", time1);

            long time2 = await Task.Run(() => Multiply(2000, 180, 270));
            tb.Text += string.Format("\n测试2(矩阵1:2000×180,矩阵2:180×270),用时:{0}毫秒",time2);

            long time3 = await Task.Run(() => Multiply(3000, 200, 300));
            tb.Text += string.Format("\n测试3(矩阵1:2000×200,矩阵2:200×300),用时:{0}毫秒", time3);
        }
        public long Multiply(int row,int col,int col2)
        {
            double[,] matrix1=InitializeMatrix(row,col);
            double[,] matrix2=InitializeMatrix(col,col2);
            double[,] result = new double[row, col2];
            stopwatch.Restart();
            MultiplyMatrices(matrix1, matrix2,result);
            stopwatch.Stop();
            return stopwatch.ElapsedMilliseconds;
        }
        public void MultiplyMatrices(double[,]matrix1, double[,] matrix2, double[,]matrix3)
        {
            int a = matrix1.GetLength(0);
            int b = matrix1.GetLength(1);
            int c = matrix2.GetLength(1);

            Action<int> action = i =>
            {
                for (int j = 0; j < c; j++)
                {
                    double temp = 0;
                    for (int k = 0; k < b; k++)
                    {
                        temp += matrix1[i, k] + matrix2[k, j];
                    }
                    matrix3[i, j] = temp;
                }
            };
            Parallel.For(0, a, action);
        }
        public double[,] InitializeMatrix(int rows,int cols)
        {
            double[,] matrix = new double[rows, cols];
            Random random = new Random();
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    matrix[i, j] = random.Next(50);
                }
            }
            return matrix;
        }
    }
}

在山腰间飘逸的红雨
随着北风凋零
我轻轻摇曳风铃
想唤醒被遗弃的爱情
雪花已铺满了地
深怕窗外枫叶已结成冰
缓缓飘落的枫叶像思念
我点燃烛火温暖岁末的秋天
极光掠夺天边
北风掠过想你的容颜
我把爱烧成了落叶
却换不回熟悉的那张脸

猜你喜欢

转载自blog.csdn.net/qq_64628470/article/details/130300226