85 lines
3.1 KiB
Java
85 lines
3.1 KiB
Java
package test;
|
|
|
|
import java.util.Random;
|
|
import sequential.SequentialSum;
|
|
import parallel.CollectingResults;
|
|
import simple2.SingleUpdatePerThread;
|
|
import simple1.MultipleUpdatesPerThread;
|
|
import atomic2.SingleUpdatePerThreadAtomic;
|
|
import atomic1.MultipleUpdatesPerThreadAtomic;
|
|
import synchronized2.SingleUpdatePerThreadSynch;
|
|
import synchronized1.MultipleUpdatesPerThreadSynch;
|
|
|
|
public class Test {
|
|
|
|
private static final Random generator = new Random();
|
|
public static final int NUM_THREADS = 256;
|
|
private static final int SIZE = 99999999;
|
|
private static final int RUNS = 15;
|
|
|
|
private static long getSum(int[] array) {
|
|
return SequentialSum.sum(array);
|
|
}
|
|
|
|
private static void testSumImplementation(
|
|
SumFunction implementation,
|
|
int[] array, String name) throws Exception {
|
|
|
|
// Cloning the array with the random numbers
|
|
int[] copy = array.clone();
|
|
// Getting a time-stamp when the sum starts
|
|
long startTime = System.nanoTime();
|
|
// Executing the implementation to sum the numbers
|
|
long result = implementation.sum(copy);
|
|
// Getting a time-stamp when the sum finishes
|
|
long endTime = System.nanoTime();
|
|
long correct = getSum(array);
|
|
if (correct != result) {
|
|
// Informing if the implementation failed
|
|
System.err.println("- ERROR: " + name + " result was: "
|
|
+ result + " but " + correct + " was expected.");
|
|
} else {
|
|
// Showing the time taken by the implementation
|
|
System.out.printf("- " + name + " time: %d ms \n"
|
|
, ((endTime - startTime) / 1000000));
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
// Generating random numbers to be added
|
|
int[] numbers = new int[SIZE];
|
|
for (int i = 0; i < SIZE; i++) {
|
|
numbers[i] = generator.nextInt();
|
|
}
|
|
// Executing several runs
|
|
for (int i = 0; i < RUNS; i++) {
|
|
System.out.println("Run " + (i + 1) + "/" + RUNS + ":");
|
|
// Testing different implementations:
|
|
// Sequential
|
|
testSumImplementation(SequentialSum::sum, numbers,
|
|
"Sequential version");
|
|
//Parallel implementation - Multiple updates per thread
|
|
testSumImplementation(MultipleUpdatesPerThread::sum, numbers,
|
|
"Parallel - Multiple updates per thread");
|
|
// Parallel - Single update per thread
|
|
testSumImplementation(SingleUpdatePerThread::sum, numbers,
|
|
"Parallel - Single update per thread");
|
|
// Parallel - Multiple updates per thread - Synchronized
|
|
testSumImplementation(MultipleUpdatesPerThreadSynch::sum, numbers,
|
|
"Parallel - Multiple updates per thread - Synchronized");
|
|
// Parallel - Single update per thread - Synchronized
|
|
testSumImplementation(SingleUpdatePerThreadSynch::sum, numbers,
|
|
"Parallel - Single update per thread - Synchronized");
|
|
// Parallel - Multiple updates per thread - Atomic
|
|
testSumImplementation(MultipleUpdatesPerThreadAtomic::sum, numbers,
|
|
"Parallel - Multiple updates per thread - Atomic");
|
|
// Parallel - Single update per thread - Atomic
|
|
testSumImplementation(SingleUpdatePerThreadAtomic::sum, numbers,
|
|
"Parallel - Single update per thread - Atomic");
|
|
// Parallel - Collecting partial results
|
|
testSumImplementation(CollectingResults::sum, numbers,
|
|
"Parallel - Collecting partial results");
|
|
}
|
|
}
|
|
}
|