. Net parallel computing-Data Parallel

Source: Internet
Author: User

From the advent of the first computer to the present, computer hardware technology has been greatly developed. Whether it's personal PC or company server. Dual-core, quad-core, and eight-core CPUs are already very common. In this way, we can allocate our program to multiple computer CPUs for computing. In the past, it was very difficult to perform low-level operations on threads in parallel. added support for parallelism in net4.0, making it easy. This time I will discuss the following from the following aspects. NET, parallel data, parallel tasks, parallel Linq tasks, factory notes this time, I will tell you about parallel data, in the following example, data parallelism is used to partition the data in the original set or array and allocate the data to multiple CPUs or threads for the same operation.. net. threading. tasks provides support classes for data parallelism, Parallel. for, Parallel. forEach is very similar to the for and foreach we often use. You do not need to create thread queues, and you do not need to use locks in the basic loop. These. net will help you deal with it. You only need to pay attention to your own business. Let's take a look at Parallel. for and Parallel. how does ForEach use Parallel. for simple use of Parallel. for (0,100, I =>{ dosameting ()}); The above example is not the shadow of the for loop that we often use. Let's talk about Parallel. for the third parameter Action <int> type delegate, no matter whether the delegate parameter is 0 or how many returns are void, how can we get Parallel. for, the following example shows how to use the local variables of the thread to store and retrieve the status of each individual task created by the for loop by using the local data of the thread, you can avoid the overhead of synchronizing a large amount of Access to the shared state. Before all iterations of a task are completed, you compute and store values instead of writing them to the shared resources of each iteration. Then, you can write the final result to the shared resource at one time, or pass it to another method to sum the list <int>. Here we assume that the List length is listLength Parallel. for <long> (0, listLength, () => 0, (j, loop, subsum) => {subsum + = lista [j]; return subsum ;}, (x) => Interlocked. add (ref sum, x); in reality, we often encounter the need to cancel the loop. For example, you can search for a number in the queue. How to exit the Parallel. For loop. Is it okay to use the Break keyword like for and foreach? The answer is no. This is because the break construction is effective for the loop, while the parallel loop is actually a method, not how to cancel the loop. See the following example Parallel. for <long> (0, listLength, () => 0, (j, loop, subsum) => {if (subsum> 20000) {loop. break ();} subsum + = lista [j]; return subsum ;}, (x) => Interlocked. add (ref sum, x); simple Parallel. forEach loop Parallel. the ForEach loop works in a way similar to Parallel. the For loop partitions the source set according to the system environment and Schedules work on multiple threads. The more processors in the system, the faster the parallel method runs. For some source sets, the sequential loop may be faster, depending on the source size and the type of work being executed Parallel. forEach (lista, I =>{ dosameting () ;}); I wonder if you have seen foreach shadows in this place. In fact, the last input parameter of the ForEach method in the preceding example is the Action <int> delegate. When all loops are completed, the method calls the delegate. This is the same as Parallel.. So how do we obtain the returned value is very similar to the For above. I still use the summation of the above array as an example Parallel. forEach <int, long> (lista, () => 0, (j, loop, subsum) => {if (subsum> 20000) {loop. break ();} subsum + = lista [j]; return subsum ;}, (x) => Interlocked. add (ref sum, x); Parallel. for and for performance test comparison we will generate a random number of 10 million here to make a performance comparison For the example. The results on the author's notebook are as follows (the results may be different on your computer) attaches the relevant code for your reference int listLength = 10000000; List <int> listTask = new List <int> (); List <int> list = new List <int> (); stopwatch wattings = Stopwatch. startNew (); Parallel. for (0, listLength, I => {Random r = new Random (100); listTask. add (r. next () ;}); Console. writeLine ("parallel time consumption:" + watch1.ElapsedMilliseconds); Stopwatch watch2 = Stopwatch. startNew (); for (int I = 0; I <listLength; I ++) {Random r = new Random (100); list. add (r. next ();} Console. writeLine ("non-parallel time consumption:" + watch2.ElapsedMilliseconds); here thank you for your understanding of parallel computing.

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.