mratsim / weave

A state-of-the-art multithreading runtime: message-passing based, fast, scalable, ultra-low overhead
Other
532 stars 22 forks source link

Feature Request: Cores as init parameter, respect core perf levels #191

Open turbo opened 1 year ago

turbo commented 1 year ago

On Apple Silicon, I'd like to be able to set the maximum count of worker threads to the number of performance cores, rather than all. Nim's built-in CPU count proc is unaware of perf levels on AS. I assume the same is the case for later Intel CPUs.

I'm using this workaround right now:

if hostOS == "macosx" and hostCPU == "arm64" and cpuEndian == Endianness.littleEndian:
  echo "Apple Silicon detected, tuning parallel runtime to use perflevel0 only"

  # sysctl hw.perflevel0.logicalcpu_max
  # hw.perflevel0.logicalcpu_max: 8 

  let outp = execProcess(
    "/usr/sbin/sysctl", 
    args = ["hw.perflevel0.logicalcpu_max"], 
    options = { poStdErrToStdOut }
  )

  let perfCores = outp.split(": ")[1].strip.parseInt
  echo &"Detected {perfCores} cores at perflevel0"

  putEnv("WEAVE_NUM_THREADS", $perfCores)

This works, and macOS scheduler seems to pin the threads to the right cores. It'd be nice for Weave to be aware of this natively, and to be able to allow one to specify cores to init directly without the round-trip through the environment, e.g.:

init(Weave, threads = 42, [perfLevel = all, performanceOnly])

No opinion on whether a potentially set ENV var should override this init parameter.