Skip to content

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

Open
@turbo

Description

@turbo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions