nim-taskpools/benchmarks/fibonacci/taskpool_fib.nim

80 lines
2.1 KiB
Nim
Raw Normal View History

import
# STD lib
os, strutils, cpuinfo, strformat, math,
# Library
../../taskpools
when not defined(windows):
# bench
import ../wtime, ../resources
var tp: Taskpool
proc fib(n: int): int =
# int64 on x86-64
if n < 2:
return n
let x = tp.spawn fib(n-1)
let y = fib(n-2)
result = sync(x) + y
proc main() =
var n = 40
var nthreads: int
if paramCount() == 0:
let exeName = getAppFilename().extractFilename()
echo &"Usage: {exeName} <n-th fibonacci number requested:{n}> "
echo &"Running with default n = {n}"
elif paramCount() == 1:
n = paramStr(1).parseInt
else:
let exeName = getAppFilename().extractFilename()
echo &"Usage: {exeName} <n-th fibonacci number requested:{n}>"
quit 1
2022-01-16 17:33:43 +00:00
if existsEnv"TP_NUM_THREADS":
nthreads = getEnv"TP_NUM_THREADS".parseInt()
else:
nthreads = countProcessors()
tp = Taskpool.new()
# measure overhead during tasking
when not defined(windows):
var ru: Rusage
getrusage(RusageSelf, ru)
var
rss = ru.ru_maxrss
flt = ru.ru_minflt
let start = wtime_msec()
let f = fib(n)
when not defined(windows):
let stop = wtime_msec()
tp.shutdown()
when not defined(windows):
getrusage(RusageSelf, ru)
rss = ru.ru_maxrss - rss
flt = ru.ru_minflt - flt
echo "--------------------------------------------------------------------------"
echo "Scheduler: Taskpool"
echo "Benchmark: Fibonacci"
echo "Threads: ", nthreads
when not defined(windows):
echo "Time(ms) ", round(stop - start, 3)
echo "Max RSS (KB): ", ru.ru_maxrss
echo "Runtime RSS (KB): ", rss
echo "# of page faults: ", flt
echo "--------------------------------------------------------------------------"
echo "n requested: ", n
echo "result: ", f
main()