As you may remember, I am trying to use GCD to speed up some of my code, namely a collision detection and resolution engine. However, I am clearly doing something wrong because all of my GCD code is significantly slower and less consistent than my serial code (between 1.4x and 10x slower). Allow me to give you an example: I am iterating over an array in a bubble-sort fashion to determine all possible collisions among objects in that array:
- (double) detectCollisionsInArray:(NSArray*)objects
{
int count = [objects count];
if (count > 0)
{
double time = CFAbsoluteTimeGetCurrent();
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
/** LOTS AND LOTS OF WORK FOR EACH OBJECT **/
}
}
return CFAbsoluteTimeGetCurrent() - time;
}
return 0;
}
Pretty straightforward, and it seems to perform well given the constraints of the problem. However, I would like to take advantage of the fact that the state of each object is not modified in the code section and use GCD to parallelize this work. To do this I am trying something like this:
- (double) detectCollisionsInArray:(NSArray*)objects
{
int count = [objects count];
if (count > 0)
{
NSOperationQueue* opQueue = [[NSOperationQueue alloc] init];
NSBlockOperation* blockOperation = nil;
double time = CFAbsoluteTimeGetCurrent();
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
void (^workBlock) (void) = ^()
{
/** LOTS AND LOTS OF WORK FOR EACH OBJECT **/
};
if (!blockOperation)
{
blockOperation = [NSBlockOperation blockOperationWithBlock:b];
}
else
{
[blockOperation addExecutionBlock:workBlock];
}
}
}
[opQueue addOperation:blockOperation];
[opQueue autorelease];
return CFAbsoluteTimeGetCurrent() - time;
}
return 0;
}
Can anyone help to put me on the right track and perhaps provide a link to a good GCD tutorial? I have looked over several GCD tutorials and scoured all of the documentation and I still feel that my grasp on the subject is tenuous at best. Thanks!
Is there a reason you’re not using the GCD C API and the
dispatch_*family of functions? You don’t have much control over the GCD aspects ofNSOperationQueue(like which queue you want to submit the blocks to). Also, I can’t tell if you’re using iOS or not, butNSOperationQueuedoes not use GCD on iOS. That might be the reason it spawned so many threads. Either way, your code will be shorter and simpler if you use the GCD API directly:You can use a
dispatch_groupto group all of the executions together and wait for them all to finish withdispatch_group_wait. If you don’t care to know when the the blocks finish, you can ignore the group part and just usedispatch_async. Thedispatch_get_global_queuefunction will get one of the 3 concurrent queues (low, default or high priority) for you to submit your blocks to. You shouldn’t have to worry about limiting the thread count or anything like that. The GCD scheduler is supposed to do all of that for you. Just make sure you submit to a concurrent queue, which could either be one of the 3 global queues, or a queue you’ve created by passingDISPATCH_QUEUE_CONCURRENTtodispatch_queue_create(this is available starting OS X 10.7 and iOS 5.0).If you’re doing some file I/O in each block or taxing some other resource, you might need to reign in GCD and limit the number of blocks you’re submitting to the queue at once. This will have the same effect as limiting the concurrent operation count in an
NSOperationQueue. You can use a GCD semaphore to do this:Once you get the hang of it, GCD is very simple to use. I use it all over my code now.
Run, don’t walk over to Mike Ash’s blog. His series on GCD is the clearest and most concise I’ve seen, and it’ll only take you around 30 minutes to read the whole thing. Apple’s WWDC videos from 2010 on GCD And blocks are also pretty good.