Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
PP - HW1
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
moataz.dbaneh
PP - HW1
Commits
b508e72a
Commit
b508e72a
authored
Mar 21, 2023
by
MoutazDebbaneh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add first question solution
parent
82400e40
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
252 additions
and
15 deletions
+252
-15
workspace.xml
.idea/workspace.xml
+101
-8
README.md
README.md
+3
-0
pom.xml
pom.xml
+8
-0
ArrayOccurrencesCount.java
src/main/java/org/example/ArrayOccurrencesCount.java
+49
-0
Main.java
src/main/java/org/example/Main.java
+0
-7
ArrayOccurrencesCountTest.java
src/test/java/ArrayOccurrencesCountTest.java
+91
-0
No files found.
.idea/workspace.xml
View file @
b508e72a
...
...
@@ -4,29 +4,110 @@
<option
name=
"autoReloadType"
value=
"SELECTIVE"
/>
</component>
<component
name=
"ChangeListManager"
>
<list
default=
"true"
id=
"39d1573e-bc8d-4759-8e1b-31b85df143ea"
name=
"Changes"
comment=
""
>
<change
afterPath=
"$PROJECT_DIR$/pom.xml"
afterDir=
"false"
/>
<list
default=
"true"
id=
"39d1573e-bc8d-4759-8e1b-31b85df143ea"
name=
"Changes"
comment=
"Initial commit"
>
<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>
<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=
"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=
"MarkdownSettingsMigration"
>
<option
name=
"stateVersion"
value=
"1"
/>
</component>
<component
name=
"ProjectId"
id=
"2NK5ztYDvTO4MIiuysgyKXIMn9e"
/>
<component
name=
"ProjectLevelVcsManager"
settingsEditedManually=
"true"
/>
<component
name=
"ProjectLevelVcsManager"
settingsEditedManually=
"true"
>
<ConfirmationsSetting
value=
"2"
id=
"Add"
/>
</component>
<component
name=
"ProjectViewState"
>
<option
name=
"hideEmptyMiddlePackages"
value=
"true"
/>
<option
name=
"showLibraryContents"
value=
"true"
/>
</component>
<component
name=
"PropertiesComponent"
>
<![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true"
<component
name=
"PropertiesComponent"
>
{
"
keyToString
"
: {
"
RunOnceActivity.OpenProjectViewOnStart
"
:
"
true
"
,
"
RunOnceActivity.ShowReadmeOnStart
"
:
"
true
"
,
"
project.structure.last.edited
"
:
"
Problems
"
,
"
project.structure.proportion
"
:
"
0.15
"
,
"
project.structure.side.proportion
"
:
"
0.2
"
}
}]]>
</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=
"TaskManager"
>
<task
active=
"true"
id=
"Default"
summary=
"Default task"
>
...
...
@@ -36,6 +117,14 @@
<option
name=
"presentableId"
value=
"Default"
/>
<updated>
1679400203183
</updated>
</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
/>
</component>
<component
name=
"Vcs.Log.Tabs.Properties"
>
...
...
@@ -49,4 +138,8 @@
</map>
</option>
</component>
<component
name=
"VcsManagerConfiguration"
>
<MESSAGE
value=
"Initial commit"
/>
<option
name=
"LAST_COMMIT_MESSAGE"
value=
"Initial commit"
/>
</component>
</project>
\ No newline at end of file
README.md
0 → 100644
View file @
b508e72a
# 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
pom.xml
View file @
b508e72a
...
...
@@ -7,6 +7,14 @@
<groupId>
org.example
</groupId>
<artifactId>
ArrayOccurancesCount
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.13.2
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>
17
</maven.compiler.source>
...
...
src/main/java/org/example/ArrayOccurrencesCount.java
0 → 100644
View file @
b508e72a
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
));
}
}
src/main/java/org/example/Main.java
deleted
100644 → 0
View file @
82400e40
package
org
.
example
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Hello world!"
);
}
}
\ No newline at end of file
src/test/java/ArrayOccurrencesCountTest.java
0 → 100644
View file @
b508e72a
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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment