Added assignment sources

This commit is contained in:
Claudio Maggioni 2023-05-07 18:10:54 +02:00
commit 63f27e6a93
9 changed files with 226 additions and 0 deletions

Binary file not shown.

2
Reversal/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.idea
target

7
Reversal/README.md Normal file
View file

@ -0,0 +1,7 @@
# Array reverser Java sources
To compile and execute the reverser run:
```shell
mvn clean compile exec:java
```

27
Reversal/pom.xml Normal file
View file

@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.usi.inf.si</groupId>
<artifactId>reverse</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>ReverserTest</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,19 @@
abstract class ArrayReverser implements Reverser
{
volatile int[] array;
volatile int[] reversed;
public void setArray(int[] array) {
this.array = array;
this.reversed = new int[array.length];
}
public int[] array() {
return array;
}
public int[] reversed() {
return reversed;
}
}

View file

@ -0,0 +1,64 @@
import java.util.ArrayList;
class ParallelReverser extends ArrayReverser
{
// number of threads
private final int N;
// number of elements mapped by each thread
private final int S;
// reverse `array` using N threads
public ParallelReverser(int[] array, int N) {
setArray(array);
this.N = N;
this.S = array.length / this.N;
}
public void reverse() {
ArrayList<Thread> threads = new ArrayList<>();
for (int n = 0; n < N; n++) {
// thread t will reverse a[n*S..n*S + S]
int from = n*S;
int to = (n*S + S);
// the last thread takes what's left
if (n == N - 1)
to = array.length;
ThreadedReverser s = new ThreadedReverser(array, from, to, reversed);
Thread t = new Thread(s);
threads.add(t);
// start the thread
t.start();
}
// wait for all threads to terminate
for (int k = 0; k < threads.size(); k++) {
try {
Thread t = threads.get(k);
// wait for thread t to terminate
t.join();
} catch (InterruptedException s) {
System.out.println("Thread interrupted");
}
}
}
}
class ThreadedReverser extends SequentialReverser implements Runnable
{
private int from, to;
ThreadedReverser(int[] array, int from, int to, int[] result) {
super(array);
// reverse `array` into the given `result` array
this.reversed = result;
this.from = from;
this.to = to;
}
public void run() {
reverse(from, to);
}
}

View file

@ -0,0 +1,17 @@
interface Reverser
{
// reverse current array
void reverse();
// set array to be reversed and resets reversed array
void setArray(int[] array);
// get current, unreversed array
int[] array();
// get the results after calling reverse()
int[] reversed();
}

View file

@ -0,0 +1,70 @@
import java.util.Arrays;
class ReverserTest
{
private int[] testSequential(int[] a) {
SequentialReverser rev = new SequentialReverser(a);
rev.reverse();
boolean ok = true;
for (int k = 0; k < a.length; k++)
ok = ok && (a[k] == rev.reversed()[a.length - k - 1]);
if (!ok)
System.out.println("Reversal error: " + Arrays.toString(a) + " reversed: " + Arrays.toString(rev.reversed()));
return rev.reversed();
}
private int[] testParallel(int[] a, int N) {
ParallelReverser rev = new ParallelReverser(a, N);
rev.reverse();
return rev.reversed();
}
private boolean check(int[] a, int N) {
int[] s = testSequential(a);
int[] p = testParallel(a, N);
boolean result = Arrays.equals(s, p);
if (result)
System.out.println("Test successful");
else
System.out.println("Sequential reversal: " + Arrays.toString(s) + " but parallel reversal: " + Arrays.toString(p));
return result;
}
void test1() {
int[] a = {1, 2, 3, 4, 5, 6};
check(a, 1);
check(a, 2);
check(a, 3);
check(a, 5);
}
void test2() {
int[] a = {-1, 2, -3, 4, -5, 6, -7};
check(a, 3);
check(a, 4);
}
void test3() {
int[] a = { };
check(a, 1);
check(a, 2);
check(a, 3);
}
void test4() {
int[] a = { 1 };
check(a, 1);
check(a, 2);
check(a, 3);
}
public static void main(String[] args) {
ReverserTest o = new ReverserTest();
o.test1();
o.test2();
o.test3();
o.test4();
}
}

View file

@ -0,0 +1,20 @@
class SequentialReverser extends ArrayReverser
{
public SequentialReverser(int[] array) {
setArray(array);
}
public void reverse() {
// reverse all elements of `array`
reverse(0, array.length);
}
// reverse elements of `array` from index `from` (included) to
// index `to` (excluded) into array `reversed`
void reverse(int from, int to) {
for (int k = from; k < to; k++)
reversed[reversed.length - k - 1] = array[k];
}
}