MCPcopy Index your code
hub / github.com/python/cpython / determine_num_threads_and_affinity

Function determine_num_threads_and_affinity

Tools/ftscalingbench/ftscalingbench.py:338–368  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

336 print(f"{color}{func.__name__:<25} {round(factor, 1):>4}x {direction}{reset_color}")
337
338def determine_num_threads_and_affinity():
339 if sys.platform != "linux":
340 return [None] * os.cpu_count()
341
342 # Try to use `lscpu -p` on Linux
343 import subprocess
344 try:
345 output = subprocess.check_output(["lscpu", "-p=cpu,node,core,MAXMHZ"],
346 text=True, env={"LC_NUMERIC": "C"})
347 except (FileNotFoundError, subprocess.CalledProcessError):
348 return [None] * os.cpu_count()
349
350 table = []
351 for line in output.splitlines():
352 if line.startswith("#"):
353 continue
354 cpu, node, core, maxhz = line.split(",")
355 if maxhz == "":
356 maxhz = "0"
357 table.append((int(cpu), int(node), int(core), float(maxhz)))
358
359 cpus = []
360 cores = set()
361 max_mhz_all = max(row[3] for row in table)
362 for cpu, node, core, maxmhz in table:
363 # Choose only CPUs on the same node, unique cores, and try to avoid
364 # "efficiency" cores.
365 if node == 0 and core not in cores and maxmhz == max_mhz_all:
366 cpus.append(cpu)
367 cores.add(core)
368 return cpus
369
370
371def thread_run(cpu, in_queue, out_queue):

Callers 1

initialize_threadsFunction · 0.85

Calls 8

setFunction · 0.85
cpu_countMethod · 0.80
check_outputMethod · 0.45
splitlinesMethod · 0.45
startswithMethod · 0.45
splitMethod · 0.45
appendMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…