Unlimited Threads

Hello all,

I recently discovered that Geant4 does not limit the amount of threads you set with
runManager->SetNumberOfThreads(n);

If anybody knows the technical answer to what happens when you increase the thread count, I would love to know. I set my thread count to 32 on my 8-core M2 laptop and was not met with any errors or limitations.

What happens behind the scenes and why don’t users just increase their thread-counts further?

Thanks,
Shivom

Theoretically, you can have as many Posix-style threads as you like per core, but you aren’t going to win anything. With one (or sometimes two) threads per core, each thread gets 100% CPU usage.

If you request more threads than you have cores, then your OS will simply swap each thread out, back and forth, so each one gets (very roughly!) a fraction of each core’s available time. So your per-thread CPU efficiency will just get worse and worse.

With Geant4, the behaviour could be even worse. Each thread requires a non-trivial amount of per-thread memory (each thread has local copies of all processes, of all electric fields, of all SDs, hit collections, and so on). If you request too many threads, it’s conceivable that your job could exceed the maximum memory on your system. In any event, if you exceed the “resident memory” (i.e., your available RAM), then your CPU efficiency will go down as there will be more and more page swapping going on.

Ah, that’s what I was predicting. I wonder if it could be any faster to over-thread by a slight margin to reduce any extra memory per thread. Probably never gonna test it, but interesting regardless.

Thanks,
Shivom