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