I was curious if there have been any strides in allowing us to set the number of threads for parallel jobs? One huge problem I have in my application is I cannot run large parallel jobs without stalling essential threads -- particularly the main thread -- because parallel jobs include all threads! Is this feature being worked on or is there any way around this such that I can run parallel jobs which do not stall the main thread?
#Controlling the number of threads used in parallel jobs.
1 messages · Page 1 of 1 (latest)
Hi @golden flint !
One way to ensure that your parallel jobs don't use too many threads is to use JobsUtility.ScheduleParallelFor(ref scheduleParams, arrayLength, innerloopBatchCount);
By setting the innerLoopBatchCount to a fraction of the arrayLength you can induce N chunks of work, which will be processed by N jobs max. This way you can control the maximum number of jobs that the work items are processed with.
Regarding the main thread being used as well for jobs, this only happens if the main thread waits, e.g., by calling JobHandle.Complete(). It will then try to help out the parallel job it's waiting on since it's the fastest thing to do. However, while it can't work on the job it's waiting on explicitly it could potentially pick up random work from other jobs.
Maybe that helps in your case to understand in what situation the main thread might get used and allows you to prevent this from happening in situations you would like to avoid it.
If you absolutely want to avoid the main thread from ever performing any job work stealing, you can use the boot.config option no-main-thread-job-stealing=true as a last resort.
See https://unity.com/releases/editor/whats-new/2022.2.7.
Please note though that there are some limitations regarding such boot.config options discussed here: https://forum.unity.com/threads/info-or-docs-on-boot-config.971457/
Sweet! Thank you very much. I was unaware of all these options.