HW2: all done including bonus

This commit is contained in:
Claudio Maggioni 2019-11-11 00:36:16 +01:00
parent 171a7aa764
commit ffd8fbf895
10 changed files with 477 additions and 43 deletions

View file

@ -1,17 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="19fb80a5-3326-4689-b0ba-ecd6ab32d599" name="Default Changelist" comment=""> <list default="true" id="19fb80a5-3326-4689-b0ba-ecd6ab32d599" name="Default Changelist" comment="" />
<change afterPath="$PROJECT_DIR$/.idea/Ex2.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/ConcurrentVoteCounter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Tester.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/VoteCounter.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../Ex1/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../Ex1/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../submission.tex" beforeDir="false" afterPath="$PROJECT_DIR$/../submission.tex" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -26,8 +16,8 @@
<file pinned="false" current-in-tab="true"> <file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/ConcurrentVoteCounter.java"> <entry file="file://$PROJECT_DIR$/src/ConcurrentVoteCounter.java">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="165"> <state relative-caret-position="135">
<caret line="11" column="64" selection-start-line="11" selection-start-column="64" selection-end-line="11" selection-end-column="64" /> <caret line="9" column="64" lean-forward="true" selection-start-line="9" selection-start-column="64" selection-end-line="9" selection-end-column="64" />
<folding> <folding>
<element signature="imports" expanded="true" /> <element signature="imports" expanded="true" />
<element signature="e#454#455#0" expanded="true" /> <element signature="e#454#455#0" expanded="true" />
@ -164,12 +154,12 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1573415904376</updated> <updated>1573415904376</updated>
<workItem from="1573415905557" duration="151000" /> <workItem from="1573415905557" duration="151000" />
<workItem from="1573416086677" duration="1451000" /> <workItem from="1573416086677" duration="1590000" />
</task> </task>
<servers /> <servers />
</component> </component>
<component name="TimeTrackingManager"> <component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="1602000" /> <option name="totallyTimeSpent" value="1741000" />
</component> </component>
<component name="ToolWindowManager"> <component name="ToolWindowManager">
<frame x="0" y="0" width="1680" height="1050" extended-state="0" /> <frame x="0" y="0" width="1680" height="1050" extended-state="0" />
@ -186,7 +176,7 @@
<window_info anchor="bottom" id="Messages" weight="0.32959184" /> <window_info anchor="bottom" id="Messages" weight="0.32959184" />
<window_info anchor="bottom" id="Message" order="0" /> <window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32959184" /> <window_info anchor="bottom" id="Find" order="1" weight="0.32959184" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.32959184" /> <window_info anchor="bottom" id="Run" order="2" weight="0.32959184" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" /> <window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" /> <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" /> <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@ -231,8 +221,8 @@
</entry> </entry>
<entry file="file://$PROJECT_DIR$/src/ConcurrentVoteCounter.java"> <entry file="file://$PROJECT_DIR$/src/ConcurrentVoteCounter.java">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="165"> <state relative-caret-position="135">
<caret line="11" column="64" selection-start-line="11" selection-start-column="64" selection-end-line="11" selection-end-column="64" /> <caret line="9" column="64" lean-forward="true" selection-start-line="9" selection-start-column="64" selection-end-line="9" selection-end-column="64" />
<folding> <folding>
<element signature="imports" expanded="true" /> <element signature="imports" expanded="true" />
<element signature="e#454#455#0" expanded="true" /> <element signature="e#454#455#0" expanded="true" />

11
hw2/Ex3/.idea/Ex3.iml Normal file
View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

9
hw2/Ex3/.idea/misc.xml Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_12" default="false" project-jdk-name="12.0.1" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Ex3.iml" filepath="$PROJECT_DIR$/.idea/Ex3.iml" />
</modules>
</component>
</project>

6
hw2/Ex3/.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
</component>
</project>

386
hw2/Ex3/.idea/workspace.xml Normal file
View file

