https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

Marc Plano-Lesay

07/14/2022, 1:53 AM
I have an algorithm that's very easy to run in parallel (think finding a best path), extremely CPU intensive, and would actually benefit from running only n branches at once for n CPU threads (once a solution is found, branches can be culled eagerly, so exploring a solution fully quicker is better than exploring more than n at once). Is there a dispatcher that would block new coroutines until some are finished, in such a way that only n would run for n CPU threads? Or is this too high level for dispatchers and I have to write my own orchestration logic?
m

Marc Plano-Lesay

07/14/2022, 1:57 AM
Ha nice, didn't find that one somehow - thanks!
n

Nick Allen

07/14/2022, 2:51 AM
Dispatchers.Default
is max(cpuCount, 2)
m

Marc Plano-Lesay

07/14/2022, 2:54 AM
It's surprising, I get noticably different results by limiting
Dispatchers.Default
to
Runtime.getRuntime().availableProcessors()
r

rnett

07/14/2022, 3:08 AM
I'm guessing because using
availableProcessors()
ignores hyperthreading
m

Marc Plano-Lesay

07/14/2022, 3:12 AM
It returns the number of logical processors, so in my case 8 (4*2), which is what I expected, and from my observations
Dispatchers.Default
was able to leverage all the logical processors as well
8 Views