1

At one point, my iOS app does some CPU-bound processing that lasts for about 2 minutes. That processing is highly "parallelizable" and I refactored it into a bunch of independent work units. My processing loop now looks like this:

while ([inputSource hasDataToProcess]) {
    size_t batchSize = [inputSource prepareNextBatch];
    [collectorArray enumerateObjectsWithOptions:NSEnumerationConcurrent
                                     usingBlock:^(BatchItemCollector *collector, NSUInteger idx, BOOL *stop) {
         [collector processBatch:[inputSource currentBatchElement: idx]];
     }];
}

So the outer loop is sequential, and the inner loop is parallel thanks to the enumerateObjectsWithOptions:usingBlock: method on NSArray. collectorArray contains a number of unit workers that process one element in the batch.

My problem is that when I use the NSEnumerationConcurrent option, my code doesn't run any faster on an iPad 2 than without. My understanding is that this option would take advantage of however many cores were available. Since my processing is CPU-bound and the iPad has two cores, I expected a two-fold speed up, give or take.

Now, three questions:

1- Am I correct to believe that NSEnumerationConcurrent will make enumerateObjectsWithOptions:usingBlock: truly parallel on multicore machines? Even on iOS? The official documentation is not explicit on this.

2- More importantly, how can I check that? Instruments should be the right tool, but I couldn't manage to get convincing data out of it. I tried the Time Profiler and Activity Monitor template. The Instruments documentation mentions a "Multicore" template, but says it's for Mac code, not iOS code. Since I'm quite a newbie with Instruments, I guess I missed something.

3- Any suggestion, perhaps using another construct than enumerateObjectsWithOptions:usingBlock:? There are many possible others, but few that convenient to apply.

Many thanks.

6
  • Your assumption #1 ... as I understand things the answer would be "No." It does not marshal the invocations to available cores on separate threads the way .NET or Java supports. On #2, short of having the proper instrumentation support, you should see a dramatic improvement in processing times (with 2 cores a 50% reduction in times). Apr 9, 2011 at 15:48
  • Where does your understanding come from? I believe this assumption is sound for two reasons: 1- without this assumption, enumerateObjectsWithOptions:usingBlock: 's NSEnumerationConcurrent option would bring no functional value whatsoever. - the definition of concurrent. I realize that this might be subject to interpretation (nitpicking?). So let me quote Apple on this: "Concurrency is a way to achieve higher performance on multi-core machines". You can find this quote in WWDC 2010 session #138 video at 26:20. It's even written on the slide. Apr 9, 2011 at 19:04
  • On #2 is your point that Instruments doesn't provide "proper instrumentation support"? Why? Apr 9, 2011 at 19:05
  • A couple of my engineers several months ago tried the same thing on the iPhones ... same results. They spoke with someone at Apple (I wasn't on the call) and was informed "not yet, but maybe in the future" as far as mobile support for multiple cores. Frankly, I hope what we heard is incorrect and you find a way to do this ... I'd love to be able to have parallel processing on iOS too. Apr 9, 2011 at 23:28
  • Well the iPhone has only one core. On the iPad2, the inner loop definitely happens asynchronously. I also rewrote it using GCD, and now I got a speed up. So parallel processing definitely is possible on iOS Apr 10, 2011 at 1:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.