欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

网络编程(2)

程序员文章站 2024-03-24 19:46:22
...

08 并行

并行串行计算比较

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 test07
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void bt_Click(object sender, RoutedEventArgs e)
        {
            long[] t1 = await Task.Run(() => Multiply(200, 18, 27));
            tb.Text = string.Format("测试1(矩阵1:200x18,矩阵2:18x27),非并行用时{0}毫秒,并行用时{1}毫秒\n", t1[0], t1[1]);
            long[] t2 = await Task.Run(() => Multiply(2000, 180, 270));
            tb.Text += string.Format("测试1(矩阵1:2000x180,矩阵2:180x270),非并行用时{0}毫秒,并行用时{1}毫秒\n", t2[0], t2[1]);
            long[] t3 = await Task.Run(() => Multiply(2000, 200, 300));
            tb.Text += string.Format("测试1(矩阵1:2000x200,矩阵2:200x300),非并行用时{0}毫秒,并行用时{1}毫秒\n", t3[0], t3[1]);

        }

        /// <summary>
        /// 传入行数和列数,初始化一个矩阵
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="cols"></param>
        /// <returns></returns>
        public static int[,] InitializeMatrix(int rows, int cols)
        {
            int[,] matrix = new int[rows, cols];
            Random r = new Random();
            for(int i = 0; i < rows; i++)
            {
                for(int j = 0; j < cols; j++)
                {
                    matrix[i, j] = r.Next(100);
                }
            }
            return matrix;
        }
        /// <summary>
        /// 普通方法计算矩阵乘法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public static void CommonCompute(int[,] a,int[,] b)
        {
            int arows = a.GetLength(0);
            int acols = a.GetLength(1);
            int bcols = b.GetLength(1);
            int[,] result = new int[arows, bcols];
            for(int i = 0; i < arows; i++)
            {
                for(int j = 0; j < bcols; j++)
                {
                    int temp = 0;
                    for (int k = 0; k < acols; k++)
                    {
                        temp += a[i, k] * b[k, j];
                    }
                    result[i, j] = temp;
                }
            }
        }
        /// <summary>
        /// 并行方法计算矩阵乘法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public static void ParallelCompute(int[,] a, int[,] b)
        {
            int arows = a.GetLength(0);
            int acols = a.GetLength(1);
            int bcols = b.GetLength(1);
            int[,] result = new int[arows, bcols];
            Action<int> action = i =>
            {
                for (int j = 0; j < bcols; j++)
                {
                    int temp = 0;
                    for (int k = 0; k < acols; k++)
                    {
                        temp += a[i, k] * b[k, j];
                    }
                    result[i, j] = temp;
                }
            };
            //外循环并行执行
            Parallel.For(0, arows, action);     
        }
        /// <summary>
        /// 计算矩阵乘法
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="cols"></param>
        /// <param name="cols2"></param>
        /// <returns></returns>
        public static long[] Multiply(int rows,int cols,int cols2)
        {
            //初始化矩阵
            int[,] a = InitializeMatrix(rows, cols);
            int[,] b = InitializeMatrix(cols, cols2);

            Stopwatch watch = new Stopwatch();
            long[] timeElapsed = new long[2];
            //串行
            watch.Start();
            CommonCompute(a, b);
            watch.Stop();
            timeElapsed[0] = watch.ElapsedMilliseconds;
            //并行
            watch.Restart();
            ParallelCompute(a, b);
            watch.Stop();
            timeElapsed[1] = watch.ElapsedMilliseconds;
            return timeElapsed;
        }
    }
}

网络编程(2)

相关标签: 课程