Commit b508e72a authored by MoutazDebbaneh's avatar MoutazDebbaneh

Add first question solution

parent 82400e40
...@@ -4,29 +4,110 @@ ...@@ -4,29 +4,110 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="39d1573e-bc8d-4759-8e1b-31b85df143ea" name="Changes" comment=""> <list default="true" id="39d1573e-bc8d-4759-8e1b-31b85df143ea" name="Changes" comment="Initial commit">
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/example/ArrayOccurrencesCount.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/ArrayOccurrencesCountTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/example/Main.java" beforeDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" /> <option name="LAST_RESOLUTION" value="IGNORE" />
</component> </component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings"> <component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component> </component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2NK5ztYDvTO4MIiuysgyKXIMn9e" /> <component name="ProjectId" id="2NK5ztYDvTO4MIiuysgyKXIMn9e" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" /> <component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="2" id="Add" />
</component>
<component name="ProjectViewState"> <component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"RunOnceActivity.OpenProjectViewOnStart": "true", &quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
"RunOnceActivity.ShowReadmeOnStart": "true" &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;project.structure.last.edited&quot;: &quot;Problems&quot;,
&quot;project.structure.proportion&quot;: &quot;0.15&quot;,
&quot;project.structure.side.proportion&quot;: &quot;0.2&quot;
} }
}]]></component> }</component>
<component name="RunManager" selected="JUnit.ArrayOccurrencesCountTest.testArrayOccurrencesCountStreamSeq">
<configuration name="ArrayOccurrencesCountTest.testArrayOccurrencesCountPP" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="ArrayOccurrencesCount" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="ArrayOccurrencesCountTest" />
<option name="METHOD_NAME" value="testArrayOccurrencesCountPP" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="ArrayOccurrencesCountTest.testArrayOccurrencesCountStream" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="ArrayOccurrencesCount" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="ArrayOccurrencesCountTest" />
<option name="METHOD_NAME" value="testArrayOccurrencesCountStream" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="ArrayOccurrencesCountTest.testArrayOccurrencesCountStreamPP" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="ArrayOccurrencesCount" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="ArrayOccurrencesCountTest" />
<option name="METHOD_NAME" value="testArrayOccurrencesCountStreamPP" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="ArrayOccurrencesCountTest.testArrayOccurrencesCountStreamSeq" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="ArrayOccurrencesCount" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="ArrayOccurrencesCountTest" />
<option name="METHOD_NAME" value="testArrayOccurrencesCountStreamSeq" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="ArrayOccurrencesCountTest.testArraySumSeq" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="ArrayOccurrencesCount" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="ArrayOccurrencesCountTest" />
<option name="METHOD_NAME" value="testArraySumSeq" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.ArrayOccurrencesCountTest.testArrayOccurrencesCountStreamSeq" />
<item itemvalue="JUnit.ArrayOccurrencesCountTest.testArrayOccurrencesCountStreamPP" />
<item itemvalue="JUnit.ArrayOccurrencesCountTest.testArrayOccurrencesCountStream" />
<item itemvalue="JUnit.ArrayOccurrencesCountTest.testArrayOccurrencesCountPP" />
<item itemvalue="JUnit.ArrayOccurrencesCountTest.testArraySumSeq" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager"> <component name="TaskManager">
<task active="true" id="Default" summary="Default task"> <task active="true" id="Default" summary="Default task">
...@@ -36,6 +117,14 @@ ...@@ -36,6 +117,14 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1679400203183</updated> <updated>1679400203183</updated>
</task> </task>
<task id="LOCAL-00001" summary="Initial commit">
<created>1679400277116</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1679400277116</updated>
</task>
<option name="localTasksCounter" value="2" />
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">
...@@ -49,4 +138,8 @@ ...@@ -49,4 +138,8 @@
</map> </map>
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration">
<MESSAGE value="Initial commit" />
<option name="LAST_COMMIT_MESSAGE" value="Initial commit" />
</component>
</project> </project>
\ No newline at end of file
# Parallel Programming HW1
> Important Note: Please make sure to change JVM memory heap settings to enable making an array of a billion elements
\ No newline at end of file
...@@ -7,6 +7,14 @@ ...@@ -7,6 +7,14 @@
<groupId>org.example</groupId> <groupId>org.example</groupId>
<artifactId>ArrayOccurancesCount</artifactId> <artifactId>ArrayOccurancesCount</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
......
package org.example;
import java.util.Arrays;
import java.util.concurrent.RecursiveAction;
public class ArrayOccurrencesCount extends RecursiveAction {
public long occurrences;
int arr[], lo, hi;
int targetElement;
public ArrayOccurrencesCount(int[] arr, int lo, int hi, int targetElement) {
this.arr = arr;
this.lo = lo;
this.hi = hi;
this.targetElement = targetElement;
}
public long computeSeq(){
for (int i = lo; i <= hi; ++i) {
if(arr[i] == targetElement) occurrences++;
}
return occurrences;
}
@Override
protected void compute() {
if (hi - lo > 1_000_000) {
int mid = (lo + hi) / 2;
ArrayOccurrencesCount left = new ArrayOccurrencesCount(arr, lo, mid, targetElement);
ArrayOccurrencesCount right = new ArrayOccurrencesCount(arr, mid + 1, hi, targetElement);
left.fork();
right.compute();
left.join();
occurrences = left.occurrences + right.occurrences;
} else {
occurrences =computeSeq();
}
}
public void computeStreamSeq() {
occurrences = Arrays.stream(arr).asLongStream().reduce(0, (acc, elm) -> acc + (elm == targetElement ? 1 : 0));
}
public void computeStreamPP() {
occurrences = Arrays.stream(arr).asLongStream().parallel().reduce(0, (acc, elm) -> acc + (elm == targetElement ? 1 : 0));
}
}
package org.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
import junit.framework.TestCase;
import org.example.ArrayOccurrencesCount;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.IntStream;
public class ArrayOccurrencesCountTest extends TestCase {
private int[] randomArray(int size) {
Random rand = new Random();
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = rand.nextInt(size);
}
return arr;
}
private int[] intStream(int size) {
IntStream intStream = IntStream.range(1, size);
return intStream.toArray();
}
public void testArrayOccurrencesCountSeq() {
// int size = 1000_000_000;
int size = 100000000;
int target = 5;
// int[] arr = randomArray(size);
int[] arr = intStream(size);
ArrayOccurrencesCount array = new ArrayOccurrencesCount(arr, 0, arr.length - 1, target);
long start = System.currentTimeMillis();
long occurrences = array.computeSeq();
long endTimer = System.currentTimeMillis() - start;
System.out.printf("Sequential Time execution for Random Array of size %d is %d ms number of occurrences is %d\n", size, endTimer, occurrences);
}
public void testArrayOccurrencesCountPP() {
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism","7");
// int size = 1000_000_000;
int size = 100000000;
// int[] arr = randomArray(size);
int[] arr = intStream(size);
int target = 5;
ArrayOccurrencesCount occurrences = new ArrayOccurrencesCount(arr, 0, arr.length - 1, target);
long start = System.currentTimeMillis();
ForkJoinPool.commonPool().invoke(occurrences);
long endTimer = System.currentTimeMillis() - start;
System.out.printf("Parallel Time execution for Random Array of size %d is %d ms sum is %d\n", size, endTimer, occurrences.occurrences);
}
public void testArrayOccurrencesCountStreamSeq() {
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism","7");
// int size = 1000_000_000;
int size = 100000000;
// int[] arr = randomArray(size);
int[] arr = intStream(size);
int target = 5;
ArrayOccurrencesCount occurrences = new ArrayOccurrencesCount(arr, 0, arr.length - 1, target);
long start = System.currentTimeMillis();
occurrences.computeStreamSeq();
long endTimer = System.currentTimeMillis() - start;
System.out.printf("Parallel Stream Time execution for Random Array of size %d is %d ms sum is %d\n", size, endTimer, occurrences.occurrences);
}
public void testArrayOccurrencesCountStreamPP() {
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism","7");
// int size = 1000_000_000;
int size = 100000000;
// int[] arr = randomArray(size);
int[] arr = intStream(size);
int target = 5;
ArrayOccurrencesCount occurrences = new ArrayOccurrencesCount(arr, 0, arr.length - 1, target);
long start = System.currentTimeMillis();
occurrences.computeStreamPP();
long endTimer = System.currentTimeMillis() - start;
System.out.printf("Parallel Stream Time execution for Random Array of size %d is %d ms sum is %d\n", size, endTimer, occurrences.occurrences);
}
}
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