Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
Parallel Programming Lab 2
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
julanar.ali
Parallel Programming Lab 2
Commits
bd36be5a
You need to sign in or sign up before continuing.
Commit
bd36be5a
authored
Nov 14, 2023
by
julanar.ali
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
writing comments
parent
4a6e9301
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
53 additions
and
4 deletions
+53
-4
main.java
src/main/java/main.java
+1
-1
ParallelCounter0.java
src/main/java/parallelCounters/ParallelCounter0.java
+8
-0
ParallelCounter1.java
src/main/java/parallelCounters/ParallelCounter1.java
+8
-0
ParallelCounter2.java
src/main/java/parallelCounters/ParallelCounter2.java
+11
-1
ParallelCounter3.java
src/main/java/parallelCounters/ParallelCounter3.java
+13
-1
ParallelCounter4.java
src/main/java/parallelCounters/ParallelCounter4.java
+12
-1
main.class
target/classes/main.class
+0
-0
No files found.
src/main/java/main.java
View file @
bd36be5a
...
...
@@ -4,7 +4,7 @@ import parallelSummers.*;
public
class
main
{
public
static
void
main
(
String
[]
args
)
{
int
size
=
10
;
int
size
=
10
0
;
int
[]
data
=
TestData
.
createData
(
size
);
long
counterResult
=
SequentialCounter
.
primesCount
(
data
);
int
threadCount
=
8
;
...
...
src/main/java/parallelCounters/ParallelCounter0.java
View file @
bd36be5a
...
...
@@ -14,16 +14,23 @@ import summers.SummerThread;
import
worker.WorkPartitioner
;
import
worker.WorkPartitioner.Part
;
//this class calculates the number of primes in [0 , size-1] using CounterThread
public
class
ParallelCounter0
{
public
static
long
primesCount
(
int
[]
data
,
int
threadCount
)
{
List
<
CounterThread
>
counters
=
new
ArrayList
<>();
//partitioning the data to assign each part to a counter to calculate the number of primes
List
<
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
//assigning each part to counter thread
for
(
Part
part
:
parts
)
{
counters
.
add
(
new
CounterThread
(
data
,
part
));
}
//starting threads to calculate the number of primes
for
(
CounterThread
counterThread
:
counters
)
{
counterThread
.
start
();
}
...
...
@@ -36,6 +43,7 @@ public class ParallelCounter0{
}
}
//defining a variable to hold the total number of primes
long
count
=
0
;
for
(
CounterThread
counterThread
:
counters
)
{
count
+=
counterThread
.
getCount
();
...
...
src/main/java/parallelCounters/ParallelCounter1.java
View file @
bd36be5a
...
...
@@ -10,15 +10,22 @@ import java.util.List;
//this class calculates the number of primes in [0 , size-1] using CounterRunnable which implements the Runnable Interface
public
class
ParallelCounter1
{
public
static
long
primesCount
(
int
[]
data
,
int
threadCount
)
{
List
<
CounterRunnable
>
counters
=
new
ArrayList
<>();
//partitioning the data to assign each part to a counter to calculate the number of primes
List
<
WorkPartitioner
.
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
//assigning each part to counter runnable
for
(
WorkPartitioner
.
Part
part
:
parts
)
{
counters
.
add
(
new
CounterRunnable
(
data
,
part
));
}
//starting the threads
for
(
CounterRunnable
counterRunnable
:
counters
)
{
counterRunnable
.
startThread
();
}
...
...
@@ -31,6 +38,7 @@ public class ParallelCounter1 {
}
}
//defining a variable to hold the total number of primes
long
count
=
0
;
for
(
CounterRunnable
counterRunnable
:
counters
)
{
count
+=
counterRunnable
.
getCount
();
...
...
src/main/java/parallelCounters/ParallelCounter2.java
View file @
bd36be5a
...
...
@@ -12,26 +12,36 @@ import java.util.List;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
//this class calculates the number of primes in [0 , size-1]
// using ExecutorService with fixed thread pool of threadCount threads, and with counter thread
public
class
ParallelCounter2
{
public
static
long
primesCount
(
int
[]
data
,
int
threadCount
)
{
List
<
CounterThread
>
counters
=
new
ArrayList
<>();
//partitioning the data to assign each part to a counter to calculate the number of primes
List
<
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
//assigning each part to counter thread
for
(
Part
part
:
parts
)
{
counters
.
add
(
new
CounterThread
(
data
,
part
));
}
//defining an executor with fixed thread pool to make each thread calculates a number of primes in its own part
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadCount
);
//executing the threads
for
(
CounterThread
counter
:
counters
)
{
executor
.
execute
(
counter
);
}
//waiting until the current thread finishing its work and then shutting it down
executor
.
shutdown
();
//Waiting for all the thread to finalize
while
(!
executor
.
isTerminated
())
{
//System.out.println("Processing....");
}
//returning the total number of primes
return
counters
.
stream
().
mapToLong
(
CounterThread:
:
getCount
).
sum
();
}
...
...
src/main/java/parallelCounters/ParallelCounter3.java
View file @
bd36be5a
...
...
@@ -12,29 +12,41 @@ import java.util.concurrent.Future;
import
worker.WorkPartitioner.Part
;
//this class calculates the number of primes in [0 , size-1]
// using ExecutorService with fixed thread pool of threadCount threads,
// and with CounterCallable which implements the Callable interface, by invoking all callables
public
class
ParallelCounter3
{
public
static
long
primesCount
(
int
[]
data
,
int
threadCount
)
{
//creating a
pool of #
threadCount threads
//creating a
fixed pool of
threadCount threads
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadCount
);
List
<
CounterCallable
>
counters
=
new
ArrayList
<
CounterCallable
>();
//partitioning the data to assign each part to a counter to calculate the number of primes
List
<
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
//assigning each part to counter callable
for
(
Part
part
:
parts
)
{
counters
.
add
(
new
CounterCallable
(
data
,
part
));
}
//the result that returning from call function in callable is returned as a future
// because the submit function doesn't wait until the task completes, so the executor service can't return the
//callable directly, instead it returns the result type Future, which can be retrieved later
List
<
Future
<
Long
>>
results
;
try
{
// the invokeAll function allows us to submit many callables at once, it accepts a collection of callable,
//and returning a list of futures
results
=
executor
.
invokeAll
(
counters
);
}
catch
(
InterruptedException
e
)
{
System
.
err
.
println
(
"Cannot invoke the threads."
);
return
-
1
;
}
//waiting until the current thread finishing its work and then shutting it down
executor
.
shutdown
();
long
count
=
0
;
...
...
src/main/java/parallelCounters/ParallelCounter4.java
View file @
bd36be5a
...
...
@@ -11,7 +11,9 @@ import java.util.concurrent.ExecutorService;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
//this class calculates the number of primes in [0 , size-1]
// using ExecutorService with fixed thread pool of threadCount threads,
// and with CounterCallable which implements the Callable interface, by using submit function
public
class
ParallelCounter4
{
...
...
@@ -19,13 +21,21 @@ public class ParallelCounter4 {
//creating a pool of 5 threads
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadCount
);
//the result that returning from call function in callable is returned as a type Future
// because the submit function doesn't wait until the task completes, so the executor service can't return the
//callable directly, instead it returns the result type Future, which can be retrieved later
List
<
Future
<
Long
>>
results
=
new
ArrayList
<
Future
<
Long
>>();
//partitioning the data to assign each part to a counter to calculate the number of primes
List
<
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
//assigning each part to counter callable
for
(
Part
part
:
parts
)
{
results
.
add
(
executor
.
submit
(
new
CounterCallable
(
data
,
part
)));
}
//waiting until the current thread finishing its work and then shutting it down
executor
.
shutdown
();
while
(!
executor
.
isTerminated
())
{
...
...
@@ -33,6 +43,7 @@ public class ParallelCounter4 {
}
long
count
=
0
;
for
(
Future
<
Long
>
future
:
results
)
{
try
{
count
+=
future
.
get
();
...
...
target/classes/main.class
View file @
bd36be5a
No preview for this file type
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