@ -0,0 +1,386 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="b6c476d3-708a-4f72-b206-a0dd033057e4" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/parallel/StreamsPrimeCounter.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../Ex2/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../Ex2/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/parallel/ForkJoinPoolPrimeCounter.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/parallel/ForkJoinPoolPrimeCounter.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/parallel/ThreadPoolPrimeCounter.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/parallel/ThreadPoolPrimeCounter.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/Tester.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/Tester.java" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="DefaultGradleProjectSettings">
<option name="testRunner" value="GRADLE" />
<option name="delegatedBuild" value="true" />
</component>
<component name="FileEditorManager">
<leaf>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/test/Tester.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="370">
<caret line="68" lean-forward="true" selection-start-line="68" selection-end-line="68" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/parallel/StreamsPrimeCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="105">
<caret line="7" column="20" selection-start-line="7" selection-start-column="20" selection-end-line="7" selection-end-column="20" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/parallel/ForkJoinPoolPrimeCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="165">
<caret line="17" column="40" selection-start-line="17" selection-start-column="40" selection-end-line="17" selection-end-column="40" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/parallel/CallablePrimeCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="255">
<caret line="24" column="15" lean-forward="true" selection-start-line="24" selection-start-column="15" selection-end-line="24" selection-end-column="15" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/parallel/ThreadPoolPrimeCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="240">
<caret line="22" column="24" selection-start-line="22" selection-start-column="24" selection-end-line="22" selection-end-column="24" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/sequential/SequentialPrimeCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="60">
<caret line="5" column="13" selection-start-line="5" selection-start-column="13" selection-end-line="5" selection-end-column="13" />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../.." />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/src/parallel/ThreadPoolPrimeCounter.java" />
<option value="$PROJECT_DIR$/src/parallel/ForkJoinPoolPrimeCounter.java" />
<option value="$PROJECT_DIR$/src/parallel/StreamPrimeCounter.java" />
<option value="$PROJECT_DIR$/src/test/Tester.java" />
</list>
</option>
</component>
<component name="ProjectConfigurationFiles">
<option name="files">
<list>
<option value="$PROJECT_DIR$/.idea/Ex3.iml" />
<option value="$PROJECT_DIR$/.idea/misc.xml" />
<option value="$PROJECT_DIR$/.idea/vcs.xml" />
<option value="$PROJECT_DIR$/.idea/modules.xml" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" fullScreen="true">
<option name="width" value="1680" />
<option name="height" value="1050" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="PackagesPane" />
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="Ex3" type="b2602c69:ProjectViewProjectNode" />
<item name="Ex3" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Ex3" type="b2602c69:ProjectViewProjectNode" />
<item name="Ex3" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Ex3" type="b2602c69:ProjectViewProjectNode" />
<item name="Ex3" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
<item name="parallel" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Ex3" type="b2602c69:ProjectViewProjectNode" />
<item name="Ex3" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
<item name="sequential" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Ex3" type="b2602c69:ProjectViewProjectNode" />
<item name="Ex3" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
<item name="test" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Ex3" type="b2602c69:ProjectViewProjectNode" />
<item name="Ex3" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
<item name="util" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="Scope" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_npm_path_reset_for_default_project" value="true" />
<property name="project.structure.last.edited" value="Modules" />
<property name="project.structure.proportion" value="0.15" />
<property name="project.structure.side.proportion" value="0.2" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration name="Tester" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="test.Tester" />
<module name="Ex3" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="test.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Tester" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="b6c476d3-708a-4f72-b206-a0dd033057e4" name="Default Changelist" comment="" />
<created>1573424652116</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1573424652116</updated>
<workItem from="1573424653239" duration="3423000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="3423000" />
</component>
<component name="ToolWindowManager">
<frame x="0" y="0" width="1680" height="1050" extended-state="0" />
<editor active="true" />
<layout>
<window_info id="Designer" />
<window_info id="Image Layers" />
<window_info id="Capture Tool" />
<window_info id="UI Designer" />
<window_info id="Favorites" side_tool="true" />
<window_info id="Learn" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.24969475" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info anchor="bottom" id="Terminal" />
<window_info anchor="bottom" id="Docker" show_stripe_button="false" />
<window_info anchor="bottom" id="Event Log" side_tool="true" />
<window_info anchor="bottom" id="Database Changes" />
<window_info anchor="bottom" id="Version Control" />
<window_info anchor="bottom" id="Messages" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.32959184" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="right" id="Maven" />
<window_info anchor="right" id="Palette" />
<window_info anchor="right" id="Capture Analysis" />
<window_info anchor="right" id="Database" />
<window_info anchor="right" id="Palette&#9;" />
<window_info anchor="right" id="Theme Preview" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/src/parallel/ThreadPoolPrimeCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="240">
<caret line="22" column="24" selection-start-line="22" selection-start-column="24" selection-end-line="22" selection-end-column="24" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/parallel/CallablePrimeCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="255">
<caret line="24" column="15" lean-forward="true" selection-start-line="24" selection-start-column="15" selection-end-line="24" selection-end-column="15" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/sequential/SequentialPrimeCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="60">
<caret line="5" column="13" selection-start-line="5" selection-start-column="13" selection-end-line="5" selection-end-column="13" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/parallel/ForkJoinPoolPrimeCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="165">
<caret line="17" column="40" selection-start-line="17" selection-start-column="40" selection-end-line="17" selection-end-column="40" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/parallel/StreamsPrimeCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="105">
<caret line="7" column="20" selection-start-line="7" selection-start-column="20" selection-end-line="7" selection-end-column="20" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/test/Tester.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="370">
<caret line="68" lean-forward="true" selection-start-line="68" selection-end-line="68" />
</state>
</provider>
</entry>
</component>
<component name="masterDetails">
<states>
<state key="ArtifactsStructureConfigurable.UI">
<settings>
<artifact-editor />
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="FacetStructureConfigurable.UI">
<settings>
<last-edited>No facets are configured</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="GlobalLibrariesConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="JdkListConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ModuleStructureConfigurable.UI">
<settings>
<last-edited>Ex3</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
<option value="0.6" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ProjectLibrariesConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

