On. NET Parallel Computing data Parallel _ practical skills

Source: Internet
Author: User

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 am speaking from the following aspects of the following. NET parallel

1. Data parallelism
2. Task Parallelism
3. Parallel LINQ
4. Mission Factory
5. Matters needing attention

This time the main to tell you the data parallel nonsense do not say, the following began

Data parallelism is actually refers to the original collection or the data in the array allocated to multiple CPUs or multiple threads to perform the same operation in. NET System.Threading.Tasks 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 then let's take a look at how parallel.for and Parallel.ForEach are used.

Parallel.For Simple to use

Copy Code code as follows:

Parallel.For (0, I => {
Dosameting ()
});

The example above is not the shadow of the For loop we often use. Let's talk about Parallel.For's third argument. action<int> type delegate regardless of whether the delegate's argument is 0 or how many of his return implants are void, how can we get the return value in Parallel.For?  The following example shows how to use a thread-local variable to store and retrieve the state of each individual task created by the for loop by using thread-local data, you can avoid the overhead of synchronizing large amounts of access to 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 final results to a shared resource at once, or pass it to another method

• To sum a list<int> we assume that the length of the list is Listlength

Copy Code code as follows:

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 situations where we need to cancel the cycle. 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. Take a look at the example below

Copy Code code as follows:

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 loops Parallel.ForEach loops work like a parallel.for loop. The source collection is partitioned according to the system environment, and the work is scheduled on multiple threads.  The more processors in the system, the faster the parallel methods run. For some source collections, sequential loops can be faster, depending on the size of the source and the type of work being performed

Copy Code 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 is very similar to the for above, I still take the above array sum as an example

Copy Code code as follows:

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 compare here we produce 10 million random numbers for example to do a performance comparison, in the author's notebook The results are as follows (may be on your computer results are not necessarily the same)

Please attach the relevant code for your reference

Copy Code 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);


Stopwatch WATCH2 = Stopwatch.startnew ();

for (int i = 0; i < listlength; i++)
{
Random r = new Random (100);
List. ADD (R.next ());
}


Console.WriteLine ("Non-concurrent 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.