I have a custom UIView which is for a GameOfLife exercise .. my board size is large enough that processing it limits "framerate". (currently this begins to kick in below the 60fps of the screen at about 100*100 in my simulator)
I am already keeping a set of only the cells that I need to calculate in each step.
But now I'd like to divide that set up into parts and thread each part. How can I do this ?
Things that I need to know:
- A starter is: How can I divide the Set into n parts?
- How do I determine how many threads I benefit from?
- Can I use
DispatchQueue.global(qos: .userInitiated)
or is there some pool feature I should be using?
1 Answers
Answers 1
You can increase the frame rate and buffer performance by utilizing the underlying GPU architecture built into iOS, SceneKit & Metal rather than just rely on the CPU.
It's difficult to answer this rather broad question with the limited information provided, however I can push you in the right direction... It seems to me that you are using Single-Thread Rendering forcing the execution to be Serialized on the CPU. The underlying CPU and GPU architecture can work together to automatically run threads in parallel while still being thread safe and synchronous but learning curve to reach this point is quite high, but don't be discouraged because simple changes can greatly improve your code's performance.
"I'd like to divide that set up into parts and thread each part. How can I do this ?"
It would be advantageous to learn about Metal and the Metal Command Queue or MTLCommandQueue, utilizing the gpu and cpu together performance can be dramatically improved. There are many great tutorials on the internet that can explain it better and in more detail than I can here. In short, you can use command buffers to encode an entire scene or divide work out among threads automatically.
"how do I determine how many threads I benefit from ?"
Metal can 'automatically' control the number of threads needed to run a command, so allocating and deallocating threads 'manually' is not needed. Depending on the load and execution metal and the CPU work together to divide work out to as many threads as needed to complete the work on time.
"can I use DispatchQueue.global(qos: .userInitiated) ?"
Grand Central Dispatch is not typically used for graphics, in general GCD is used off of the main CPU thread to run concurrent code on multicore hardware unrelated to graphics. GCD doesn't utilize the GPU and Metal to run code unless it explicitly calls that code. Again There are plenty of tutorials available for GCD, but these tutorials may not improve the performance of your application as dramatically as metal command queues would. Without the code in question it would be difficult to analyze performance issues or recommend an effective solution.
There are plenty of Game of Life example on GitHub written in swift. I'd recommend you review theses and see how others are doing it, learn by example as they say.
Watch the WWDC Metal for Game Developers video from apple, this will give you a general insight on how metal performs, but without a stable underlying understanding of it you may not be able to implement its features right away.
There is a good ray wenderlich metal tutorial that can get you started using Metal and SceneKit effectively.
0 comments:
Post a Comment