Setup tests

This commit is contained in:
Mamy André-Ratsimbazafy 2021-07-01 10:51:35 +02:00
parent 1f7f2dfe01
commit a1e350094b
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
4 changed files with 55 additions and 20 deletions

View File

@ -8,12 +8,15 @@
import
# Stdlib
system/ansi_c, strformat, os, strutils, cpuinfo,
# Weave
../../weave
# Library
../../taskpools
when not defined(windows):
# bench
import ../wtime
var tp: Taskpool
proc dfs(depth, breadth: int): uint32 =
if depth == 0:
return 1
@ -22,13 +25,13 @@ proc dfs(depth, breadth: int): uint32 =
var sums = newSeq[Flowvar[uint32]](breadth)
for i in 0 ..< breadth:
sums[i] = spawn dfs(depth - 1, breadth)
sums[i] = tp.spawn dfs(depth - 1, breadth)
for i in 0 ..< breadth:
result += sync(sums[i])
proc test(depth, breadth: int): uint32 =
result = sync spawn dfs(depth, breadth)
result = sync tp.spawn dfs(depth, breadth)
proc main() =
@ -38,8 +41,8 @@ proc main() =
answer: uint32
nthreads: int
if existsEnv"WEAVE_NUM_THREADS":
nthreads = getEnv"WEAVE_NUM_THREADS".parseInt()
if existsEnv"TP_NUM_THREADS":
nthreads = getEnv"TP_NUM_THREADS".parseInt()
else:
nthreads = countProcessors()
@ -62,18 +65,14 @@ proc main() =
when not defined(windows):
let start = wtime_usec()
init(Weave)
tp = Taskpool.new()
answer = test(depth, breadth)
exit(Weave)
tp.shutdown()
when not defined(windows):
let stop = wtime_usec()
const lazy = defined(WV_LazyFlowvar)
const config = if lazy: " (lazy flowvars)"
else: " (eager flowvars)"
echo "Scheduler: Weave", config
echo "Scheduler: Taskpool"
echo "Benchmark: dfs"
echo "Threads: ", nthreads
when not defined(windows):

View File

@ -8,3 +8,37 @@ license = "MIT"
### Dependencies
requires "nim >= 1.2.12"
proc test(flags, path: string) =
if not dirExists "build":
mkDir "build"
# Note: we compile in release mode. This still have stacktraces
# but is much faster than -d:debug
# Compilation language is controlled by TEST_LANG
var lang = "c"
if existsEnv"TEST_LANG":
lang = getEnv"TEST_LANG"
echo "\n========================================================================================"
echo "Running [ ", lang, " ", flags, " ] ", path
echo "========================================================================================"
exec "nim " & lang & " " & flags & " --verbosity:0 --hints:off --warnings:off --threads:on -d:release --stacktrace:on --linetrace:on --outdir:build -r " & path
task test, "Run Taskpools tests":
# Internal data structures
test "", "taskpools/channels_spsc_single.nim"
test "", "taskpools/sparsesets.nim"
# Examples
test "", "examples/e01_simple_tasks.nim"
# Benchmarks
test "", "benchmarks/bouncing_producer_consumer/taskpool_bpc.nim"
test "", "benchmarks/dfs/taskpool_dfs.nim"
test "", "benchmarks/heat/taskpool_heat.nim"
test "", "benchmarks/nqueens/taskpool_nqueens.nim"
test "", "benchmarks/single_task_producer/taskpool_spc.nim"
# TODO - generics in macro issue
# test "", "benchmarks/matmul_cache_oblivious/taskpool_matmul_co.nim"

View File

@ -93,7 +93,7 @@ func trySend*[T](chan: var ChannelSPSCSingle, src: sink T): bool {.inline.} =
# Sanity checks
# ------------------------------------------------------------------------------
when isMainModule:
import ../memory/memory_pools
import system/ansi_c
when not compileOption("threads"):
{.error: "This requires --threads:on compilation flag".}
@ -157,11 +157,9 @@ when isMainModule:
proc main() =
echo "Testing if 2 threads can send data"
echo "-----------------------------------"
var threads: array[2, Thread[ThreadArgs]]
var pool: TLPoolAllocator
pool.initialize()
var chan = pool.borrow(ChannelSPSCSingle)
var threads: array[2, Thread[ThreadArgs]]
var chan = cast[ptr ChannelSPSCSingle](c_calloc(1, csize_t sizeof(ChannelSPSCSingle)))
chan[].initialize(itemSize = sizeof(int))
createThread(threads[0], thread_func, ThreadArgs(ID: Receiver, chan: chan))
@ -170,7 +168,7 @@ when isMainModule:
joinThread(threads[0])
joinThread(threads[1])
recycle(chan)
c_free(chan)
echo "-----------------------------------"
echo "Success"

View File

@ -54,7 +54,11 @@ proc park*(en: var EventNotifier) {.inline.} =
## Wait until we are signaled of an event
## Thread is parked and does not consume CPU resources
en.lock.acquire()
preCondition: en.signals == 0
if en.signals > 0:
en.signals -= 1
en.lock.release()
return
en.parked += 1
while en.signals == 0: # handle spurious wakeups