Libraryless. Click here for Pure Java version (9237L/51K).
1 | srecord noeq StupidFib_collab(ICollab collab, int n) { |
2 | // if n <= parallelizationThreshold, we calculate serially |
3 | settable int parallelizationThreshold = 24; |
4 | |
5 | // main calculation function |
6 | // (if one core, go serial, otherwise parallelize) |
7 | long get() { |
8 | if (collab.coresToUse() == 1) |
9 | ret serialComputation(n); |
10 | else |
11 | ret collab_computeLong(collab, receiver -> new Step(n, receiver)); |
12 | } |
13 | |
14 | // serial version |
15 | long serialComputation(int n) { |
16 | ret stupidFib(n); |
17 | } |
18 | |
19 | // individual calculation step for parallel version |
20 | record noeq Step(int n, LongConsumer continuation) extends LongPairCollector is Runnable { |
21 | // if below parallelization threshold, go serial, otherwise parallelize |
22 | run { |
23 | if (n < max(3, parallelizationThreshold)) |
24 | continuation.accept(serialComputation(n)); |
25 | else { |
26 | collab.addWork(new Step(n-1, setterForA())); |
27 | collab.addWork(new Step(n-2, setterForB())); |
28 | } |
29 | } |
30 | |
31 | // calculate and forward result when subcomputations have completed |
32 | void complete(long a, long b) { |
33 | continuation.accept(a+b); |
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: | #1035571 |
Snippet name: | StupidFib_collab - Stupid-Fib in parallel as a class |
Eternal ID of this version: | #1035571/26 |
Text MD5: | cbe9761503b6df8ad35540f2320fb56d |
Transpilation MD5: | 1818c1204f84d219c8c7529cfa57be4b |
Author: | stefan |
Category: | javax / maths |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-06-20 15:57:50 |
Source code size: | 1156 bytes / 36 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 192 / 341 |
Version history: | 25 change(s) |
Referenced in: | [show references] |