Should I start using a parallel foreach everywhere possible?

In summary: C++11 added threading support to the language, and with the release of Visual C++ 2013, threading is now the default for all new projects.
  • #1
SlurrerOfSpeech
141
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
  • #2
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.
 
  • #3
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 harborsparrow
  • #4
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
 
  • #5
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.
 
  • #6
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.
 

Related to Should I start using a parallel foreach everywhere possible?

1. What is a parallel foreach loop?

A parallel foreach loop is a type of loop in computer programming that allows for multiple iterations to be performed simultaneously on different processors or cores. This can significantly improve the speed and efficiency of a program compared to a traditional sequential foreach loop.

2. When should I use a parallel foreach loop?

A parallel foreach loop should be used when you have a large amount of data to process and you want to speed up the execution time of your program. It is particularly useful when the iterations of the loop do not depend on each other, as this allows for true parallel processing.

3. Are there any downsides to using a parallel foreach loop?

Yes, there are a few downsides to using a parallel foreach loop. One potential issue is that it may not always result in a faster execution time, especially if the iterations of the loop are too small or if there is a lot of overhead involved in managing the parallel processes. Additionally, parallel processing can be more complex and may require more resources, so it may not be the best choice for all situations.

4. How do I know if using a parallel foreach loop is the right choice for my program?

The best way to determine if using a parallel foreach loop is the right choice for your program is to test it out and compare the execution time to a traditional sequential foreach loop. If the data set is large and the iterations are independent, there is a good chance that the parallel loop will result in a faster execution time. However, it is important to consider the potential downsides and weigh them against the potential benefits before making a decision.

5. Are there any best practices for using parallel foreach loops?

Yes, there are a few best practices to keep in mind when using parallel foreach loops. It is important to properly manage resources and avoid creating too many parallel processes, as this can actually slow down the execution time. It is also a good idea to use a thread-safe data structure and to consider the order in which the iterations will be processed, as this can impact the results of the loop.

Similar threads

  • Programming and Computer Science
Replies
29
Views
3K
  • STEM Academic Advising
Replies
5
Views
866
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
Replies
5
Views
957
Replies
2
Views
634
Replies
4
Views
2K
  • Sci-Fi Writing and World Building
Replies
31
Views
2K
Replies
6
Views
2K
Replies
7
Views
5K
Back
Top