View file

@ -4,6 +4,7 @@ import util.Prime;
import util.PrimeCounter; import util.PrimeCounter;
import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask; import java.util.concurrent.RecursiveTask;
import java.util.concurrent.ThreadPoolExecutor;
public class ForkJoinPoolPrimeCounter implements PrimeCounter { public class ForkJoinPoolPrimeCounter implements PrimeCounter {
@ -15,10 +16,8 @@ public class ForkJoinPoolPrimeCounter implements PrimeCounter {
} }
public int countPrimes(int[] nums) { public int countPrimes(int[] nums) {
int count = 0; final ForkJoinPool p = new ForkJoinPool();
// TODO: Complete method countPrimes return p.invoke(new CountPrimes(nums, 0, nums.length - 1));
return count;
} }
private static class CountPrimes extends RecursiveTask<Integer> { private static class CountPrimes extends RecursiveTask<Integer> {
@ -47,15 +46,14 @@ public class ForkJoinPoolPrimeCounter implements PrimeCounter {
return count; return count;
} }
else { else {
int right, left, rightAns, leftAns; final int mid = low + (high - low) / 2;
right = left = rightAns = leftAns = 0;
right = low + ((int) Math.floor((high - low) / 2 ) - 1);
left = low + ((int) Math.ceil((high - low) / 2 ));
// TODO: Complete method compute
final CountPrimes leftTask = new CountPrimes(nums, low, mid);
final CountPrimes rightTask = new CountPrimes(nums, mid + 1, high);
rightTask.fork();
return leftAns + rightAns; return leftTask.compute() + rightTask.join();
} }
} }
} }

View file

@ -0,0 +1,14 @@
package parallel;
import util.Prime;
import util.PrimeCounter;
import java.util.Arrays;
public class StreamsPrimeCounter implements PrimeCounter {
@Override
public int countPrimes(int[] nums) throws Exception {
return Arrays.stream(nums).parallel().map(n -> Prime.isPrime(n) ? 1 : 0).sum();
}
}

View file

