mirror of
https://github.com/logos-storage/apatheia.git
synced 2026-01-02 13:03:11 +00:00
remove expers
This commit is contained in:
parent
69ce89f43a
commit
816ce10932
@ -1,40 +0,0 @@
|
||||
import std/os
|
||||
|
||||
import chronos
|
||||
import chronos/threadsync
|
||||
import chronos/unittest2/asynctests
|
||||
import taskpools
|
||||
|
||||
## todo: setup basic async + threadsignal + taskpools example here
|
||||
##
|
||||
|
||||
var todo: Future[float]
|
||||
|
||||
proc completeAfter() {.async.} =
|
||||
{.cast(gcsafe).}:
|
||||
await sleepAsync(500.milliseconds)
|
||||
echo "completing result: "
|
||||
todo.complete(4.0)
|
||||
|
||||
proc addNums(a, b: float): Future[float] =
|
||||
{.cast(gcsafe).}:
|
||||
let fut = newFuture[float]("addNums")
|
||||
todo = fut
|
||||
result = fut
|
||||
|
||||
suite "async tests":
|
||||
|
||||
# var tp = Taskpool.new(num_threads = 2) # Default to the number of hardware threads.
|
||||
|
||||
asyncTest "test":
|
||||
|
||||
# await sleepAsync(100.milliseconds)
|
||||
let fut = addNums(1, 3)
|
||||
asyncSpawn completeAfter()
|
||||
echo "\nawaiting result: "
|
||||
let res = await fut
|
||||
|
||||
echo "\nRES: ", res
|
||||
|
||||
check true
|
||||
|
||||
@ -1,40 +0,0 @@
|
||||
import std/os
|
||||
|
||||
import chronos
|
||||
import chronos/threadsync
|
||||
import chronos/unittest2/asynctests
|
||||
import taskpools
|
||||
|
||||
## todo: setup basic async + threadsignal + taskpools example here
|
||||
##
|
||||
|
||||
type
|
||||
ThreadArg = object
|
||||
startSig: ThreadSignalPtr
|
||||
doneSig: ThreadSignalPtr
|
||||
value: float
|
||||
|
||||
proc addNums(a, b: float, ret: ptr ThreadArg) =
|
||||
ret.value = a + b
|
||||
os.sleep(500)
|
||||
let res = ret.doneSig.fireSync().get()
|
||||
if not res:
|
||||
echo "ERROR FIRING!"
|
||||
|
||||
suite "async tests":
|
||||
|
||||
var tp = Taskpool.new(num_threads = 2) # Default to the number of hardware threads.
|
||||
|
||||
asyncTest "test":
|
||||
var args = ThreadArg()
|
||||
args.startSig = ThreadSignalPtr.new().get()
|
||||
args.doneSig = ThreadSignalPtr.new().get()
|
||||
|
||||
tp.spawn addNums(1, 2, addr args)
|
||||
# await sleepAsync(100.milliseconds)
|
||||
await wait(args.doneSig).wait(1500.milliseconds)
|
||||
|
||||
echo "\nRES: ", args.value
|
||||
|
||||
check true
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
# This is just an example to get you started. You may wish to put all of your
|
||||
# tests into a single file, or separate them into multiple `test1`, `test2`
|
||||
# etc. files (better names are recommended, just make sure the name starts with
|
||||
# the letter 't').
|
||||
#
|
||||
# To run these tests, simply execute `nimble test`.
|
||||
|
||||
import unittest
|
||||
|
||||
import apatheia/tasks
|
||||
|
||||
test "can add":
|
||||
check 10 == 10
|
||||
@ -1,33 +0,0 @@
|
||||
|
||||
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: int): float =
|
||||
if k mod 2 == 1:
|
||||
-4'f / float(2*k + 1)
|
||||
else:
|
||||
4'f / float(2*k + 1)
|
||||
|
||||
proc piApprox(tp: Taskpool, n: int): float =
|
||||
var pendingFuts = newSeq[FlowVar[float]](n)
|
||||
for k in 0 ..< pendingFuts.len:
|
||||
pendingFuts[k] = tp.spawn term(k) # Schedule a task on the threadpool a return a handle to retrieve the result.
|
||||
for k in 0 ..< pendingFuts.len:
|
||||
result += sync pendingFuts[k] # Block until the result is available.
|
||||
|
||||
proc main() =
|
||||
var n = 1_000
|
||||
var nthreads = countProcessors()
|
||||
|
||||
var tp = Taskpool.new(num_threads = nthreads) # Default to the number of hardware threads.
|
||||
|
||||
echo formatFloat(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()
|
||||
Loading…
x
Reference in New Issue
Block a user