- Home /
allocate specific cpu-cores for Unity
Hi
Is it possible to allow Unity to run only on specific cpu-cores? (maybe with a 32bit-int Bitmask)
I need that to run some heavy calculations without affect the game itself.
Thanks
This is typically no recommended as the OS (i.e. windows) scheduler is better at judging which processors to run processes and threads on. If you bind affinity to only one CPU, or exclude one, you could end up with worse performance.
I would first ask if there was a reason to do this - are you seeing a performance problems? Can you be sure it's because your calculations are starving Unity from running efficiently?
Otherwise you could write a c plugin to set the thread affinity.
If you want to debug on windows, then you could get SysInternals process explorer which allows you to see and set the process affinity of running apps so that you can see what the effect is.
Hi
The reason i ask is the following: I am working on my own AI. The complete AI is running in a separate thread but in the same process ( Unity + DLL). The advantage is, that i can run the thread on a specific core without to affect the 3D-Engine. You can see it as a kind of two main-loops. It worked very good in Torque.
This way, i can run the Engine (for example on a QuadCore) on the first two cores and the AI on cores three and four. The AI is self-balancing.
Please convert this to a comment.
You could do this but I don't know as there is any evidence of it being better. $$anonymous$$y understanding of thread affinity, is that the only benefit is to ensure that you're not thrashing the cache - that your threads are not likely to access main memory as often.
As I said before, the scheduler on a PC at least, is better at deciding which CPU to use. The argument against setting thread affinity is that what you are actually doing is preventing the scheduler from running your code on particular CPUs, which means that if a CPU is available for work but you cannot run on it, then your thread won't get any CPU time.
Rather than threads, you could always use coroutines - but you are of course sharing the CPU with your game loop.
Hi
Thank you very much for the explantation. I have to see how my AI affects the Unity-Engine over the time. The main goal is, that my AI does not directly slow down the main loop of the 3D-Engine.