diff --git a/apatheia.nimble b/apatheia.nimble index b318d51..e1826e9 100644 --- a/apatheia.nimble +++ b/apatheia.nimble @@ -11,3 +11,5 @@ srcDir = "src" requires "chronos >= 4.0.0" requires "threading" requires "taskpool" +requires "taskpools" + diff --git a/tests/config.nims b/tests/config.nims index 80091ff..a418ce0 100644 --- a/tests/config.nims +++ b/tests/config.nims @@ -1 +1,3 @@ switch("path", "$projectDir/../src") + +--threads:on diff --git a/tests/tpools.nim b/tests/tpools.nim new file mode 100644 index 0000000..4c84704 --- /dev/null +++ b/tests/tpools.nim @@ -0,0 +1,30 @@ + +import std/[strutils, math, cpuinfo] + +import taskpools + +# From https://github.com/nim-lang/Nim/blob/v1.6.2/tests/parallel/tpi.nim +# Leibniz Formula https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 +proc term(k: openArray[float]): float = + for n in k: + result += n + +proc piApprox(tp: Taskpool, n: int): float = + var args = newSeq[float]() + for i in 0..n: + args[i] = i.toFloat() + result = tp.spawn term(args) # Schedule a task on the threadpool a return a handle to retrieve the result. + +proc main() = + var n = 10_000 + var nthreads = countProcessors() + + var tp = Taskpool.new(num_threads = nthreads) # Default to the number of hardware threads. + + echo tp.piApprox(n) + + tp.syncAll() # Block until all pending tasks are processed (implied in tp.shutdown()) + tp.shutdown() + +# Compile with nim c -r -d:release --threads:on --outdir:build example.nim +main() \ No newline at end of file