Difference between revisions of "Fibonacci"

From CSE231 Wiki
Jump to navigation Jump to search
Line 16: Line 16:
  
 
==Javadocs==
 
==Javadocs==
[http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/edu/wustl/cse231s/rice/habanero/Habanero.html Habanero]
+
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/v5/V5.html V5]
: [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/edu/wustl/cse231s/rice/habanero/Habanero.html#future-edu.rice.hj.api.HjSuspendingCallable- future(body)]  
+
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/v5/V5.html#future-edu.wustl.cse231s.v5.api.CheckedCallable- future(body)]  
[http://www.cs.rice.edu/~vs3/hjlib/doc/edu/rice/hj/api/HjFuture.html interface HjFuture<V>].   
+
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html interface Future<V>].   
: [http://www.cs.rice.edu/~vs3/hjlib/doc/edu/rice/hj/api/HjFuture.html#get-- get()]
+
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html#get-- get()]
  
 
=Common Mistakes To Avoid=
 
=Common Mistakes To Avoid=
==use Habanero's HjFuture<V>, NOT java.util.concurrent's Future<V>==
+
{{Warning|When memoizing, be sure to store the value for each calculation as it is made.}}
 
 
Check out [[Habanero#Future]] for pointers on using Habanero's futures.
 
 
 
==use long, NOT int==
 
lambdas will get cranky if you try to return an int instead of a long.
 
 
 
also, if you use ints in your calculation you will fail the tests when the result goes over [https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#MAX_VALUE Integer.MAX_VALUE].
 
  
 
=Studio (Required)=
 
=Studio (Required)=
Line 47: Line 40:
 
{{CodeToImplement|RecurrenceRelationParallelFibonacciCalculator|fibonacci|fibonacci.studio}}
 
{{CodeToImplement|RecurrenceRelationParallelFibonacciCalculator|fibonacci|fibonacci.studio}}
 
Use futures to add parallelism to the recurrence relation solution.
 
Use futures to add parallelism to the recurrence relation solution.
 +
 +
WORK: <math>\Phi^N</math>
 +
 +
CPL: <math>N</math>
 +
 +
Ideal Parallelism: <math>\dfrac{\Phi^N}{N}</math>
  
 
==MemoizationSequentialFibonacciCalculator==
 
==MemoizationSequentialFibonacciCalculator==
Line 54: Line 53:
 
==DynamicIterativeSequentialFibonacciCalculator==
 
==DynamicIterativeSequentialFibonacciCalculator==
  
 +
==LinearRecurrenceSequentialFibonacciCalculator==
 
===recurrence relation===
 
===recurrence relation===
  
Line 75: Line 75:
 
=Fun (Optional)=
 
=Fun (Optional)=
 
==DynamicRecursiveSequentialFibonacciCalculator==
 
==DynamicRecursiveSequentialFibonacciCalculator==
==LinearRecurrenceSequentialFibonacciCalculator==
 
 
==MemoizationParallelFibonacciCalculator==
 
==MemoizationParallelFibonacciCalculator==
 
Create all of your futures [0..N] up front and then call get on memo[n].
 
Create all of your futures [0..N] up front and then call get on memo[n].

Revision as of 20:07, 5 February 2018

Motivation

It is important to always reduce the work, then parallelize if possible. In this studio you will build a beautiful, elegant solution to Fibonacci which can be easily parallelized yielding off-the-charts ideal parallelism. Sadly, it performs terribly since the work is exponential . It is far better to build a linear time or log time algorithm independent of if can or cannot be parallelized.

We will convert recurrence relations into code.

Additionally, (and of minor importance) we gain experience working with the BigInteger class.

Background

The fibonacci sequence is a mathematical concept often used in computer science as a means to demonstrate iteration and recursion. Although you should be familiar with it from CSE 131, we will use the fibonacci sequence to demonstrate memoization and dynamic programming. Follow these links for a quick recap on memoization, dynamic programming, and the fibonacci sequence.

Where to Start

You will need to return the number associated with a given position in the fibonacci sequence. For example, if 0 was fed in, the method should return 0. 1 should return 1, 2 should return 1, 3 should return 2, and so on.

JUnit Test Suite

FibonacciTestSuite

Javadocs

V5

future(body)

interface Future<V>.

get()

Common Mistakes To Avoid

Attention niels epting.svg Warning:When memoizing, be sure to store the value for each calculation as it is made.

Studio (Required)

RecurrenceRelationSequentialFibonacciCalculator

class: RecurrenceRelationSequentialFibonacciCalculator.java Java.png
methods: fibonacci
package: fibonacci.studio
source folder: student/src/main/java

implement a recursive method for:

recurrence relation

seed values

RecurrenceRelationParallelFibonacciCalculator

class: RecurrenceRelationParallelFibonacciCalculator.java Java.png
methods: fibonacci
package: fibonacci.studio
source folder: student/src/main/java

Use futures to add parallelism to the recurrence relation solution.

WORK:

CPL:

Ideal Parallelism:

MemoizationSequentialFibonacciCalculator

class: MemoizationSequentialFibonacciCalculator.java Java.png
methods: fibonacciMemo
package: fibonacci.studio
source folder: student/src/main/java

Implement the recurrence relation algorithm but employ memoization for the win.

DynamicIterativeSequentialFibonacciCalculator

LinearRecurrenceSequentialFibonacciCalculator

recurrence relation

if n is odd
else

seed values

Fun (Optional)

DynamicRecursiveSequentialFibonacciCalculator

MemoizationParallelFibonacciCalculator

Create all of your futures [0..N] up front and then call get on memo[n].

RoundPhiToTheNOverSqrt5SequentialFibonacciCalculator

Testing Your Solution

Correctness

class: FibonacciTestSuite.java Junit.png
package: fibonacci.studio
source folder: testing/src/test/java

Performance

class: FibonacciIterations.java Noun Project stopwatch icon 386232 cc.svg
package: fibonacci.studio
source folder: src/main/java