一 并行任务库TPL
1 并行任务库(TPL,Task Parallel Library)
2 最重要的是Task类,还有Parallel类
3 Task类,是利用线程池来进行任务的执行
比如直接用ThreadPool更优化,而且编程更方便
4 Paallel类,是并行执行任务类的实用类
好处是可以隐式地实用Task,更方便。
二 Task类的使用
1 使用Task.Run方法来得到Task的实例
2 Tasktask=Task.Run()=>SomeFun());
3 可以使用Task.WaitAll(task数组)
4 可以使用task.ContinueWith(另一个task)
5 Task中的异常
可以使用AggregateException(合并的异常)
try
{Task.WaitAll(task1,task2,task3);
}
catch (AggregateException ex)
{foreach (Exception inner in ex.InnerException){Console.WriteLine("Exception type{0} from {1}",inner.GetType(),inner.Source);}
}
三 Parallel类的使用
Parallel.Invoke(Action[] actions);//并行执行多个任务,直到完成
Parallel.For(0,100,i>={…})
Parallel.ForEach(list,item=>{…})
四 并行Linq
1 并行Linq(即PLinq)
2 只要在集合上加个.AsParallel()
var a=(from n in persons.AsParallel()
where n.Age>20&&n.Age<25
select n)
.ToList();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;namespace 使用Task
{internal class Program{static void Main(string[] args){Task<double>[] tasks ={Task.Run(()=>SomeFun()),Task.Run(()=>SomeFun()),};Thread.Sleep(1);for(int i=0;i<tasks.Length;i++){Console.WriteLine(tasks[i].Status);//可以查看状态Console.WriteLine(tasks[i].Result);//取Result时,会等到计算结束}Task.WaitAll(tasks);//也可以用这句,来等结束Console.ReadKey();}static void DoSometing() { }static double SomeFun() { Thread.Sleep(50);return 0; }}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 并行计算矩阵乘法
{internal class Program{static void Main(string[] args){int m = 100, n = 400, t = 1000;double[,] ma = new double[m, n];double[,] mb = new double[n, t];double[,] r1 = new double[m, t];double[,] r2 = new double[m, t];InitMatrix(ma);InitMatrix(mb);InitMatrix(r1);InitMatrix(r2);Console.WriteLine("矩阵乘法");Stopwatch sw = new Stopwatch();sw.Start();MultiMatrixNormal(ma, mb, r1);sw.Stop();Console.WriteLine("普通方法用时" + sw.ElapsedMilliseconds);sw.Restart();MultiMatrixParallel(ma, mb, r2);sw.Stop();Console.WriteLine("并行方法用时" + sw.ElapsedMilliseconds);bool ok = CompareMatrix(r1, r2);Console.WriteLine("结果相同" + ok);Console.ReadKey();}static Random rnd = new Random();static void InitMatrix(double[,] matA){int m = matA.GetLength(0);int n = matA.GetLength(1);for (int i = 0; i < m; i++){for (int j = 0; j < n; j++){matA[i, j] = rnd.Next();}}}static void MultiMatrixNormal(double[,] matA, double[,] matB, double[,] result){int m = matA.GetLength(0);int n = matA.GetLength(1);int t = matB.GetLength(1);for (int i = 0; i < m; i++){for (int j = 0; j < t; j++){double temp = 0;for (int k = 0; k < n; k++){temp += matA[i, k] * matB[k, j];}result[i, j] = temp;}}}static void MultiMatrixParallel(double[,] matA, double[,] matB, double[,] result){int m = matA.GetLength(0);int n = matA.GetLength(1);int t = matB.GetLength(1);Parallel.For(0, m, i =>{for (int j = 0; j < t; j++){double temp = 0;for (int k = 0; k < n; k++){temp += matA[i, k] * matB[k, j];}result[i, j] = temp;}});}static bool CompareMatrix(double[,] matA,double[,] matB){int m = matA.GetLength(0);int n = matA.GetLength(1);for(int i=0;i<m;i++){for(int j=0;j<n;j++){if (Math.Abs(matA[i, j] - matB[i, j]) > 0.1)return false;}}return true;}}
}