Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the data parallelism of .net parallel computing

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand the data parallelism of .net parallel computing". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "how to understand the data parallelism of .net parallel computing".

Since the advent of the first computer, computer hardware technology has made great progress. Whether it's the PC used by individuals or the servers used by companies. Dual-core, four-core, and eight-core CPU are already very common. In this way, we can distribute our program to multiple computer CPU to calculate. In the past, parallelization required low-level operation of threads, which was very difficult. The enhanced support for parallelization in .net4.0 makes it very simple. This time, I will talk about the following .NET parallelism from the following aspects.

1. Data parallelism

two。 Task parallelism

3. Parallel Linq

4. Mission factory

5. Matters needing attention

This time, let's mainly talk about data parallelism. Let's start.

Data parallelism actually means that the data in the original set or array is divided into multiple CPU or multiple threads to perform the same operation. Net System.Threading.Tasks provides support for data parallelism, Parallel.For,Parallel.ForEach and we often use for and foreach are very similar, you do not have to create thread queues, you do not need to use locks in the basic loop. Net will help you with this, you just need to focus on your own business, so let's take a look at how Parallel.For and Parallel.ForEach are used.

Parallel.For is easy to use

The copy code is as follows:

Parallel.For (0,100, I = > {

Dosameting ()

});

The above example is not a shadow of the for loop that we often use. Talking about the third parameter of Parallel.For, a delegate of type Action, regardless of whether the delegate's parameters are 0 or how many, its return plant is void, so how to get the return value in Parallel.For? the following example shows how to use thread-local variables to store and retrieve state in 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. Instead of writing to shared resources on each iteration, you will calculate and store values before all iterations of the task are completed. You can then write the final result to the shared resource at once, or pass it to another method

To sum a list, we assume that the length of the List is listLength.

The copy code is as follows:

Parallel.For (0, listLength, () = > 0, (j, loop, subsum) = >

{

Subsum + = lista [j]

Return subsum

}, (x) = > Interlocked.Add (ref sum, x))

In reality, we often encounter situations that need to cancel the loop. For example, you look up a number in the queue. So how to exit the Parallel.For loop. Is it possible to use the Break keyword like for and foreach? the answer is no. This is because break constructs are valid for loops, while parallel loops are actually a method, not loops, so how to cancel them. Take a look at the following example

The copy code is as follows:

Parallel.For (0, listLength, () = > 0, (j, loop, subsum) = >

{

If (subsum > 20000)

{

Loop.Break ()

}

Subsum + = lista [j]

Return subsum

}, (x) = > Interlocked.Add (ref sum, x))

A simple Parallel.ForEach loop Parallel.ForEach loop works like a Parallel.For loop that partitions the source collection according to the system environment and plans work on multiple threads. The more processors in the system, the faster the parallel method runs. For some source collections, sequential loops may be faster, depending on the size of the source and the type of work being performed

The copy code is as follows:

Parallel.ForEach (lista, I = > {dosameting ();})

I don't know if you have seen foreach in this place. In fact, the last input parameter to the ForEach method in the above example is the Action delegate, which the method will call when all the loops are complete. This place is the same as the previous Parallel.For. So how do we get the return value is very similar to the For above, I still take the summation of the above array as an example

The copy code is as follows:

Parallel.ForEach (lista, () = > 0, (j, loop, subsum) = >

{

If (subsum > 20000)

{

Loop.Break ()

}

Subsum + = lista [j]

Return subsum

}, (x) = > Interlocked.Add (ref sum, x))

Comparison of Parallel.For and for performance tests We generate 10 million random numbers here as an example to make a performance comparison. The results on the author's notebook are as follows (the results may not be the same on your computer).

Attach the relevant code for your reference.

The copy code is as follows:

Int listLength = 10000000

List listTask = new List ()

List list = new List ()

Stopwatch watch2 = Stopwatch.StartNew ()

Parallel.For (0, listLength, I = > {

Random r = new Random

ListTask.Add (r.Next ())

});

Console.WriteLine ("parallel time consuming:" + watch2.ElapsedMilliseconds)

Stopwatch watch3 = Stopwatch.StartNew ()

For (int I = 0; I < listLength; iTunes +)

{

Random r = new Random

List.Add (r.Next ())

}

Console.WriteLine ("non-parallel time consuming:" + watch3.ElapsedMilliseconds)

Thank you for your reading. the above is the content of "how to understand the data parallelism of .net parallel computing". After the study of this article, I believe you have a deeper understanding of how to understand the data parallelism of .net parallel computing. The specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report