Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

36
LINES

< > BotCompany Repo | #1035571 // StupidFib_collab - Stupid-Fib in parallel as a class

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (9237L/51K).

srecord noeq StupidFib_collab(ICollab collab, int n) {
  // if n <= parallelizationThreshold, we calculate serially
  settable int parallelizationThreshold = 24;

  // main calculation function
  // (if one core, go serial, otherwise parallelize)
  long get() {
    if (collab.coresToUse() == 1)
      ret serialComputation(n);
    else
      ret collab_computeLong(collab, receiver -> new Step(n, receiver));
  }
  
  // serial version
  long serialComputation(int n) {
    ret stupidFib(n);
  }
  
  // individual calculation step for parallel version
  record noeq Step(int n, LongConsumer continuation) extends LongPairCollector is Runnable {
    // if below parallelization threshold, go serial, otherwise parallelize
    run {
      if (n < max(3, parallelizationThreshold))
        continuation.accept(serialComputation(n));
      else {
        collab.addWork(new Step(n-1, setterForA()));
        collab.addWork(new Step(n-2, setterForB()));
      }
    }
    
    // calculate and forward result when subcomputations have completed
    void complete(long a, long b) {
      continuation.accept(a+b);
    }
  }
}

Author comment

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: 191 / 339
Version history: 25 change(s)
Referenced in: [show references]