What are Parallel.For and Parallel.ForEach loop?

10

I saw the use of Parallel.For and Parallel.ForEach in some places, I know they are loops , but I did not 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?

Are there any differences between them?

    
asked by anonymous 04.09.2017 / 14:38

3 answers

16

Imagine that you have a directory on your computer with 8 FLAC music files. Great format on purpose, clean audio.

Now imagine that you want to convert these files from FLAC to MP3 (not so good) because your cd-player (nor 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 invented now.

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

Ready, 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 why wait?

Let's rewrite code so that all processing power is enjoyed:

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

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

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

Considering that it would take 1 minute for each file, the default%% would take a total of 8 minutes, whereas a foreach would take 1 minute considering 8 files in the directory and a processor with 8 threads.

Parallel.ForEach works just as you imagine it:

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 are CPU-dependent and can be run in parallel when there is more than one occurrence of the item being processed, similarly to our list of songs.

Leave it :

Using Parallel.For in simple operations / iterations that do not use many resources is not guaranteed to be faster than a% default%. In fact, the cost of allocating threads in the way that Parallel.ForEach does can cause an overhead to let its code slow down, so the "weight" of the operation to be executed within foreach is what it will dictate if it compensates or not the use of the parallel form.

    
04.09.2017 / 15:29
7
Some processing in collections of data or some algorithm that is done as a repetition, since they do not depend on the sequence to be executed, can benefit if they are done in parallel taking advantage of the current capacity of the processors to have several independent units of processing , this is usually done through threads . However making threads is not always simple, so a parallel processing framework has been created that abstracts this for you, so instead of executing a loop normal you create an anonymous function ( lambda ) with the algorithm to be executed and transfer to the framework run in parallel as best as possible, all internet logic and care to be taken, how to consolidate the result is the responsibility of framework .

Sequences that depend on the previous result to continue execution can not be paralleled. Adding the items in a collection or finding which cousins works fine, but a 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, to be used in data collections.

See also: Foreach C # vs EFE ForEach ()

Required reading: Is it 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 resources external to the processor, it was meant to make better use of the processor and not the external devices that do the best with Asynchrony .

    
04.09.2017 / 15:14
2

Responding:

  

What are loops.

They are similar to for and for each loops, but are optimized for parallel data programming.

  

When should we use them.

It is difficult to define an optimal scenario, but always when multi-threaded execution performance is required. Parallel is used to do two things at the same time, using multiple threads.

  

Is there a difference between them?

The same between the common and the common foreach.

    
04.09.2017 / 14:51