Re: Does GCD auto-limit concurrent tasks to number of cores?
Re: Does GCD auto-limit concurrent tasks to number of cores?
- Subject: Re: Does GCD auto-limit concurrent tasks to number of cores?
- From: Ken Thomases <email@hidden>
- Date: Thu, 09 Apr 2015 14:40:16 -0500
On Apr 9, 2015, at 2:23 PM, Clark Smith Cox III <email@hidden> wrote:
> On Apr 9, 2015, at 12:07, Carl Hoefs <email@hidden> wrote:
>>
>> I have a highly CPU-bound project that parallelizes well. A small case has
>> about 5000 tasks that can execute concurrently (max is ~100,000). I would
>> like to use GCD queues to take advantage of the 24 cores I have on my
>> MacPro. Does GCD automatically limit the number of tasks that become
>> current on its queues to the number of cores (hw.logicalcpu)?
>
> Yes.
Well, it's not as simple as limiting to the number of cores. It will continue to spawn worker threads so long as there is spare overall system capacity (up to some internal limit, but that's not the number of cores).
If there are other processes/threads doing work on your system, your process will use fewer threads than cores.
If some of your tasks are blocked (due to I/O or locks or even sleeps), then you may have more tasks in flight than there are cores, because the blocked tasks don't consume CPU capacity. This can actually be a problem if you have tasks which are mixed I/O and computation, because GCD will over-subscribe the CPU. When the tasks unblock, there are more tasks wanting CPU than there are cores.
As much as possible, use dispatch I/O or dispatch sources for I/O. Keep tasks segregated to doing either I/O or computation, not a mix. If you can't use dispatch I/O or sources, then try to self-limit the number of I/O tasks in flight at once (for example by submitting them to a serial queue or low-concurrency-limit NSOperationQueue per device). Once you do that, you should be safe to submit large numbers of CPU tasks and let GCD manage them.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden