Mistakes in DotNet parallel computing (1)

Source: Internet
Author: User

Parallel Computing is undoubtedly. net Framework Platform, which automatically breaks down a task and executes it in the form of concurrency. programmers do not have to worry about collaboration and synchronization between tasks, this allows you to focus more on business implementation.

In. NET, TPL (Task Parallel Library) is a Parallel Task Library. It is designed to write more simple managed code that can use multiple processors automatically. With this library, you can easily use existing serial code to express potential parallelism. In this way, parallel tasks published in the serial code will run simultaneously on all available processors, this usually greatly increases the speed.

However, from the perspective of many released parallel computing examples on the Internet, there are many misunderstandings or even misleading ones, which leads to some incorrect ideas by frontline programmers, the advantages of parallel computing are illustrated through examples. It seems that programmers can increase the program performance by N times without any effort. If these ideas are not compared, they will be applied to reality, this will cause some losses. This article will talk about the rational use of parallel computing for your reference. These misunderstandings include:

1. As long as parallel processing is used, the program performance will be improved.

2. The more nested Parallel Loops, the higher the program performance

3. parallel computing is a task of running.

Let's explain these misunderstandings one by one.

● Misunderstanding 1. As long as parallel processing is used, the program performance will be improved.

This is not the Case in real time. In fact, the use of parallel computing imposes strict requirements on the premise. Generally, the use of parallel computing in large quantities will not improve performance, but will be counterproductive. The following two cases will be explained to you.

Case 1. It is not objective to use Thread. Sleep () to compare the performance of parallel and single-line programs.

In many examples of Performance Comparison between parallel computing and a single-line program, many examples include Thread. run the Sleep () statement to run such a Demo. We can see that the parallel time results have been improved so much, but have you carefully studied the reasons for the time reduction?

There are two sections of code:

Code Part:

For (int I = 0; I <10; I ++) {a = I. ToString (); Thread. Sleep (200 );}
Code Part B:

 

Parallel. For (0, 10, (I) => {a = I. ToString (); Thread. Sleep (200 );});
On my dual-core local machine, the test results are exciting: Code Part A ran for more than 2 seconds, while Code Part B ran for more than 800 milliseconds, greatly reducing the time, but then you decide to migrate your code to the parallel mode?

I suggest you wait. The reason why Code Part B has higher performance than A is not the performance improvement caused by parallel primary Code, but the performance improvement caused by Sleep () in A parallel environment, the task is actually only resting 1/N (N is the number of parallel tasks), rather than all in a single program. This is because TPL breaks down loop work on a dual-core local machine, code Part B is equivalent to two or five cycles at the same time. Sleep () seldom consumes shared resources and does not need to be synchronized with other processes, therefore, the running time is lower than that of A 10-time loop. If we remove the Sleep statement in Code Part A and B, what is the result? The answer is that the two are very close. In fact, in the case that the main code is small, parallel computing will show certain performance instability. Here, let's keep a little bit. If you are interested, let's test it by yourself.

When you select parallel computing to handle problems, you must first ensure that your samples or requirements are suitable for parallel processing. For example, you cannot use parallel computing when writing a sorting algorithm.

Case 2: parallel programs have different processing efficiency for strings and numbers.

@ String accumulation:

Single row:

For (int I = 0; I <100000; I ++) {a + = I. ToString ();}
Parallelism:

 

Parallel. For (0, 100000, (I) => {a + = I. ToString ();});
@ String comparison:

Single row:

For (int I = 0; I <100000; I ++) {f = a. Equals (I. ToString ());}
Parallelism:

 

Parallel. For (0, 100000, (I) =>{ f = a. Equals (I. ToString ());});
@ Numeric comparison:

Single row:

 

For (int I = 0; I <100000000; I ++) {f = I = j ;}
Parallelism:

 


Parallel. For (0, 100000000, (I) => {f = I = j ;});
Running the above three test codes shows that TPL is very satisfied with string processing. Therefore, not all situations are suitable for the TPL library handler. This is very important for traversal scenarios in the program. When using PLINQ, we recommend that you analyze the sample space types and specific operations first, then decide which computing to use.

● Misunderstanding 2. The more concurrent loop nesting, the higher the program performance

For code segments with more than two layers of loops, before you decide to use parallelism, you must first find out where the main consumption of the Code is, whether it is in the outer layer or in the internal layer, only when the performance improvement brought by parallelism is greater than the loss is evaluated, it is not too late to reconstruct the data into parallel because TPL provides parallel support in many cases, many programmers use parallelism in places where they can use parallelism without passing tests and comparisons. In fact, many times, when parallel features are added in all places, the performance of the program will be compromised, no case is provided here.

It can be seen that although TPL automatically manages the objects in the loop, these "automatic" results in some performance loss, if our code constantly requires TPL to split, merge, and synchronize objects, while ignoring the optimization of the business itself, it is undoubtedly not good for performance.

Here, Aicken provides a suggestion to evaluate the number of tasks in this code before parallel execution. Is there any need for parallel execution? Does this code compete for "write" Requirements for disks? What tasks does the Code process and whether the code Runtime Environment supports parallel processing? Furthermore, performance tests are performed after code refactoring to ensure the significance of parallel computing.

In fact, in addition to improving the performance of parallel computing, there is also a valuable aspect: Code reconstruction, concise and structured code, and more in line with the requirements of coding progress, isn't it?

(To be continued)

Tiandao rewards
Original reprinted by Li Ming (aicken)

Related Article

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.