What are Parallel.For and Parallel.Foreach loops?

Asked

Viewed 8,582 times

20

I saw the use of Parallel.For and Parallel.ForEach in some places, I know they are loops, but I didn’t understand how and when to use them and I have my doubts.

What are loop Parallel.For and Parallel.ForEach?

When should we use them?

There’s a difference between them?

  • I think it’s already answered here: https://answall.com/q/211053/101

  • @Bigown, what I don’t understand is the use of Parellel. Your answer specifies the use of loops and even links the Parallel, but does not explain the subject or say when to use them.

3 answers

26


Imagine you have a directory on your computer with 8 music files in FLAC format. Excellent format by the way, clean audio.

Now imagine that you want to convert these files from FLAC to MP3 (not so good) as your cd-player (not even mine) recognize the FLAC format. You will create your own code to convert audio files:

First, let’s get a list of the audios using a method I just invented.

FlacAudio[] audioFiles = FlacHelper.ReadFromDirectory("caminhoDoDiretorio");

Okay, we have a list of the 8 music files.

Now let’s convert them:

List<Mp3Audio> mp3Files = new List<Mp3Audio>();

foreach (FlacAudio file in audioFiles)
{
   Mp3Audio mp3 = FlacHelper.ConvertToMp3(file);
   mp3Files.Add(mp3);
}

// Salvar os arquivos convertidos no diretório destino...

Here, in this iterator foreach only one thread is used for each of the 8 files, so while the first one is being converted, the other 7 are impatient waiting. But look, you have an Intel Core I7 processor with 8 threads available, so to wait?

We will rewrite code so that all this processing power is enjoyed:

List<Mp3Audio> mp3Files = new List<Mp3Audio>();

Parallel.ForEach(audioFiles, file =>
{
   Mp3Audio mp3 = FlacHelper.ConvertToMp3(file);
   mp3Files.Add(mp3);
});

Okay, in this code the foreach of Parallel discovers and utilizes all the available threads of your processor so that the action, which in this case is to convert audio files, runs in parallel, so each thread will convert a file at the same time.

Whereas it would take 1 minute for each file, the foreach standard would take a total of 8 minutes while a Parallel.ForEach it would take 1 minute considering 8 files in the directory and a processor with 8 threads.

The Parallel.For works exactly as you imagine:

List<Mp3Audio> mp3Files = new List<Mp3Audio>();

Parallel.For(0, files.Length - 1,
index => { 
            Mp3Audio mp3 = FlacHelper.ConvertToMp3(audioFiles[index]);
            mp3Files.Add(mp3);
         }); // Index é o número da iteração atual, que neste caso parte de zero e é incrementada a cada iteração.

When to use:

For operations that depend on processing (CPU-Bound) and can be performed in parallel when there is more than one item occurrence to be processed, similarly to our playlist.

Caveat:

Use the Parallel.ForEach in simple operations/iterations that do not use many features is no guarantee to be faster than a foreach standard. Actually the cost of allocating threads in the way that the Parallel can cause an "overhead" that slows down your code, then the "weight" of the operation to be executed within the foreach is what will dictate whether or not to compensate for the use of the parallel form.

  • The Parallel is not suitable for IO, for this asynchrony is ideal.

  • You’re right, I’m going crazy.

  • Now I saw that switched to the converter, if it is intensive in processing gets ok.

  • 1

    A doubt. What is the difference of using "Parallel.For.." and "mp3Files.AsParallel.Foreach...". You can use the Parallel before or after.

  • @Leandro The extension method "Asparallel" works as a "wrapper" around the "Ienumerable". It is part of the PLINQ (Parallel LINQ). When you call "Asparallel" in a Ienumerable, it causes the LINQ extension method called consequently to be parallel. For example: "mp3Files.Sum(t => t.Size)" is not parallel, now "mp3Files.Asparallel(). Sum(t => t.Size)" is parallel and will sum each item in parallel. This only works for LINQ methods like "Sum", "Select", 'Max", etc. "Foreach" is not a LINQ method, so it will not be parallel. Its PLINQ equivalent is the "Forall".

  • As for the "cost for thread allocation" question, does Parallel not use pool threads? 'Cause if so, I think whenever it’s possible to use it we’ll be getting performance benefits.

Show 1 more comment

8

some processing in data collections or some algorithm that is done as a repetition, provided that it is not dependent on the sequence to be executed, can benefit if they are made in parallel by taking advantage of the current capacity of the processors to have several independent processing units, in general this happens through threads.

Meanwhile make use of threads is not always simple, so a framework parallel processing that abstracts it for you, so instead of running a loop normal you create an anonymous function (lambda) with the algorithm to be executed and transfers to the framework run in parallel in the best possible way, all internal logic and care to be taken, how to consolidate the result is in charge of the framework.

Sequences that depend on the previous result to continue execution cannot be parallelized. Adding up the items in a collection or finding out which cousins work well, but progression anyway, arithmetic, Fibonacci, etc.) does not work.

The ForEach is used for data collections and the For is used for repeated processing in general, which does not prevent, but is not ideal, from being used in data collections.

See also: Foreach of C# vs Foreach() of EF6

Must-read: It’s always guaranteed that a multi-threaded application runs faster than using a single thread?. Do not try to use this mechanism to do file operations or access external resources to the processor, it was made to better utilize the processor and not the external devices that get along better with asymchronism.

  • So the use of Parellel is nothing more than a way to encapsulate what we would do through multiple threads at once.

  • 2

    Basically this.

2

Responding:

What are the loops.

Similar to loops for and for each, but optimized for parallel data programming.

When should we use them.

It is difficult to define an ideal scenario, but always when it is necessary to achieve execution performance with multiple threads. Parallel is used to be able to do two things at the same time, using several threads.

There’s a difference between them?

The same between the common and the common foreach.

  • So there’s no difference between a Loop For or a Parallel.For Loop ?

  • The difference between them is that one will run sequentially, and the other in parallel. In the second you have to control the running threads tbm, because an item in front of the sequence can end before the previous one. Unless you need very, very fast processing, use Parallel.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.