Should I start using a parallel foreach everywhere possible?

  • Context:
  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Asynchronous Parallel
Click For Summary

Discussion Overview

The discussion revolves around the use of C#.NET's Parallel.ForEach function and whether it should be employed universally in programming practices. Participants explore the conditions under which parallel processing is beneficial, the potential pitfalls, and the performance implications of using parallel loops.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants suggest that Parallel.ForEach can optimize processing time when tasks are independent and do not require a specific order of completion.
  • Others caution that parallel processing should only be used for time-consuming loops, as the overhead of managing parallel tasks can negate performance benefits for shorter loops.
  • Concerns are raised about the introduction of hard-to-detect bugs, such as race conditions, when using parallel programming, necessitating careful synchronization of code.
  • Some participants emphasize the importance of profiling to identify bottlenecks before deciding to implement parallel processing.
  • There are warnings about the potential for multi-threaded programs to behave inconsistently across different environments, highlighting the complexities involved in debugging such applications.
  • Discussion includes the importance of understanding threading concepts like mutexes, volatile variables, and thread safety of libraries, with specific examples provided regarding C++ and C#.
  • A participant shares a personal benchmark experience demonstrating that multi-threading can significantly improve performance for certain computational tasks, while also noting that not all operations benefit from parallelization.

Areas of Agreement / Disagreement

Participants generally do not reach a consensus on the universal applicability of Parallel.ForEach, with multiple competing views on its use and effectiveness depending on the context of the task.

Contextual Notes

Limitations include the need for careful consideration of task independence, potential overhead, and the risk of introducing bugs. The discussion also highlights the importance of understanding the specific characteristics of the tasks being parallelized.

Who May Find This Useful

Programmers and software developers interested in optimizing performance through parallel processing, as well as those looking to understand the complexities and risks associated with multi-threaded programming.

SlurrerOfSpeech
Messages
141
Reaction score
11
I recently learned about C#.NET's Parallel.ForEach function and it made me think that this is the new hammer I have that will make everything look like a nail. Under the covers, the parallel foreach figures out how to use the optimal number of threads (based on your machines CPUs and cores) to accomplish the tasks in the loop in the least amount of time possible. Of course, it only makes sense to use this if

  • the tasks are independent
  • there is no specific order in which the tasks need to be completed
But that's easily the case for 75%+ of the instances of when I need to write a loop.

Am I making any incorrect assumptions?
 
Technology news on Phys.org
I would only use it for loops that take a significant amount of time (in addition to the two rules you mentioned.) There is additional overhead in splitting the loop up for separate processors. If there is very little to do inside the loop, a parallel foreach can take up to 10 times as long. There is also the concern that a future software mod will create some dependencies that make you change those parallel loops. So I would only use a parallel foreach if it will solve a processing time problem in the loop.
 
The short answer is no. You should profile your program to see where the bottkenecks are consider using it to speed up blocks only.

Parallel programming done poorly can introduce so many hard to find bugs that you will rue the day when you have to fix them. Usually you get race conditions where some variable is used before it has the proper value. Basically your code needs tobe synchronized with itself to eliminate the race conditions using mutexes to control access to data.

I would recommend that you explore multi threaded programming to understand the risks involved.
 
  • Like
Likes   Reactions: harborsparrow
Beware: Your first multi-threaded program will not work or will only work on the computer you developed it on at the time of day you developed it (phase of moon and what you ate for breakfast may be important too). This is normal. Just determine the reason and try again.

BoB
 
No, you should only create threads in areas that take a while. Creating a thread takes about a quarter millisecond on a modern computer, a for each loop through a string takes way less than that. If the loop takes less than 100ms, don't bother threading.

Also be sure you understand: mutexes, volatile, and thread pooling.

Also be aware of what libraries are threadsafe and which aren't. For example, on Visual C++, std::string is NOT threadsafe by default, you have to link to a special version because it uses reference counting.
 
The other example

http://msdn.microsoft.com/en-us/library/dd460703(v=vs.110).aspx

is a better example, as file reads from even an SSD can't be done in parallel. The concept is that C# will determine the optimal number of threads depending on the code and system that the code is running on. For a simple summation, it probably won't help much, but the example is just that. If the math was more complex and cpu bound then multiple threads would help. You could always try a benchmark to see if it helps.

Locality of data within each core's local cache can also help in what would otherwise seem like a memory bandwidth limited process. I wrote a single and multi threaded (4 threads) version of bottom up (iterative) merge sort, and for 16 million 32 bit integers, the 4 thread version runs about 3 times as fast as the single thread version (0.5 seconds versus 1.5 seconds with Intel 3770k 3.5ghz cpu).

This isn't a new concept. Fortran compilers have included vectorization and multi-threaded extensions for decades.
 

Similar threads

Replies
1
Views
2K
Replies
29
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
6K
  • · Replies 61 ·
3
Replies
61
Views
13K
  • · Replies 31 ·
2
Replies
31
Views
5K
  • · Replies 6 ·
Replies
6
Views
4K