Commit 4a6e9301 authored by julanar.ali's avatar julanar.ali

implementing parallel counters 1-4

parent 71cd0095
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
...@@ -6,12 +6,12 @@ import worker.Worker; ...@@ -6,12 +6,12 @@ import worker.Worker;
public class Counter extends Worker { public class Counter extends Worker {
protected long sum; protected long count;
public Counter(int[] data, Part part) { public Counter(int[] data, Part part) {
super(data, part); super(data, part);
} }
public long getSum() { public long getCount() {
return sum; return count;
} }
} }
package counters; package counters;
import PrimerFinder.PrimerFinder;
import worker.WorkPartitioner.Part; import worker.WorkPartitioner.Part;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
public class CounterCallable extends Counter implements Callable<Long> { public class CounterCallable extends Counter implements Callable<Long> {
public CounterCallable(int[] data,Part part) { public CounterCallable(int[] data, Part part) {
super(data, part); super(data, part);
} }
@Override @Override
public Long call() throws Exception { public Long call() throws Exception {
this.sum = 0; count = 0;
List<Integer> primes = new ArrayList<>();
PrimerFinder primerFinder = new PrimerFinder(start, finish, primes);
for (int i = start; i < finish; i++) { for (int i = start; i < finish; i++) {
sum += data[i]; if (primerFinder.isPrime(i)) {
synchronized (primerFinder.getPrimeNumList()) {
primerFinder.getPrimeNumList().add(i);
count++;
}
}
} }
return sum; return count;
} }
} }
\ No newline at end of file
package counters; package counters;
import PrimerFinder.PrimerFinder;
import summers.Summer; import summers.Summer;
import worker.WorkPartitioner; import worker.WorkPartitioner;
import java.util.ArrayList;
import java.util.List;
public class CounterRunnable extends Counter implements Runnable{ public class CounterRunnable extends Counter implements Runnable{
private Thread thread; private Thread thread;
...@@ -12,9 +16,16 @@ public class CounterRunnable extends Counter implements Runnable{ ...@@ -12,9 +16,16 @@ public class CounterRunnable extends Counter implements Runnable{
@Override @Override
public void run() { public void run() {
sum = 0; count = 0;
List<Integer> primes=new ArrayList<>();
PrimerFinder primerFinder=new PrimerFinder(start,finish,primes);
for (int i = start; i < finish; i++) { for (int i = start; i < finish; i++) {
sum += data[i]; if (primerFinder.isPrime(i)) {
synchronized (primerFinder.getPrimeNumList()) {
primerFinder.getPrimeNumList().add(i);
count++;
}
}
} }
} }
......
import data.TestData; import data.TestData;
import parallelCounters.ParallelCounter0; import parallelCounters.*;
import parallelSummers.ParallelSummer0; import parallelSummers.*;
public class main { public class main {
public static void main(String[] args) { public static void main(String[] args) {
int size = 10; int size = 10;
int[] data = TestData.createData(size); int[] data = TestData.createData(size);
//long result = SequentialSummer.sum(data); long counterResult = SequentialCounter.primesCount(data);
long result = SequentialCounter.primesCount(data);
int threadCount = 8; int threadCount = 8;
//System.out.println("Sequential summer:" + result);
System.out.println("Sequential Primes Count:" + result); System.out.println("Sequential Primes Count:" + counterResult);
System.out.println("Parallel Counter 1: " + ParallelCounter0.primesCount(data, 8)); System.out.println("Parallel Counter 1: " + ParallelCounter0.primesCount(data, threadCount));
/*System.out.println("Parallel Counter 2: " + ParallelCounter1.sum(data, 8)); System.out.println("Parallel Counter 2: " + ParallelCounter1.primesCount(data, threadCount));
System.out.println("Parallel Counter 3: " + ParallelCounter2.sum(data, 8)); System.out.println("Parallel Counter 3: " + ParallelCounter2.primesCount(data, threadCount));
System.out.println("Parallel Counter 4: " + ParallelCounter3.sum(data, 8));*/ System.out.println("Parallel Counter 4: " + ParallelCounter3.primesCount(data, threadCount));
System.out.println("Parallel Counter 4: " + ParallelCounter4.primesCount(data, threadCount));
/*long summerResult = SequentialSummer.sum(data);
System.out.println("Sequential Summer:" + summerResult);
System.out.println("Parallel Summer 1: " + ParallelSummer0.sum(data, threadCount));
System.out.println("Parallel Summer 2: " + ParallelSummer1.sum(data, threadCount));
System.out.println("Parallel Summer 3: " + ParallelSummer2.sum(data, threadCount));
System.out.println("Parallel Summer 4: " + ParallelSummer3.sum(data, threadCount));
System.out.println("Parallel Summer 4: " + ParallelSummer4.sum(data, threadCount));*/
} }
} }
package parallelCounters; package parallelCounters;
import counters.CounterRunnable;
import counters.CounterThread;
import summers.SummerRunnable; import summers.SummerRunnable;
import worker.WorkPartitioner; import worker.WorkPartitioner;
...@@ -10,35 +12,34 @@ import java.util.List; ...@@ -10,35 +12,34 @@ import java.util.List;
public class ParallelCounter1 { public class ParallelCounter1 {
public static long primesCount(int[] data, int threadCount) {
public static long sum(int[] data, int threadCount) { List<CounterRunnable> counters = new ArrayList<>();
List<SummerRunnable> summers = new ArrayList<>();
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount); List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (WorkPartitioner.Part part : parts) { for (WorkPartitioner.Part part : parts) {
summers.add(new SummerRunnable(data,part)); counters.add(new CounterRunnable(data,part));
} }
for (CounterRunnable counterRunnable : counters) {
for (SummerRunnable SummerRunnable : summers) { counterRunnable.startThread();
SummerRunnable.startThread();
} }
for (SummerRunnable SummerRunnable : summers) { for (CounterRunnable counterRunnable : counters) {
try { try {
SummerRunnable.joinThread(); counterRunnable.joinThread();
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.err.println("Thread cannot join!"); System.err.println("Thread cannot join!");
} }
} }
long sum = 0; long count = 0;
for (SummerRunnable summerRunnable : summers) { for (CounterRunnable counterRunnable : counters) {
sum += summerRunnable.getSum(); count += counterRunnable.getCount();
} }
return sum; return count;
} }
} }
package parallelCounters; package parallelCounters;
import counters.CounterRunnable;
import counters.CounterThread;
import summers.SummerThread; import summers.SummerThread;
import worker.WorkPartitioner; import worker.WorkPartitioner;
import worker.WorkPartitioner.Part;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -11,32 +14,25 @@ import java.util.concurrent.Executors; ...@@ -11,32 +14,25 @@ import java.util.concurrent.Executors;
public class ParallelCounter2{ public class ParallelCounter2{
public static long primesCount(int[] data, int threadCount) {
public static long sum(int[] data, int threadCount) { List<CounterThread> counters = new ArrayList<>();
List<Part> parts = WorkPartitioner.partitions(data.length, threadCount);
List<SummerThread> summers = new ArrayList<>(); for (Part part : parts) {
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount); counters.add(new CounterThread(data,part));
for (WorkPartitioner.Part part : parts) {
summers.add(new SummerThread(data,part));
} }
//creating a pool of 5 threads
ExecutorService executor = Executors.newFixedThreadPool(threadCount); ExecutorService executor = Executors.newFixedThreadPool(threadCount);
for (SummerThread summer: summers) { for (CounterThread counter : counters) {
executor.execute(summer); executor.execute(counter);
} }
executor.shutdown(); executor.shutdown();
//Waiting for all the thread to finalize //Waiting for all the thread to finalize
while (!executor.isTerminated()) { while (!executor.isTerminated()) {
//System.out.println("Processing...."); //System.out.println("Processing....");
} }
return summers.stream().mapToLong(SummerThread::getSum).sum(); return counters.stream().mapToLong(CounterThread::getCount).sum();
}
}
} }
package parallelCounters; package parallelCounters;
import counters.CounterCallable;
import summers.SummerCallable; import summers.SummerCallable;
import worker.WorkPartitioner; import worker.WorkPartitioner;
...@@ -14,21 +15,21 @@ import worker.WorkPartitioner.Part; ...@@ -14,21 +15,21 @@ import worker.WorkPartitioner.Part;
public class ParallelCounter3 { public class ParallelCounter3 {
public static long sum(int[] data, int threadCount) { public static long primesCount(int[] data, int threadCount) {
//creating a pool of #threadCount threads //creating a pool of #threadCount threads
ExecutorService executor = Executors.newFixedThreadPool(threadCount); ExecutorService executor = Executors.newFixedThreadPool(threadCount);
List<SummerCallable> summers = new ArrayList<SummerCallable>(); List<CounterCallable> counters = new ArrayList<CounterCallable>();
List<Part> parts = WorkPartitioner.partitions(data.length, threadCount); List<Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (Part part : parts) { for (Part part : parts) {
summers.add(new SummerCallable(data,part)); counters.add(new CounterCallable(data,part));
} }
List<Future<Long>> results; List<Future<Long>> results;
try { try {
results = executor.invokeAll(summers); results = executor.invokeAll(counters);
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.err.println("Cannot invoke the threads."); System.err.println("Cannot invoke the threads.");
return -1; return -1;
...@@ -36,16 +37,16 @@ public class ParallelCounter3 { ...@@ -36,16 +37,16 @@ public class ParallelCounter3 {
executor.shutdown(); executor.shutdown();
long sum = 0; long count = 0;
for (Future<Long> future : results) { for (Future<Long> future : results) {
try { try {
sum += future.get(); count += future.get();
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
System.err.println("Cannot get the results from threads."); System.err.println("Cannot get the results from threads.");
return -2; return -2;
} }
} }
return sum; return count;
} }
} }
package parallelCounters; package parallelCounters;
import counters.CounterCallable;
import summers.SummerCallable; import summers.SummerCallable;
import worker.WorkPartitioner; import worker.WorkPartitioner;
import worker.WorkPartitioner.Part; import worker.WorkPartitioner.Part;
...@@ -14,7 +15,7 @@ import java.util.concurrent.Future; ...@@ -14,7 +15,7 @@ import java.util.concurrent.Future;
public class ParallelCounter4 { public class ParallelCounter4 {
public static long sum(int[] data, int threadCount) { public static long primesCount(int[] data, int threadCount) {
//creating a pool of 5 threads //creating a pool of 5 threads
ExecutorService executor = Executors.newFixedThreadPool(threadCount); ExecutorService executor = Executors.newFixedThreadPool(threadCount);
...@@ -22,7 +23,7 @@ public class ParallelCounter4 { ...@@ -22,7 +23,7 @@ public class ParallelCounter4 {
List<Part> parts = WorkPartitioner.partitions(data.length, threadCount); List<Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (Part part : parts) { for (Part part : parts) {
results.add(executor.submit(new SummerCallable(data, part))); results.add(executor.submit(new CounterCallable(data, part)));
} }
executor.shutdown(); executor.shutdown();
...@@ -31,17 +32,17 @@ public class ParallelCounter4 { ...@@ -31,17 +32,17 @@ public class ParallelCounter4 {
//System.out.println("Processing..."); //System.out.println("Processing...");
} }
long sum = 0; long count = 0;
for (Future<Long> future : results) { for (Future<Long> future : results) {
try { try {
sum += future.get(); count += future.get();
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
System.err.println("Cannot get the results from threads."); System.err.println("Cannot get the results from threads.");
return -2; return -2;
} }
} }
return sum; return count;
} }
......
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