How to Detect a Dual Core CPU on iOS

How do I detect a dual core CPU on iOS?

Method 1

[[NSProcessInfo processInfo] activeProcessorCount];

NSProcessInfo also has a processorCount property. Learn the difference here.

Method 2

#include <mach/mach_host.h>

unsigned int countCores()
{
host_basic_info_data_t hostInfo;
mach_msg_type_number_t infoCount;

infoCount = HOST_BASIC_INFO_COUNT;
host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount ) ;

return (unsigned int)(hostInfo.max_cpus);
}

Method 3

#include <sys/sysctl.h>

unsigned int countCores()
{
size_t len;
unsigned int ncpu;

len = sizeof(ncpu);
sysctlbyname ("hw.ncpu",&ncpu,&len,NULL,0);

return ncpu;
}

Detect CPU cores on iOS device

Here's the code to detect the number of cores on an iOS device:

#include <sys/sysctl.h>

unsigned int countCores()
{
size_t len;
unsigned int ncpu;

len = sizeof(ncpu);
sysctlbyname ("hw.ncpu",&ncpu,&len,NULL,0);

return ncpu;
}

In addition to that, you can check against the [[UIDevice currentDevice] userInterfaceIdiom] to determine if the device is an iPhone or an iPad. Like this:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSLog(@"iPad");
}
else {
NSLog(@"iPhone");
}

Reference

Detect CPU cores on iOS device

Here's the code to detect the number of cores on an iOS device:

#include <sys/sysctl.h>

unsigned int countCores()
{
size_t len;
unsigned int ncpu;

len = sizeof(ncpu);
sysctlbyname ("hw.ncpu",&ncpu,&len,NULL,0);

return ncpu;
}

In addition to that, you can check against the [[UIDevice currentDevice] userInterfaceIdiom] to determine if the device is an iPhone or an iPad. Like this:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSLog(@"iPad");
}
else {
NSLog(@"iPhone");
}

Reference

How to utilize multiple processor cores in iOS to achieve the fastest/shortest computation of a loop accessing shared data?

I solved the problem, thanks to the community.
This answer encompasses the various results brought forth by the comment section.

There are two ways, one is to use pointers, which is the more general approach. The other is more specific to my problem and makes use of the GPU to see if a multiple of points is within a predefined polygon. Either way, here are both ways as code speaks more than words ;).

Make use of pointers (Note: The basic "contains/containsWithPointer" function can be found in the question):

private static func contains_multiThread(coordinates: [CLLocationCoordinate2D], with pointList: [CLLocationCoordinate2D]) -> Bool {
let numOfCoordinates = coordinates.count
var booleanArray = Array(repeating: true, count: numOfCoordinates)
let coordinatePointer: UnsafeBufferPointer<CLLocationCoordinate2D> = {
return coordinates.withUnsafeBufferPointer { pointer -> UnsafeBufferPointer<CLLocationCoordinate2D> in
return pointer
}
}()
let pointListPointer: UnsafeBufferPointer<CLLocationCoordinate2D> = {
return pointList.withUnsafeBufferPointer { pointer -> UnsafeBufferPointer<CLLocationCoordinate2D> in
return pointer
}
}()
let booleanPointer: UnsafeMutableBufferPointer<Bool> = {
return booleanArray.withUnsafeMutableBufferPointer { pointer -> UnsafeMutableBufferPointer<Bool> in
return pointer
}
}()

DispatchQueue.concurrentPerform(iterations: numOfCoordinates) { (index) in
if !containsWithPointer(coordinate: coordinatePointer[index], with: pointListPointer) {
booleanPointer[index] = false
}
}

return !booleanArray.contains(false)
}

Make use of GPU:

private static func contains_gpu(coordinates: [CLLocationCoordinate2D], with pointList: [CLLocationCoordinate2D]) -> Bool {
let regionPoints = pointList.compactMap {CGPoint(x: $0.latitude, y: $0.longitude)}
let trackPoints = coordinates.compactMap {CGPoint(x: $0.latitude, y: $0.longitude)}

let path = CGMutablePath()
path.addLines(between: regionPoints)
path.closeSubpath()

var flag = true
for point in trackPoints {
if !path.contains(point) {
flag = false
}
}

return flag
}

Which function is faster depends on the system, count of points and complexity of the polygon. My results are that the multithread variant is roughly 30% faster but when the polygon is fairly simple or the point count goes into the millions the gap closes and eventually the gpu variant becomes faster. Who knows, you might get even better results combining the two for this specific problem.

Which one is the performance cores in instruments?

GCD doesn't know anything about different kind of cores and GCD also doesn't decide which code runs on which core.

GCD decides which queue gets a thread of which thread pool and which code is scheduled to run next on the thread of the queue.

Deciding when a thread will run and on which core it will run is done by the thread schedular of the kernel. And the kernel also decides how many threads are available in which GCD thread pool.

The main thread is just a thread like any other thread. How much CPU time a thread gets depends on its own priority level, the amount of other threads, their priority levels, and the amount of workload scheduled for each of them.

As the A11 allows all 6 cores to be active at the same time, the kernel will decide which thread gets a high performance core and which one just a low performance one. High priority threads and threads with high computation workload (those that want to run very often and usually use up their full runtime quantum when running) are preferred for high performance cores. Low priority threads and threads with little computation workload (those that want to run infrequently and very often yield/block although their runtime quantum hasn't been used up yet) are preferred for low performance cores. Though, in theory every thread can run on any core as it would be stupid to leave cores unused if threads are waiting to run, yet low power cores are generally preferred as that reduces power consumption and increases battery runtime.

Tracking app's CPU usage on iOS

The different columns are documented here. Basically, the Total activity is the average CPU usage during the given period, and the Foreground activity is the average CPU usage when your app was scheduled by the kernel.

100% CPU usage means that you are using one CPU core 100% (or, unlikely, two cores 50%). This means that your app is running at maximum speed on the hardware and you may benefit from parallelizing your code to run on multiple cores (which may also save battery, depending on hardware).

Xcode shows up to 400% CPU Usage - but iPhone is only 2-core

If you are testing in a simulator then it shows reports on the basis of your MAC's processor that's why it is showing 400% ( for a quad-core processor).



Related Topics



Leave a reply



Submit