On. Data parallelism of net parallel computing

Source: Internet
Author: User
Tags foreach

  This article mainly introduces. NET parallel computing data parallelism, a friend in need can refer to the

From the advent of the first computer to the present computer hardware technology has been a great development. Whether it's a personal PC or a server that the company is using. Dual-core, quad-core, eight-core CPUs are already very common. So we can spread our program to the CPU of multiple computers to compute, in the past parallelization requires the low-level operation of the thread, very difficult, in. net4.0 support for parallelization, making it all very simple. This time I will talk about the following. NET parallel   1.       Data parallel 2.       Task Parallel 3.       Parallel LINQ 4.       Mission factory 5.       notes           This time to tell you about the data parallel nonsense, the following began the       data parallel is actually refers to the original set or number Data in a group is allocated to multiple CPUs or multiple threads perform the same operation System.Threading.Tasks in. NET provides support classes for data parallelism, Parallel.for,parallel.foreach is very similar to the one we use for and foreach so much that you don't have to create thread queues and you don't have to use locks in a basic loop. These. NET will help you to deal with, you only need to focus on your own business   below we'll see how parallel.for and Parallel.ForEach are using  ·    Parallel.For simple Use & nbsp     Code as follows:    parallel.for (0, I => {                &NBS P Dosameting ()            });     The above example is not the shadow of the For loop we often use. Tell me about Parallel.For's third argument. action<int> type of delegate regardless of whether the delegate's argument is 0 or how many heReturns are void, so how do you get the return value in Parallel.For, and the following example shows how to use thread-local variables to store and retrieve the status of each individual task created by the for loop   by using thread-local data. You can avoid the overhead of synchronizing a large number of accesses to a shared state.   Before all iterations of the task are complete, you will calculate and store the values instead of writing the shared resources on each iteration.   You can then write the end result one at a a shared resource, or pass it to another method   the sum of a list<int> we assume the length of the list is listlength      The code is as follows:    Parallel.For<long> (0, Listlength, () => 0, (J, Loop, Subsum) =>             (                subsum + lista[j];         & nbsp       return subsum;               {}, (x) => interlocked.add (ref sum, x));       in reality we often encounter situations where we need to cancel the loop. For example, you look up a number in the queue. So how to exit the Parallel.For loop. is not also the same as for and foreach use the break keyword, the answer to the negative. This is because the break construct is valid for the loop, and the parallel loop is actually a method, not a loop so how to cancel it. Please see the following example     code as follows: Parallel.for<long> (0, Listlength, () => 0, (J, Loop, Subsum) =>     &NBSP ;       {    &NBSP           if (Subsum > 20000)                 {                    loop. Break ();                                   s Ubsum + = Lista[j];                 return subsum;               {}, (x) => interlocked.add (ref sum, x));       simple Parallel.ForEach cycle     Parallel.ForEach cycle works like the parallel.for cycle partitions the source collection according to the system environment, and in many Schedule work on a thread. The more processors in the   system, the faster the parallel methods run.   for some source collections, sequential loops may be faster, depending on the source size and the type of work being performed       code as follows:    parallel.foreach (lista, i => { Dosameting (); });       I wonder if you have seen the shadow of foreach in this place. In fact, the last input parameter of the Foreach method in the above example is the Action<int> delegate, and when all loops are complete, the method invokes the delegate. This place is the same as the parallel.for in front. So how do we get the return value and the for is very similar to the above, I still take the above array sum as an example     code as follows: ParAllel. Foreach<int, Long> (Lista, () => 0, (J, Loop, Subsum) =>         {             IF (Subsum > 20000)              {      &N Bsp          loop. Break ();             }               subsum = lista[j];             return subsum;           , (x) =&G T Interlocked.add (ref sum, x));        Parallel.For and for performance test compare here we produce 10 million random numbers as an example of a performance comparison, The results in the author's Notebook are as follows (may not be the same results on your computer)   Attach the relevant code to everyone reference   code as follows: int listlength = 10000000;             list<int> listtask = new list<int> ();             list<int> List = new list<int> ();             Stopwatch watch1 = Stopwatch.startnew ();               parallel.for (0, listlength, I => {                Random r = new Random (100);                 Listtask.add (R.next ());              });             Console.WriteLine ("Concurrent Time Consuming:" + watch1. Elapsedmilliseconds);                                 &NBSP ;   Stopwatch WATCH2 = Stopwatch.startnew ();               for (int i = 0; i < listlength i++)     &NBSP       {                Random r = new Random (MB);     & nbsp           list. ADD (R.next ());                             Console.WriteLine ("Non-parallel Time consuming: "+ watch2." Elapsedmilliseconds);  

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.