Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
homework_parallel
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
roset.alwzrah
homework_parallel
Commits
8bca5127
Commit
8bca5127
authored
Mar 21, 2023
by
roset.alwzrah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
calculate fibonacci number
parent
cb8822d8
Pipeline
#179
canceled with stages
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
92 additions
and
9 deletions
+92
-9
workspace.xml
.idea/workspace.xml
+16
-9
Fibonacci.java
src/main/java/Service/Fibonacci.java
+50
-0
FibonacciTest.java
src/test/java/FibonacciTest.java
+26
-0
No files found.
.idea/workspace.xml
View file @
8bca5127
...
@@ -4,15 +4,10 @@
...
@@ -4,15 +4,10 @@
<option
name=
"autoReloadType"
value=
"SELECTIVE"
/>
<option
name=
"autoReloadType"
value=
"SELECTIVE"
/>
</component>
</component>
<component
name=
"ChangeListManager"
>
<component
name=
"ChangeListManager"
>
<list
default=
"true"
id=
"bd82bf16-4018-4075-b7d4-6d792f3cd58b"
name=
"Changes"
comment=
""
>
<list
default=
"true"
id=
"bd82bf16-4018-4075-b7d4-6d792f3cd58b"
name=
"Changes"
comment=
"Find the number of occurrences of a number in an array"
>
<change
afterPath=
"$PROJECT_DIR$/.gitignore"
afterDir=
"false"
/>
<change
afterPath=
"$PROJECT_DIR$/src/main/java/Service/Fibonacci.java"
afterDir=
"false"
/>
<change
afterPath=
"$PROJECT_DIR$/.idea/encodings.xml"
afterDir=
"false"
/>
<change
afterPath=
"$PROJECT_DIR$/src/test/java/FibonacciTest.java"
afterDir=
"false"
/>
<change
afterPath=
"$PROJECT_DIR$/.idea/misc.xml"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/.idea/workspace.xml"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/.idea/workspace.xml"
afterDir=
"false"
/>
<change
afterPath=
"$PROJECT_DIR$/.idea/vcs.xml"
afterDir=
"false"
/>
<change
afterPath=
"$PROJECT_DIR$/.idea/workspace.xml"
afterDir=
"false"
/>
<change
afterPath=
"$PROJECT_DIR$/pom.xml"
afterDir=
"false"
/>
<change
afterPath=
"$PROJECT_DIR$/src/main/java/Service/NumOfOccurrences.java"
afterDir=
"false"
/>
<change
afterPath=
"$PROJECT_DIR$/src/test/java/NumOfOccurrencesTest.java"
afterDir=
"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"
/>
...
@@ -54,6 +49,14 @@
...
@@ -54,6 +49,14 @@
<option
name=
"presentableId"
value=
"Default"
/>
<option
name=
"presentableId"
value=
"Default"
/>
<updated>
1679420459839
</updated>
<updated>
1679420459839
</updated>
</task>
</task>
<task
id=
"LOCAL-00001"
summary=
"Find the number of occurrences of a number in an array"
>
<created>
1679420949412
</created>
<option
name=
"number"
value=
"00001"
/>
<option
name=
"presentableId"
value=
"LOCAL-00001"
/>
<option
name=
"project"
value=
"LOCAL"
/>
<updated>
1679420949412
</updated>
</task>
<option
name=
"localTasksCounter"
value=
"2"
/>
<servers
/>
<servers
/>
</component>
</component>
<component
name=
"Vcs.Log.Tabs.Properties"
>
<component
name=
"Vcs.Log.Tabs.Properties"
>
...
@@ -67,4 +70,8 @@
...
@@ -67,4 +70,8 @@
</map>
</map>
</option>
</option>
</component>
</component>
<component
name=
"VcsManagerConfiguration"
>
<MESSAGE
value=
"Find the number of occurrences of a number in an array"
/>
<option
name=
"LAST_COMMIT_MESSAGE"
value=
"Find the number of occurrences of a number in an array"
/>
</component>
</project>
</project>
\ No newline at end of file
src/main/java/Service/Fibonacci.java
0 → 100644
View file @
8bca5127
package
Service
;
import
java.util.HashMap
;
import
java.util.Map
;
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
<
Integer
>
{
final
int
n
;
public
static
HashMap
<
Integer
,
Integer
>
fibValues
=
new
HashMap
<
Integer
,
Integer
>();
/////
public
Fibonacci
(
int
n
)
{
this
.
n
=
n
;
}
public
Integer
compute
()
{
if
(
n
>
20
)
{
if
(
n
<=
1
)
return
n
;
Fibonacci
f1
=
new
Fibonacci
(
n
-
1
);
f1
.
fork
();
Fibonacci
f2
=
new
Fibonacci
(
n
-
2
);
return
f2
.
compute
()
+
f1
.
join
();
}
else
{
return
computeSeq
();
}
}
public
Integer
computeSeq
()
{
if
(
n
<=
1
)
return
n
;
else
if
(
fibValues
.
containsKey
(
n
))
return
fibValues
.
get
(
n
);
else
{
Fibonacci
f1
=
new
Fibonacci
(
n
-
1
);
Fibonacci
f2
=
new
Fibonacci
(
n
-
2
);
int
value
=
f2
.
computeSeq
()
+
f1
.
computeSeq
();
fibValues
.
put
(
n
,
value
);
return
value
;
}
}
}
src/test/java/FibonacciTest.java
0 → 100644
View file @
8bca5127
import
Service.Fibonacci
;
import
junit.framework.TestCase
;
public
class
FibonacciTest
extends
TestCase
{
int
n
=
40
;
public
void
testFiboPP
(){
long
start
=
System
.
currentTimeMillis
();
Fibonacci
fib
=
new
Fibonacci
(
n
);
int
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
(){
long
start
=
System
.
currentTimeMillis
();
Fibonacci
fib
=
new
Fibonacci
(
n
);
int
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
}
}
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