Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to set global thread number during runtime #33

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/malebolgia.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
shouldEndAt: Time
usesTimeout, activeProducer: bool

proc `=destroy`(m: var Master) {.inline.} =

Check warning on line 19 in src/malebolgia.nim

View workflow job for this annotation

GitHub Actions / linux-amd64-nim-devel (master)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 19 in src/malebolgia.nim

View workflow job for this annotation

GitHub Actions / macos-amd64-nim-devel (master)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 19 in src/malebolgia.nim

View workflow job for this annotation

GitHub Actions / windows-amd64-nim-devel (master)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 19 in src/malebolgia.nim

View workflow job for this annotation

GitHub Actions / windows-i386-nim-devel (master)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]
deinitCond(m.c)
deinitLock(m.L)

Expand Down Expand Up @@ -104,7 +104,7 @@
data: array[FixedChanSize, PoolTask]

var
thr: array[ThreadPoolSize-1, Thread[void]] # -1 because the main thread counts too
thr: seq[Thread[void]]
chan: FixedChan
globalStopToken: Atomic[bool]
busyThreads: Atomic[int]
Expand Down Expand Up @@ -153,10 +153,13 @@
# but mark it as completed either way!
taskCompleted item.m[]

proc setup() =
proc malebolgiaSetup*(numThr: int) =
doAssert numThr > 0, "Malebolgia must be initialized with a thread pool size of at least one"
doAssert thr.len == 0, "Malebolgia can only be initialized once"
initCond(chan.dataAvailable)
initCond(chan.spaceAvailable)
initLock(chan.L)
thr.setLen(numThr - 1) # -1 because the main thread counts too
for i in 0..high(thr): createThread[void](thr[i], worker)

proc panicStop*() =
Expand All @@ -168,7 +171,10 @@
deinitLock(chan.L)

proc shouldSend(master: var Master): bool {.inline.} =
master.activeProducer or busyThreads.load(moRelaxed) < ThreadPoolSize-1
{.cast(gcsafe).}:
let numThr = thr.len
doAssert numThr > 0, "Malebolgia must be initialized first"
master.activeProducer or busyThreads.load(moRelaxed) < numThr

template spawnImplRes[T](master: var Master; fn: typed; res: T) =
if stillHaveTime(master):
Expand Down Expand Up @@ -254,6 +260,6 @@
waitForCompletions(master)

when not defined(maleSkipSetup):
setup()
malebolgiaSetup(ThreadPoolSize)

include malebolgia / masterhandles
27 changes: 27 additions & 0 deletions tests/tasymmetric_load.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import malebolgia

import std/[os, atomics]

malebolgiaSetup(numThr = 20)

var
threadpool = createMaster(activeProducer = true)
busyThreads: Atomic[int]
busyThreads.store 0

let mainThreadId = getThreadId()

proc findStartPositionsAndPlay() =
atomicInc busyThreads
echo "Started. Busy threads: ", busyThreads.load

sleep(1000)
if getThreadId() == mainThreadId:
sleep(10_000)

atomicDec busyThreads
echo "Finished. Busy threads: ", busyThreads.load

threadpool.awaitAll:
for i in 1 .. 10000:
threadpool.spawn findStartPositionsAndPlay()
Loading