-
Notifications
You must be signed in to change notification settings - Fork 59
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
Add a concurrency example using a massive number of spawns #1429
Open
lucteo
wants to merge
5
commits into
bulk_spawn
Choose a base branch
from
skynet
base: bulk_spawn
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d26138c
Add skynet benchmark, using strict concurrency.
lucteo 75be07f
Add skynet with weakly structured concurrency.
lucteo 436fffc
Bulk skynet example.
lucteo 0465354
Add timing inside the skynet examples.
lucteo dced14d
Update Tests/EndToEndTests/TestCases/Concurrency/skynet_bulk.hylo
kyouko-taiga 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
42 changes: 42 additions & 0 deletions
42
Tests/EndToEndTests/TestCases/Concurrency/skynet_bulk.hylo
This file contains 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,42 @@ | ||
//- compileToLLVM expecting: success | ||
|
||
fun skynet(num: Int, size: Int, div: Int) -> Int { | ||
if size == 1 { | ||
return num.copy() | ||
} else { | ||
var results = Array<Int>(count: div, initialized_with: fun (_ i: Int) { 0 }) | ||
let sub_size = size / div | ||
let p = mutable_pointer[to: &results] | ||
spawn_(count: div, action: fun[sink let q = p.copy(), sink let n = num.copy(), sink let s = sub_size.copy(), sink let d = div.copy()] (index: Int) -> Void { | ||
inout results = q.copy().unsafe[] | ||
&results[index] = skynet(num: n + index * s, size: s, div: d) | ||
}).await() | ||
|
||
return sum(results) | ||
} | ||
} | ||
|
||
fun sum(_ array: Array<Int>) -> Int { | ||
var result = 0 | ||
var i = 0 | ||
while i != array.count() { | ||
&result += array[i] | ||
&i += 1 | ||
} | ||
return result | ||
} | ||
|
||
@ffi("clock") | ||
public fun clock() -> Int | ||
|
||
fun time_in_ms(_ clock_diff: Int) -> Int { | ||
return clock_diff / 10_000 // TODO: constant dependent on the platform | ||
} | ||
|
||
public fun main() { | ||
let start = clock() | ||
let result = skynet(num: 0, size: 1_000_000, div: 10); | ||
let end = clock() | ||
print(time_in_ms(end - start), terminator: " ms\n") | ||
precondition(result == 499999500000, "invalid result") | ||
} |
67 changes: 67 additions & 0 deletions
67
Tests/EndToEndTests/TestCases/Concurrency/skynet_strict.hylo
This file contains 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,67 @@ | ||
//- compileToLLVM expecting: success | ||
|
||
fun skynet(num: Int, size: Int, div: Int) -> Int { | ||
if size == 1 { | ||
return num.copy() | ||
} else { | ||
let sub_size = size / div | ||
let f1 = spawn_(fun[sink let n = num.copy(), sink let s = sub_size.copy(), sink let d = div.copy()] () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto long lines |
||
skynet(num: n, size: s, div: d) | ||
}) | ||
let f2 = spawn_(fun[sink let n = num + sub_size, sink let s = sub_size.copy(), sink let d = div.copy()] () { | ||
skynet(num: n, size: s, div: d) | ||
}) | ||
let f3 = spawn_(fun[sink let n = num + 2 * sub_size, sink let s = sub_size.copy(), sink let d = div.copy()] () { | ||
skynet(num: n, size: s, div: d) | ||
}) | ||
let f4 = spawn_(fun[sink let n = num + 3 * sub_size, sink let s = sub_size.copy(), sink let d = div.copy()] () { | ||
skynet(num: n, size: s, div: d) | ||
}) | ||
let f5 = spawn_(fun[sink let n = num + 4 * sub_size, sink let s = sub_size.copy(), sink let d = div.copy()] () { | ||
skynet(num: n, size: s, div: d) | ||
}) | ||
let f6 = spawn_(fun[sink let n = num + 5 * sub_size, sink let s = sub_size.copy(), sink let d = div.copy()] () { | ||
skynet(num: n, size: s, div: d) | ||
}) | ||
let f7 = spawn_(fun[sink let n = num + 6 * sub_size, sink let s = sub_size.copy(), sink let d = div.copy()] () { | ||
skynet(num: n, size: s, div: d) | ||
}) | ||
let f8 = spawn_(fun[sink let n = num + 7 * sub_size, sink let s = sub_size.copy(), sink let d = div.copy()] () { | ||
skynet(num: n, size: s, div: d) | ||
}) | ||
let f9 = spawn_(fun[sink let n = num + 8 * sub_size, sink let s = sub_size.copy(), sink let d = div.copy()] () { | ||
skynet(num: n, size: s, div: d) | ||
}) | ||
let f10 = spawn_(fun[sink let n = num + 9 * sub_size, sink let s = sub_size.copy(), sink let d = div.copy()] () { | ||
skynet(num: n, size: s, div: d) | ||
}) | ||
|
||
var sum = 0 | ||
sum += f1.await() | ||
sum += f2.await() | ||
sum += f3.await() | ||
sum += f4.await() | ||
sum += f5.await() | ||
sum += f6.await() | ||
sum += f7.await() | ||
sum += f8.await() | ||
sum += f9.await() | ||
sum += f10.await() | ||
return sum | ||
} | ||
} | ||
|
||
@ffi("clock") | ||
public fun clock() -> Int | ||
|
||
fun time_in_ms(_ clock_diff: Int) -> Int { | ||
return clock_diff / 10_000 // TODO: constant dependent on the platform | ||
} | ||
|
||
public fun main() { | ||
let start = clock() | ||
let result = skynet(num: 0, size: 1_000_000, div: 10); | ||
let end = clock() | ||
print(time_in_ms(end - start), terminator: " ms\n") | ||
precondition(result == 499999500000, "invalid result") | ||
} |
53 changes: 53 additions & 0 deletions
53
Tests/EndToEndTests/TestCases/Concurrency/skynet_weak.hylo
This file contains 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,53 @@ | ||
//- compileToLLVM expecting: success | ||
|
||
fun skynet(num: Int, size: Int, div: Int) -> Int { | ||
if size == 1 { | ||
return num.copy() | ||
} else { | ||
let sub_size = size / div | ||
var futures = Array<EscapingFuture<{n: Int, s: Int, d: Int}>>() | ||
&futures.reserve_capacity(div) | ||
var i = 0 | ||
while i != div { | ||
let cur_num = num + i * sub_size | ||
let f = escaping_spawn_(fun[sink let n = cur_num.copy(), sink let s = sub_size.copy(), sink let d = div.copy()] () -> Int { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so long! |
||
skynet(num: n, size: s, div: d) | ||
}) | ||
&futures.append(f) | ||
&i += 1 | ||
} | ||
|
||
var sum = 0 | ||
var i2 = 0 | ||
while i2 != div { | ||
if inout f: EscapingFuture<{n: Int, s: Int, d: Int}> = &futures.pop_last() { | ||
sum += &f.await() | ||
} | ||
&i2 += 1 | ||
} | ||
return sum | ||
} | ||
} | ||
|
||
public conformance EscapingFuture: SemiRegular { | ||
|
||
public fun infix== (_ other: Self) -> Bool { | ||
return false | ||
} | ||
|
||
} | ||
|
||
@ffi("clock") | ||
public fun clock() -> Int | ||
|
||
fun time_in_ms(_ clock_diff: Int) -> Int { | ||
return clock_diff / 10_000 // TODO: constant dependent on the platform | ||
} | ||
|
||
public fun main() { | ||
let start = clock() | ||
let result = skynet(num: 0, size: 1_000_000, div: 10); | ||
let end = clock() | ||
print(time_in_ms(end - start), terminator: " ms\n") | ||
precondition(result == 499999500000, "invalid result") | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines are getting pretty long and even if they are less than 100 columns there's so much going on in the capture that it is hard to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Lucian mentioned that he thinks that's a problem with Hylo's current design of captures. The issue is that he needs independent lambdas and so there is currently no real alternative, since implicit captures create
let
/inout
accesses.I guess one solution would be to declare the environment as a possibly typealiased tuple and then there would be only one capture, but that's only shifting verbosity around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not asking for anything fancy; just line breaks