. NET parallel implementation methods (3). net parallel
This article continued: several parallel methods implemented by. NET (2)
In the first two essays, we introduced Thread, ThreadPool, IAsyncResult (APM series), Task, and TPL (Task Parallel Library ).
The author suddenly realized that there is another EMP series that has not been written. Here I will add:
VI. The typical representatives of the PMNs and PMNs are WebClient:
* Async method + *** Completed event encoding specification is adopted in the EPM series. The specific demo is as follows:
Var address = "http://www.cnblogs.com/08shiyan/"; WebClient client = new WebClient (); Uri uri = new Uri (address); client. downloadStringCompleted + = new DownloadStringCompletedEventHandler (object sender, DownloadStringCompletedEventArgs e) =>{ this.txt Tip. setTip ("download completed") ;}); client. downloadStringAsync (uri); this.txt Tip. setTip ("START asynchronous data download ");WebClient
VII. Parallel versions of PLINQ and LINQ 1) First, let's take a look at the introduction of PLINQ in MSDN:
Parallel LINQ (PLINQ) is a parallel implementation of the LINQ mode. PLINQ queries are similar to non-parallel LINQ to Objects queries in many aspects. Like the sequential LINQ query, PLINQ queries perform operations on IEnumerable or IEnumerable <T> data sources in any memory and delay the execution, which means that these operations are not performed before enumeration queries. The main difference is that PLINQ tries to make full use of all the processors in the system. It uses all the processors to divide the data source into segments, and then execute queries on each segment of a separate worker thread in parallel on multiple processors. In many cases, parallel execution means that the query speed is significantly improved.
Through parallel execution, PLINQ generally only needs to add the AsParallel query operation to the data source to achieve significant performance improvement on earlier versions of some query types. However, parallelism may introduce its own complexity, so not all query operations run faster in PLINQ. In fact, parallel processing reduces the speed of some queries. Therefore, you should understand how problems such as sorting affect parallel queries. For more information, see Understanding Speedup in PLINQ.
From the introduction, we can clarify three points:
1. PLINQ is a parallel version of LINQ. It has the same powerful genes as LINQ.
2. PLINQ tries to make full use of all the processors (PLINQ is described in MSDN at most 64). Therefore, PLINQ can significantly improve the parallel efficiency in many cases, in particular, the parallel rate of CPU-intensive computing.
3. Not all query operations run faster in PLINQ.
2) seamless connection between PLINQ and LINQ
The basic implementation of PLINQ is ParallelQuery <TSource>. The basic implementation of LINQ is IEnumerable <TSource>. Of course, this is a generic version.
The following two extension methods can easily convert between PLINQ and LINQ.
Public static ParallelQuery <TSource> AsParallel <TSource> (this IEnumerable <TSource> source );
Public static IEnumerable <TSource> AsSequential <TSource> (this ParallelQuery <TSource> source );
3) simple Demo /// <summary> /// PLinq: parallel version of Linq /// </summary> public void Demo1 () {Task. run () => {var result = Enumerable. range (1, 10 ). asParallel (). where (e => {SetTip ("START" + e); SetTip ("Sleep" + e); Thread. sleep (1000); SetTip ("end" + e); return e> 5 ;}); SetTip ("print result"); foreach (var item in result) {SetTip (item. toString () ;}settip ("parallel query execution completed ");});}PLinq: parallel version of Linq
4) The subtle relationship between PLINQ and LINQ
In terms of functionality: PLINQ almost implements all the functions of LINQ, and the method names are consistent.
In terms of time performance: PLINQ will always attempt to execute a query at least as fast as the query wocould run sequentially.
Therefore, PLINQ performs a "pre-prediction" before executing the query. If some situations are found, it may be automatically converted to the sequential execution mode, that is, the LINQ mode.
5) when the query contains the following conditions, PLINQ will be executed in order mode by default.
Queries that contain the Select clause, the Where clause that has created an index, the selectlimit clause that has created an index, or the ElementAt clause (after the sorting or filtering operator is removed or the index is rearranged ).
Queries that contain the Take, TakeWhile, Skip, and SkipWhile operators and the indexes in the source sequence do not use the original sequence.
Queries that contain Zip or SequenceEquals, unless one data source has an index in the original order and another data source can be indexed (for example, an array or IList (T )).
A query that contains Concat, unless it is applied to a data source that can create an index.
Queries that contain Reverse, unless applied to data sources that can be indexed.
6) force PLINQ parallel query
We can use the WithExecutionMode <TSource> operator to specify ParallelExecutionMode. ForceParallelism to force PLINQ parallel queries.
7) ForAll multi-thread Enumeration
You may be confused about the word "multi-thread enumeration". First, let's look at the figure:
If you use the foreach or IEnumerable interface to enumerate the parallel query results, the flowchart is shown in the first one: after multiple threads are completed in parallel,
There is also a Merger operation that returns the result to the main thread that uses the data and merges the data.
ForAll is a parallel version of Foreach.
8) Other operators
1. AsOrdered <TSource> PLINQ queries do not retain the order by default. This operator can preserve the order of the source sequence (it will produce additional sorting overhead and reduce performance ).
2. AsUnordered <TSource> is an AsOrdered <TSource> reverse operation that does not keep the sequence for subsequent operations. It can be used for intermediate queries to improve performance.
AsUnordered can be called anywhere in the query;It does not needTo be called immediately after AsParallel.
3. WithCancellation <TSource> is used to cancel the operation.
4. WithDegreeOfParallelism <TSource> is used to specify the maximum number of processors that can be used.
9) supplement: Factors Affecting PLINQ query performance
Appendix, Demo: http://files.cnblogs.com/files/08shiyan/ParallelDemo.zip
See more: Casual guide: synchronous and asynchronous
(To be continued ...)