Commit 551cc185 authored by julanar.ali's avatar julanar.ali

testing

parent bd36be5a
......@@ -19,6 +19,12 @@
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package PrimerFinder;
package fiftyShadesOfMultiThreading.PrimerFinder;
import java.util.List;
......
import PrimerFinder.PrimerFinder;
package fiftyShadesOfMultiThreading;
import fiftyShadesOfMultiThreading.PrimerFinder.PrimerFinder;
import java.util.ArrayList;
import java.util.List;
......
import PrimerFinder.PrimerFinder;
package fiftyShadesOfMultiThreading;
import fiftyShadesOfMultiThreading.PrimerFinder.PrimerFinder;
import java.util.ArrayList;
import java.util.List;
......
package counters;
package fiftyShadesOfMultiThreading.counters;
import worker.WorkPartitioner.Part;
import worker.Worker;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
import fiftyShadesOfMultiThreading.worker.Worker;
public class Counter extends Worker {
......
package counters;
import PrimerFinder.PrimerFinder;
import worker.WorkPartitioner.Part;
package fiftyShadesOfMultiThreading.counters;
import fiftyShadesOfMultiThreading.PrimerFinder.PrimerFinder;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
import java.util.ArrayList;
import java.util.List;
......
package counters;
package fiftyShadesOfMultiThreading.counters;
import PrimerFinder.PrimerFinder;
import summers.Summer;
import worker.WorkPartitioner;
import fiftyShadesOfMultiThreading.PrimerFinder.PrimerFinder;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
import java.util.ArrayList;
import java.util.List;
......@@ -10,7 +9,7 @@ import java.util.List;
public class CounterRunnable extends Counter implements Runnable{
private Thread thread;
public CounterRunnable(int[] data, WorkPartitioner.Part part) {
public CounterRunnable(int[] data, Part part) {
super(data, part);
}
......
package counters;
package fiftyShadesOfMultiThreading.counters;
import java.util.ArrayList;
import java.util.List;
import PrimerFinder.PrimerFinder;
import worker.WorkPartitioner.Part;
import fiftyShadesOfMultiThreading.PrimerFinder.PrimerFinder;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
public class CounterThread extends Thread{
private int start;
......
package data;
package fiftyShadesOfMultiThreading.data;
public class TestData {
......
import data.TestData;
import parallelCounters.*;
import parallelSummers.*;
package fiftyShadesOfMultiThreading;
import fiftyShadesOfMultiThreading.data.TestData;
import fiftyShadesOfMultiThreading.parallelCounters.*;
import fiftyShadesOfMultiThreading.parallelSummers.*;
public class main {
public static void main(String[] args) {
......@@ -16,13 +19,13 @@ public class main {
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);
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));*/
System.out.println("Parallel Summer 4: " + ParallelSummer4.sum(data, threadCount));
}
}
package parallelCounters;
package fiftyShadesOfMultiThreading.parallelCounters;
import java.util.ArrayList;
......@@ -8,11 +8,10 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import PrimerFinder.PrimerFinder;
import counters.CounterThread;
import summers.SummerThread;
import worker.WorkPartitioner;
import worker.WorkPartitioner.Part;
import fiftyShadesOfMultiThreading.PrimerFinder.PrimerFinder;
import fiftyShadesOfMultiThreading.counters.CounterThread;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
//this class calculates the number of primes in [0 , size-1] using CounterThread
......
package parallelCounters;
import counters.CounterRunnable;
import counters.CounterThread;
import summers.SummerRunnable;
import worker.WorkPartitioner;
package fiftyShadesOfMultiThreading.parallelCounters;
import fiftyShadesOfMultiThreading.counters.CounterRunnable;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
import java.util.ArrayList;
import java.util.List;
......@@ -18,10 +16,10 @@ public class ParallelCounter1 {
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);
List<Part> parts = WorkPartitioner.partitions(data.length, threadCount);
//assigning each part to counter runnable
for (WorkPartitioner.Part part : parts) {
for (Part part : parts) {
counters.add(new CounterRunnable(data,part));
}
......
package parallelCounters;
package fiftyShadesOfMultiThreading.parallelCounters;
import counters.CounterRunnable;
import counters.CounterThread;
import summers.SummerThread;
import worker.WorkPartitioner;
import worker.WorkPartitioner.Part;
import fiftyShadesOfMultiThreading.counters.CounterThread;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
import java.util.ArrayList;
import java.util.List;
......
package parallelCounters;
import counters.CounterCallable;
import summers.SummerCallable;
import worker.WorkPartitioner;
package fiftyShadesOfMultiThreading.parallelCounters;
import fiftyShadesOfMultiThreading.counters.CounterCallable;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner;
import java.util.ArrayList;
import java.util.List;
......@@ -10,7 +10,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import worker.WorkPartitioner.Part;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
//this class calculates the number of primes in [0 , size-1]
// using ExecutorService with fixed thread pool of threadCount threads,
......
package parallelCounters;
import counters.CounterCallable;
import summers.SummerCallable;
import worker.WorkPartitioner;
import worker.WorkPartitioner.Part;
package fiftyShadesOfMultiThreading.parallelCounters;
import fiftyShadesOfMultiThreading.counters.CounterCallable;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
import java.util.ArrayList;
import java.util.List;
......
package parallelSummers;
package fiftyShadesOfMultiThreading.parallelSummers;
import java.util.ArrayList;
import java.util.List;
import counters.CounterThread;
import summers.SummerThread;
import worker.WorkPartitioner;
import worker.WorkPartitioner.Part;
import fiftyShadesOfMultiThreading.summers.SummerThread;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
public class ParallelSummer0 {
public static long primesCount(int[] data, int threadCount) {
List<CounterThread> counters = new ArrayList<>();
List<Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (Part part : parts) {
counters.add(new CounterThread(data,part));
}
for (CounterThread counterThread : counters) {
counterThread.start();
}
for (CounterThread counterThread : counters) {
try {
counterThread.join();
} catch (InterruptedException e) {
System.err.println("Thread cannot join!");
}
}
int count = 0;
for (CounterThread counterThread : counters) {
count += counterThread.getCount();
}
return count;
}
public static long sum(int[] data, int threadCount) {
List<SummerThread> summers = new ArrayList<>();
......
package parallelSummers;
package fiftyShadesOfMultiThreading.parallelSummers;
import summers.SummerRunnable;
import worker.WorkPartitioner;
import fiftyShadesOfMultiThreading.summers.SummerRunnable;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
import java.util.ArrayList;
import java.util.List;
......@@ -14,9 +15,9 @@ public class ParallelSummer1 {
public static long sum(int[] data, int threadCount) {
List<SummerRunnable> summers = new ArrayList<>();
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount);
List<Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (WorkPartitioner.Part part : parts) {
for (Part part : parts) {
summers.add(new SummerRunnable(data,part));
}
......
package parallelSummers;
package fiftyShadesOfMultiThreading.parallelSummers;
import summers.SummerThread;
import worker.WorkPartitioner;
import fiftyShadesOfMultiThreading.summers.SummerThread;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner;
import java.util.ArrayList;
import java.util.List;
......
package parallelSummers;
import summers.SummerCallable;
import worker.WorkPartitioner;
package fiftyShadesOfMultiThreading.parallelSummers;
import fiftyShadesOfMultiThreading.summers.SummerCallable;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner;
import java.util.ArrayList;
import java.util.List;
......@@ -9,7 +9,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import worker.WorkPartitioner.Part;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
public class ParallelSummer3 {
......
package parallelSummers;
import summers.SummerCallable;
import worker.WorkPartitioner;
import worker.WorkPartitioner.Part;
package fiftyShadesOfMultiThreading.parallelSummers;
import fiftyShadesOfMultiThreading.summers.SummerCallable;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
import java.util.ArrayList;
import java.util.List;
......
package summers;
package fiftyShadesOfMultiThreading.summers;
import worker.WorkPartitioner;
import worker.Worker;
public class Summer extends Worker{
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
import fiftyShadesOfMultiThreading.worker.Worker;
public class Summer extends Worker {
protected long sum;
public Summer(int[] data, WorkPartitioner.Part part) {
public Summer(int[] data, Part part) {
super(data, part);
}
public long getSum() {
......
package summers;
package fiftyShadesOfMultiThreading.summers;
import counters.Counter;
import worker.WorkPartitioner;
import fiftyShadesOfMultiThreading.counters.Counter;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner;
import java.util.concurrent.Callable;
......
package summers;
package fiftyShadesOfMultiThreading.summers;
import java.util.concurrent.Callable;
import worker.WorkPartitioner.Part;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
public class SummerRunnable extends Summer implements Runnable{
......
package summers;
package fiftyShadesOfMultiThreading.summers;
import java.util.ArrayList;
import java.util.List;
import worker.WorkPartitioner.Part;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
public class SummerThread extends Thread{
private int start;
......
package worker;
package fiftyShadesOfMultiThreading.worker;
import java.util.ArrayList;
import java.util.List;
......
package worker;
package fiftyShadesOfMultiThreading.worker;
import worker.WorkPartitioner.Part;
import fiftyShadesOfMultiThreading.worker.WorkPartitioner.Part;
public abstract class Worker {
......
package fiftyShadesOfMultiThreading;
import fiftyShadesOfMultiThreading.data.TestData;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import fiftyShadesOfMultiThreading.parallelCounters.*;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
/*import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;*/
public class TestParallelCounter {
private static int size;
private static int[] data;
private static long result;
@BeforeAll
public static void initialize() {
size = 1000000;
data = TestData.createData(size);
result = SequentialCounter.primesCount(data);
}
private static void testAll(int threadCount) {
assertEquals(result, ParallelCounter0.primesCount(data,threadCount));
assertEquals(result, ParallelCounter1.primesCount(data,threadCount));
assertEquals(result, ParallelCounter2.primesCount(data,threadCount));
assertEquals(result, ParallelCounter3.primesCount(data,threadCount));
assertEquals(result, ParallelCounter4.primesCount(data,threadCount));
}
@Test
public void testSingleThread() {
testAll(1);
}
@Test
public void testTwoThread() {
testAll(2);
}
@Test
public void testFourThread() {
testAll(4);
}
@Test
public void testEightThread() {
testAll(8);
}
@Test
public void testSixteenThread() {
testAll(16);
}
}
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