iOS Swift DispatchQueue: How do I run a process using multiple CPUs
iOS Swift DispatchQueue: How do I run a process using multiple CPUs
So far I am trying to run the Grand Central Dispatch to process images on multiple cores.
The issue I am running into is that the processing only occurs on the main queue and stays on the first core.
I am using an iPhone 6 which has dual cores up to 200% cpu I can use and it caps out at 100% CPU and other threads are clearly not running the function.
CODE:
//value is an image inside an image array that I am using the .map function
//imageProcess processes an image on the device CPU.
let core1 = DispatchQueue(label: "core1", qos: .userInitiated)
let core2 = DispatchQueue(label: "core2", qos: .userInitiated)
core1.async
self.imageProcess(fromImage: value, completion: (error) in
if let error = error
print("Oops something failed!")
else
print("It has finished! Core1")
)
core2.async
self.imageProcess(fromImage: value, completion: (error) in
if let error = error
print("Oops something failed!")
else
print("It has finished! Core2")
)
What I am seeing is the DispatchQueue just drops everything into thread 11 on my CPU and all the images are processed that way. There is no multithreading and it stays on a single core.
Questions:
Thanks all!
Update
Looks like my function is not properly configured for DispatchQueue. Any suggestions?
Please note that faceDetector is calling a firebase function MLKit Firebase
private func imageProcess(fromImage image: UIImage, completion: @escaping (Error?) -> Void)
let visionImage = VisionImage(image: image)
faceDetector.detect(in: visionImage) [unowned self] (faces, error) in
guard error == nil, let detectedFaces = faces else
self.showAlert("An error occured", alertMessage: "The face detection failed.")
return
let faceStates = self.faceStates(forDetectedFaces: detectedFaces)
self.updateDetectedInfo(forFaceStates: faceStates)
completion(nil)
qos
let core1 = DispatchQueue(label: "core1", qos: .background)
Just tried it, not seeing a difference, CPU was still 100%/200%. If you put prints inside the core1.async and core2.async it'll jump through them before the self.imageProcess() completes.
– Node.it
Aug 20 at 23:53
Is
imageProcess
async itself?– rmaddy
Aug 20 at 23:54
imageProcess
The firebase function inside the imageProcessing function is synchronous.
– Node.it
Aug 21 at 1:24
There's your problem.
detect
is async so imageProcess
returns immediately. Your use of core1.async
and core2.async
is pointless at the moment since imageProcess returns immediately while
detect` runs in the background on whatever queue is it written to use.– rmaddy
Aug 21 at 2:10
detect
imageProcess
core1.async
core2.async
imageProcess returns immediately while
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Try a lower priority
qos
or create your own queue usinglet core1 = DispatchQueue(label: "core1", qos: .background)
– rmaddy
Aug 20 at 23:42