. NET implementation of several ways of parallelism (iii)

Source: Internet
Author: User

This essay continues:. NET implementation of several ways of parallelism (ii)

In the first two essays, the Thread, ThreadPool, IAsyncResult (i.e. APM series), Task, TPL (Task Parallel Library) were introduced successively.

Write to these authors suddenly realize there is another EMP series not written, here to add:

The typical representative in EPM and EPM is WebClient:

The EPM Series uses the ***async method + ***completed Event encoding specification, does not do too many explanations, the specific demo is as follows:

            varAddress ="http://www.cnblogs.com/08shiyan/"; WebClient Client=NewWebClient (); Uri URI=NewUri (address); Client. Downloadstringcompleted+=NewDownloadstringcompletedeventhandler ((Objectsender, DownloadStringCompletedEventArgs e) = =            {                 This. Txttip.settip ("Download Complete");            }); Client.            DownloadStringAsync (URI);  This. Txttip.settip ("start downloading data asynchronously");
WebClient

Vii. parallel version of PLINQ, LINQ 1) First look at the introduction of PLINQ in MSDN:

Parallel LINQ (PLINQ) is a parallel implementation of the LINQ pattern.PLINQ queries are similar in many ways to non-parallel LINQ to Objects queries.As with the sequential LINQ query, PLINQ queries any in-memory IEnumerable or IEnumerable<T>Data sources and defer execution, which means that these operations do not begin until the query is enumerated.The main difference is that PLINQ attempts to take full advantage of all the processors in the system. It takes advantage of all processors by dividing the data source into fragments and then executing queries on multiple processors in parallel on each fragment on a separate worker thread. in many cases, parallel execution means that queries run significantly faster.

By executing in parallel, PLINQ typically simply adds AsParallel query operations to the data source to achieve significant performance improvements on older code for some query types.However, parallelism may introduce its own complexity, so not all query operations run faster in PLINQ. in fact, parallelism 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 clear three points:

1. PLINQ is a parallel version of LINQ, and it has the same powerful genes as LINQ.

2. PLINQ will try to take full advantage of all the processors, (PLINQ describes up to 64 in MSDN), so in many cases PLINQ can significantly improve parallel efficiency, especially CPU-intensive computations.

3, not all query operations run faster in PLINQ.

2) A seamless connection between PLINQ and LINQ

The implementation of PLINQ is based on the following: The PARALLELQUERY<TSOURCE>,LINQ is based on the following: Ienumerable<tsource> and, of course, the generic version.

The two extension methods below make it easy to 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>        ///parallel versions of Plinq:linq/// </summary>         Public voidDemo1 () {Task.run ()=            {                varresult = Enumerable.range (1,Ten). AsParallel (). Where (E ={Settip ("Start"+e); Settip ("Hibernate"+e); Thread.Sleep ( +); Settip ("End"+e); returnE >5;                }); Settip ("Print Results"); foreach(varIteminchresult) {Settip (item.                ToString ()); } settip ("parallel Query Execution complete");        }); }
parallel versions of Plinq:linq

4) The subtle relationship between PLINQ and LINQ

Functionally: PLINQ almost implements the full functionality of LINQ, and the method names are consistent.

On time performance: PLINQ would always attempt to execute a query at least as fast as the query would run sequentially.

As a result, PLINQ performs a "pre-contract" before executing the query, and if certain conditions are found, it may now be automatically converted to sequential execution, or LINQ mode.

5) PLINQ will be executed by default in sequential mode when the query contains the following conditions
  • A query that contains a Select clause, an indexed WHERE clause, an indexed SelectMany clause, or a ELEMENTAT clause (after the sorting or filtering operator has been removed or the index is rearranged).

  • A query that contains the take, TakeWhile, Skip, skipwhile operators, and the indexes in the source sequence are not in the original order.

  • A query that contains a Zip or sequenceequals, unless one of the data sources has an index in its original order, and the other 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 be indexed.

  • A query that contains Reverse, unless applied to a data source that can be indexed.

6) Force PLINQ parallel query

We can specify parallelexecutionmode.forceparallelism to force PLINQ to parallel queries through the Withexecutionmode<TSource> operator.

7) ForAll Multi-Threaded enumeration

You may be baffled by the word "multi-threaded enumeration," First look at the diagram:

If you enumerate parallel query results with the foreach, or IEnumerable interface, the flowchart is shown in the first: after multiple threads are completed in parallel,

There is also a merger operation that returns the result back to the main thread that uses the data and merges the data.

And ForAll is a parallel version of foreach.

8) Other operators

1. AsOrdered<TSource> PLINQ queries are not reserved in order by default. This operator preserves the order of the source sequence, which results in additional sorting overhead and performance degradation.

2, AsUnordered<TSource> is asordered<tsource> anti-operation, the subsequent operation is not in the sequence, can be used for intermediate query, improve performance.

AsUnordered can called anywhere in the query; it does not need to being called immediately after AsParallel.

3. WithCancellation<TSource> for canceling operation

4, Withdegreeofparallelism<TSource> To specify the maximum number of processors to use.

9) Supplement: Factors that affect the performance of PLINQ queries

Attached, demo:http://files.cnblogs.com/files/08shiyan/paralleldemo.zip

See more: Essay Guide: Synchronous vs. asynchronous


(not to be continued ...)

. NET implementation of several ways of parallelism (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.