Sei sulla pagina 1di 2

search

How do you get thread count in Nim


 Moderated

jzakiya Oct 2017

How do you get the number of available system threads inside a Nim proggram?
    Reply

dom96  Oct 2017

You mean the number of available cores? https://nim‐lang.org/docs/osproc.html#countProcessors,


  Reply

jzakiya Oct 2017

Yes, that returns the number of available threads, though its doc says processors/cores.
    Reply

Araq  Oct 2017

The docs are correct, there is no such thing as the "number of available threads", threads are dynamic in nature, on some
OSes you can create thousands of them.
  Reply

Tiberium Oct 2017

TS probably means "processor threads" ‐ Hyper Threading


  Reply

jzakiya Oct 2017

Yes, that is what I meant, the threads that are available per system ﴾all cpu cores﴿, which you can see using commands
such as lscpu or htop .
    Reply

JohnS Nov 2017

For Linux systems, this is how I do it in the psutil library:

Nim
proc cpu_count_logical*(): int =
## Return the number of logical CPUs in the system.
try:
return sysconf( SC_NPROCESSORS_ONLN )
except ValueError:
# as a second fallback we try to parse /proc/cpuinfo
for line in lines(PROCFS_PATH / "cpuinfo"):
if line.toLowerAscii().startswith("processor"):
result += 1

# unknown format (e.g. amrel/sparc architectures), see:


# https://github.com/giampaolo/psutil/issues/200
# try to parse /proc/stat as a last resort
if result == 0:
for line in lines(PROCFS_PATH / "stat"):
if line.toLowerAscii().startswith("cpu"):
result += 1
# Remove one from the count for the top "cpu" line (with no digit)
# Saves us the regular expression used in the python impl
if result != 0: result -= 1

return result

proc cpu_count_physical*(): int =


## Return the number of physical cores in the system.
var mapping = initTable[int, int]()
var current_info = initTable[string, int]()
for raw_line in lines(PROCFS_PATH / "cpuinfo"):
let line = raw_line.strip().toLowerAscii()
if line == "":
# new section
if "physical id" in current_info and "cpu cores" in current_info:
mapping[current_info["physical id"]] = current_info["cpu cores"]
current_info = initTable[string, int]()
else:
# ongoing section
if line.startswith("physical id") or line.startswith("cpu cores"):
let parts = line.split("\t:")
current_info[parts[0].strip()] = parseInt(parts[1].strip())

let values = toSeq(mapping.values())


return sum(values)

I haven't implemented the windows side yet, but take a look here for similar C code.

  Reply

8 MONTHS SINCE LAST REPLY

 Reply

Potrebbero piacerti anche