This commit is contained in:
Jaremy Creechley 2024-02-08 20:28:25 -07:00
parent 22566c40ff
commit 7fe5d512bb
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
3 changed files with 34 additions and 0 deletions

View File

@ -11,3 +11,5 @@ srcDir = "src"
requires "chronos >= 4.0.0"
requires "threading"
requires "taskpool"
requires "taskpools"

View File

@ -1 +1,3 @@
switch("path", "$projectDir/../src")
--threads:on

30
tests/tpools.nim Normal file
View File

@ -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()