Libraryless. Click here for Pure Java version (9223L/51K).
1 | // Main function that user can call |
2 | static long stupidFib_collab(ICollab collab, int n) { |
3 | ret collab_computeLong2(collab, continuation -> stupidFib_collab(collab, n, continuation)); |
4 | } |
5 | |
6 | // Work function used internally |
7 | svoid stupidFib_collab(ICollab collab, int n, LongConsumer continuation) { |
8 | // switch to serial if no coworkers, OR if small enough work unit |
9 | if (collab.singleCore() || n < 24) ret with continuation.accept(stupidFib(n)); |
10 | |
11 | // recursion base |
12 | if (n < 3) ret with continuation.accept(1); |
13 | |
14 | // recursion step |
15 | var collector = longPairCollector((a, b) -> continuation.accept(a+b)); |
16 | collab.addWork(-> stupidFib_collab(collab, n-1, collector.a)); |
17 | collab.addWork(-> stupidFib_collab(collab, n-2, collector.b)); |
18 | } |
19 | |
20 | /* |
21 | |
22 | // Both functions in one using the "parallel" modifier |
23 | static parallel long stupidFib_collab(int n) { |
24 | // switch to serial if no coworkers, OR if small enough work unit |
25 | if (collab.singleCore() || n < 24) ret stupidFib(n); |
26 | |
27 | // recursion base |
28 | if (n < 3) ret 1; |
29 | |
30 | // recursion step |
31 | var collector = longPairCollector((a, b) -> ret a+b); |
32 | collab.addWork(-> stupidFib_collab(collab, n-1, collector.setA())); |
33 | collab.addWork(-> stupidFib_collab(collab, n-2, collector.setB())); |
34 | } |
35 | |
36 | */ |
Began life as a copy of #1035566
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): elmgxqgtpvxh, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035569 |
Snippet name: | stupidFib_collab - stupidFib as a massively parallel function! |
Eternal ID of this version: | #1035569/26 |
Text MD5: | e8869546fb889c61bbcbb648ca2feb06 |
Transpilation MD5: | 965b76052e4273e340432f54d9edc74e |
Author: | stefan |
Category: | javax / maths |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-06-19 04:22:48 |
Source code size: | 1273 bytes / 36 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 172 / 324 |
Version history: | 25 change(s) |
Referenced in: | [show references] |