I have a time consuming loop I would like to execute in parallel. Pseudocode:
for(int n = 0; n < 2048; n++)
{
output_data[n] = function(constant_input_data, n)
}
How to divide this loop in C equal parts, where C is CPU core count?
What is the best and the most elegant way to do this in C#, .net?
Using Parallel.For of TPL
Parallel.For( 0, 2048, n=>
{
output_data[n] = function(constant_input_data, n);
});
TPL tries to spawn as many threads as the no. of cpu cores you have and then your work is divided in tasks that are scheduled on those threads. So it is 2048 tasks on possibly x no. of threads where x is no. of cores.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With