How to divide a FOR loop into N even parts for parallel execution, given constant input data for each iteration?

I have a time loop that I would like to execute in parallel. Pseudocode:

for(int n = 0; n < 2048; n++) { output_data[n] = function(constant_input_data, n) } 
  • The input data for each iteration is completely the same.
  • The output for the Nth iteration is stored in an array with index N.

How to divide this cycle into C in equal parts, where C is the processor core count?

What is the best and most elegant way to do this in C # ,. net?

+4
source share
1 answer

Using Parallel.For in TPL

 Parallel.For( 0, 2048, n=> { output_data[n] = function(constant_input_data, n); }); 

TPL tries to create as many threads as not. from the processor cores that you have, and then your work is divided into tasks scheduled for these threads. So these are 2048 tasks, maybe not. threads where x is not. cores.

+7
source

Source: https://habr.com/ru/post/1393696/


All Articles