@ -4,11 +4,7 @@ import util.Prime;
import java.util.List; import java.util.List;
import util.PrimeCounter; import util.PrimeCounter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.Future; import java.util.concurrent.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class ThreadPoolPrimeCounter implements PrimeCounter { public class ThreadPoolPrimeCounter implements PrimeCounter {
@ -19,9 +15,20 @@ public class ThreadPoolPrimeCounter implements PrimeCounter {
} }
public int countPrimes(int[] nums) throws Exception { public int countPrimes(int[] nums) throws Exception {
int count = 0; final ExecutorService e = Executors.newFixedThreadPool(numThreads);
// TODO: Complete method countPrimes final int step = (int) Math.ceil((double) nums.length / numThreads);
final List<Future<Integer>> results = new ArrayList<>(numThreads);
for (int i = 0; i < nums.length; i += step) {
results.add(e.submit(new CountPrimes(nums, i, i + step)));
}
e.shutdown();
int count = 0;
for (Future<Integer> result : results) {
count += result.get();
}
return count; return count;
} }

View file

@ -1,6 +1,10 @@
package test; package test;
import parallel.StreamsPrimeCounter;
import util.PrimeCounter; import util.PrimeCounter;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import parallel.CallablePrimeCounter; import parallel.CallablePrimeCounter;
import parallel.ThreadPoolPrimeCounter; import parallel.ThreadPoolPrimeCounter;
@ -13,22 +17,26 @@ public class Tester {
public static final int LOW = 2; public static final int LOW = 2;
public static final int HIGH = 11201609; public static final int HIGH = 11201609;
private PrimeCounter strategy; private PrimeCounter strategy;
private static Map<int[], Long> checkMap = new HashMap<>();
private void setStrategy(PrimeCounter strategy) { private void setStrategy(PrimeCounter strategy) {
this.strategy = strategy; this.strategy = strategy;
} }
private int countPrimes(int[] array) { private long countPrimes(int[] array) {
return new SequentialPrimeCounter().countPrimes(array); return new SequentialPrimeCounter().countPrimes(array);
} }
private void testImplementation(int[] array, String name) private void testImplementation(int[] array, String name)
throws Exception { throws Exception {
checkMap.computeIfAbsent(array, k -> countPrimes(array));
long result = checkMap.get(array);
long startTime = System.nanoTime(); long startTime = System.nanoTime();
int count = strategy.countPrimes(array); int count = strategy.countPrimes(array);
long endTime = System.nanoTime(); long endTime = System.nanoTime();
long result = countPrimes(array);
if (count != result) { if (count != result) {
// Informing if the implementation failed // Informing if the implementation failed
System.err.printf("- ERROR: %s; Expected: %d; Found: %d\n", System.err.printf("- ERROR: %s; Expected: %d; Found: %d\n",
@ -55,8 +63,6 @@ public class Tester {
tester.setStrategy(new CallablePrimeCounter(NUM_THREADS)); tester.setStrategy(new CallablePrimeCounter(NUM_THREADS));
tester.testImplementation(numbers, "Parallel - Callable version"); tester.testImplementation(numbers, "Parallel - Callable version");
// TODO: Uncomment to test additional implementations
/*
// Parallel - ThreadPool implementation // Parallel - ThreadPool implementation
tester.setStrategy(new ThreadPoolPrimeCounter(NUM_THREADS)); tester.setStrategy(new ThreadPoolPrimeCounter(NUM_THREADS));
tester.testImplementation(numbers, "Parallel - ThreadPool version"); tester.testImplementation(numbers, "Parallel - ThreadPool version");
@ -66,8 +72,7 @@ public class Tester {
tester.testImplementation(numbers, "Parallel - ForkJoinPool version"); tester.testImplementation(numbers, "Parallel - ForkJoinPool version");
// Parallel - Streams implementation - BONUS // Parallel - Streams implementation - BONUS
//tester.setStrategy(new StreamsPrimeCounter()); tester.setStrategy(new StreamsPrimeCounter());
//tester.testImplementation(numbers, "Parallel - Streams version"); tester.testImplementation(numbers, "Parallel - Streams version");
*/
} }
} }