Commit aa801bc4 authored by MoutazDebbaneh's avatar MoutazDebbaneh

Add question 3 solution. Edit README.md

parent ea496c5a
......@@ -5,14 +5,10 @@
</component>
<component name="ChangeListManager">
<list default="true" id="39d1573e-bc8d-4759-8e1b-31b85df143ea" name="Changes" comment="Add question 2 solution">
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/hiast/hw/question2/Student.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/hiast/hw/question2/StudentAnalytics.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/hiast/hw/question2/StudentAnalyticsTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/hiast/hw/question3/Fibonacci.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/hiast/hw/question3/FibonacciTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/example/ArrayOccurrencesCount.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/hiast/hw/question1/ArrayOccurrencesCount.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/ArrayOccurrencesCountTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/hiast/hw/question1/ArrayOccurrencesCountTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -62,19 +58,22 @@
<key name="MoveClassesOrPackagesDialog.RECENTS_KEY">
<recent name="hiast.hw.question1" />
</key>
<key name="CopyClassDialog.RECENTS_KEY">
<recent name="hiast.hw.question3" />
</key>
</component>
<component name="RunManager" selected="JUnit.StudentAnalyticsTest.testCountNumberOfFailedStudentsOlderThan20Perf">
<configuration name="StudentAnalyticsTest.testAverageAgeOfEnrolledStudentsPerf" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<component name="RunManager" selected="JUnit.FibonacciTest.testFiboPP">
<configuration name="FibonacciTest.testFiboPP" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="ArrayOccurrencesCount" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="hiast.hw.question2.*" />
<option name="PATTERN" value="hiast.hw.question3.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="hiast.hw.question2" />
<option name="MAIN_CLASS_NAME" value="hiast.hw.question2.StudentAnalyticsTest" />
<option name="METHOD_NAME" value="testAverageAgeOfEnrolledStudentsPerf" />
<option name="PACKAGE_NAME" value="hiast.hw.question3" />
<option name="MAIN_CLASS_NAME" value="hiast.hw.question3.FibonacciTest" />
<option name="METHOD_NAME" value="testFiboPP" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
......@@ -146,11 +145,11 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.FibonacciTest.testFiboPP" />
<item itemvalue="JUnit.StudentAnalyticsTest.testCountNumberOfFailedStudentsOlderThan20Perf" />
<item itemvalue="JUnit.StudentAnalyticsTest.testCountNumberOfFailedStudentsOlderThan20" />
<item itemvalue="JUnit.StudentAnalyticsTest.testMostCommonFirstNameOfInactiveStudentsPerf" />
<item itemvalue="JUnit.StudentAnalyticsTest.testMostCommonFirstNameOfInactiveStudents" />
<item itemvalue="JUnit.StudentAnalyticsTest.testAverageAgeOfEnrolledStudentsPerf" />
</list>
</recent_temporary>
</component>
......@@ -184,7 +183,14 @@
<option name="project" value="LOCAL" />
<updated>1679409170258</updated>
</task>
<option name="localTasksCounter" value="4" />
<task id="LOCAL-00004" summary="Add question 2 solution">
<created>1679411809900</created>
<option name="number" value="00004" />
<option name="presentableId" value="LOCAL-00004" />
<option name="project" value="LOCAL" />
<updated>1679411809900</updated>
</task>
<option name="localTasksCounter" value="5" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
......
# Parallel Programming HW1
# Parallel Programming HW1 - Moutaz Debbaneh
> Important Note: Please make sure to change JVM memory heap settings to enable making an array of a billion elements
......@@ -6,4 +6,8 @@
<br />
#### 1. Question One:
- Function intStream() was used for testing instead of randomArray() because its answer is expected, and therefore the comparison is easier
\ No newline at end of file
- Function intStream() was used for testing instead of randomArray() because its answer is expected, and therefore the comparison is easier.
- The array size of 1e9 elements was commented and replaced with 1e8 to avoid runtime errors in case JVM heap settings was not properly adjusted.
#### 2. Question Three:
- The return type of fibonacci was changed from Integer to Long to avoid overflow for n=50.
\ No newline at end of file
package hiast.hw.question3;
import java.util.HashMap;
import java.util.concurrent.RecursiveTask;
/*
However, besides being a dumb way to compute Fibonacci functions (there is a simple fast linear algorithm that you'd use in practice),
this is likely to perform poorly because the smallest subtasks are too small to be worthwhile splitting up.
Instead, as is the case for nearly all fork/join applications,
you'd pick some minimum granularity size (for example 10 here) for which you always sequentially solve rather than subdividing.
*/
public class Fibonacci extends RecursiveTask<Long> {
final int n;
final HashMap<Integer, Long> memo;
public Fibonacci(int n, HashMap<Integer, Long> memo) {
this.n = n;
this.memo = memo;
}
public Long compute() {
if(memo.get(n) != -1) return memo.get(n);
if(n > 20) {
if (n <= 1)
return Long.valueOf(n);
Fibonacci f1 = new Fibonacci(n - 1, memo);
f1.fork();
Fibonacci f2 = new Fibonacci(n - 2, memo);
Long result = f2.compute() + f1.join();
memo.put(n, result);
}else{
Long result = computeSeq();
memo.put(n, result);
}
return memo.get(n);
}
public Long computeSeq() {
if(memo.get(n) != -1) return memo.get(n);
Fibonacci f1 = new Fibonacci(n - 1, memo);
Fibonacci f2 = new Fibonacci(n - 2, memo);
Long result = f2.computeSeq() + f1.computeSeq();
memo.put(n, result);
return result;
}
}
package hiast.hw.question3;
import junit.framework.TestCase;
import java.util.HashMap;
public class FibonacciTest extends TestCase {
int n=50;
final HashMap<Integer, Long> memo = new HashMap<>();
private void initializeMemo(){
memo.put(0, 0L);
memo.put(1, 1L);
for(int i=2;i<=n;i++){
memo.put(i, -1L);
}
}
public void testFiboPP(){
initializeMemo();
long start = System.currentTimeMillis();
Fibonacci fib = new Fibonacci(n, memo);
long res = fib.compute();
long end = System.currentTimeMillis()-start;
System.out.printf("Fibonacci for %d is %d, and parallel execution took %d ms\n",n,res,end);
// Fibonacci parallel takes 144269 fon n = 50 before enhancement 1
}
public void testFiboSeq(){
initializeMemo();
long start = System.currentTimeMillis();
Fibonacci fib = new Fibonacci(n, memo);
long res = fib.computeSeq();
long end = System.currentTimeMillis()-start;
System.out.printf("Fibonacci for %d is %d, and sequential execution took %d ms\n",n,res,end);
//Fibonacci Sequential takes 86495 fon n = 50 before enhancement 1
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment