-
Notifications
You must be signed in to change notification settings - Fork 159
Chronos coro prime sieve #483
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
Merged
hanabi1224
merged 9 commits into
hanabi1224:main
from
Clemente90:chronos-coro-prime-sieve
Jul 31, 2025
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dc63102
Add async solution to Nim
463e974
Add another async solution to Nim
6263af7
Merge branch 'main' into chronos-coro-prime-sieve
Clemente90 a34cc25
Update bench/bench_nim.yaml
Clemente90 b9e8693
Revert build line
21d8bb1
Revert build line
6863f1c
Add command to install deps prior to build
a8831c9
fix build and add new implementations
46fba96
fix 'floating point contraction' messing up mandelbrot
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import chronos | ||
| import std/os | ||
| import std/strutils | ||
|
|
||
|
|
||
|
|
||
| ## | ||
| ## # Channel[T] | ||
| ## | ||
| ## I craeted a very simple async-awaitable channel type so I can match the | ||
| ## reference Go implementation this benchmark originated from. | ||
| ## | ||
|
|
||
|
|
||
| type Channel[T] = object | ||
| ## An simple one-item, async-awaitable Channel. | ||
| untilIsEmpty: Future[void] | ||
| untilIsFull: Future[void] | ||
| val: T | ||
|
|
||
|
|
||
| proc newChannel[T](): ref Channel[T] = | ||
| ## Initializer. Allocate a new ref Channel object on the heap. | ||
| result = new Channel[T] | ||
| result[].untilIsEmpty = newFuture[void]() | ||
| result[].untilIsFull = newFuture[void]() | ||
| result[].untilIsEmpty.complete() | ||
|
|
||
|
|
||
| proc send(chan: ref Channel[int], val: int) {.async.} = | ||
| # Accept val if empty, otherwise, suspend until empty. | ||
| await chan[].untilIsEmpty | ||
| chan[].untilIsEmpty = newFuture[void]() | ||
| chan[].val = val | ||
| chan[].untilIsFull.complete() | ||
|
|
||
|
|
||
| proc recv[T](chan: ref Channel[T]): Future[T] {.async.} = | ||
| # Return held val if full, otherwise, suspend until full. | ||
| await chan[].untilIsFull | ||
| chan[].untilIsFull = newFuture[void]() | ||
| result = chan[].val | ||
| chan[].untilIsEmpty.complete() | ||
|
|
||
|
|
||
|
|
||
| ## | ||
| ## # Benchmark | ||
| ## | ||
| ## Below, "Concurrent Prime Sieve" that matches Go reference implementation. | ||
| ## | ||
| ## [X] Uses coroutines. | ||
| ## [X] Uses a coroutine scheduler. | ||
| ## [X] Uses an async channel for communitating between coroutines. | ||
| ## [X] Same 3 functions, structured like the reference. | ||
| ## | ||
|
|
||
|
|
||
| proc generate(chan: ref Channel[int]) {.async.} = | ||
| ## Send the sequence 2, 3, 4, ... to cannel `chan`. | ||
| for i in 2 .. int.high: | ||
| await chan.send(i) | ||
|
|
||
|
|
||
| proc filter(inChan, outChan: ref Channel[int], prime: int) {.async.} = | ||
| ## Copy the values from channel `inChan` to channel `outChan`, removing those | ||
| ## divisible by `prime`. | ||
| while true: | ||
| let i = await inChan.recv() # revieve value from `inChan` | ||
| if i mod prime != 0: | ||
| await outChan.send(i) # send `i` to `outChan` | ||
|
|
||
|
|
||
| proc main(n: int) {.async.} = | ||
| ## The prime sieve: Daisy-chain filter processes. | ||
| var firstChan = newChannel[int]() # craete a new channel | ||
| asyncCheck generate(firstChan) # launch generate coroutine | ||
| for i in 0 ..< n: | ||
| let prime = await firstChan.recv() | ||
| echo prime | ||
| var secondChan = newChannel[int]() | ||
| asyncCheck filter(firstChan, secondChan, prime) | ||
| firstChan = secondChan | ||
|
|
||
|
|
||
| when isMainModule: | ||
|
|
||
| let n = if paramCount() > 0: parseInt(paramStr(1)) else: 100 | ||
| waitFor main(n) | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ bin = @["app"] | |
| # Dependencies | ||
|
|
||
| requires "nim >= 2.0.0" | ||
| requires "chronos" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.