Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a general rule for a maximum amount of parallel tasks?

I am using parallel tasks for the first time instead of using a traditional threadpool. In my application I allow for the user to input the number of tasks started to complete the job. (jobs can be very big). I noticed that if I allow any more than 10 or so tasks, the application starts to hang and I actually get worse performance due to the resources used.

I am wondering if there is any correlation between amount of processors and max amount of tasks, so that I can limit the maximum amount of tasks for the users pc so it doesn't slow it down.

like image 268
user1632018 Avatar asked Apr 25 '14 18:04

user1632018


2 Answers

No, mostly becuase there is no definition of task. A task can be CPU intensive (limit is like Cores * factor), IO intensive (limit can be very low), or network intensivbe oto a limited ressource (which does not like to handle 1000 requests at the same time).

So, it is up for you as a programmer to use your brain and come up with a concept, then validate it and then put it into your program, depending on what the task actually IS and where the bottlenecks are foreseen. Such planning can be complex - very complex - but most of the time it is quite simple.

like image 54
TomTom Avatar answered Oct 26 '22 18:10

TomTom


The TPL will automatically change how tasks are scheduled and add or remove ThreadPool threads over time. This means that, given enough time and similar work, the default behavior should improve to be the best option.

By default, it will start by using more threads than cores, since many tasks are not "pure CPU". Given that you're seeing extra tasks causing a slowdown, you likely either have resource contention (via locking), or your tasks are CPU bound, and having more tasks than processor cores will cause slowdowns. If this is going to be problematic, you can make a custom TaskScheduler that limits the number of tasks allowed at once, such as the LimitedConcurrencyTaskScheduler. This allows you to limit the number of tasks to the number of processors in pure CPU scenarios.

If your tasks are bound by other factors, such as IO, then you may need to profile to determine the best balance between # of concurrently scheduled tasks and throughput, though this will be system specific.

like image 43
Reed Copsey Avatar answered Oct 26 '22 18:10

Reed Copsey