forked from Parallele_Programmierung/Labs
initial
parent
d6914c49cc
commit
53803a654a
|
|
@ -8,3 +8,4 @@
|
||||||
*/target/
|
*/target/
|
||||||
*/.apt_generated/
|
*/.apt_generated/
|
||||||
*/.apt_generated_tests/
|
*/.apt_generated_tests/
|
||||||
|
*/.vscode/
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Laboraufgabe "Monte-Carlo-Algorithmus zur Annäherung von pi - sequenziell"
|
||||||
|
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-01-ConcurrencyMonteCarloPiSeq.html)
|
||||||
|
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-Solutions.html#laboraufgabe-monte-carlo-algorithmus-zur-annäherung-von-pi-sequenziell)
|
||||||
|
|
||||||
|
# Materialien
|
||||||
|
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/08-completableFuture.html)
|
||||||
|
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/08-completableFuture.html)
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Justfile (https://just.systems/) for starting Maven standard targets
|
||||||
|
|
||||||
|
default: clean compile package
|
||||||
|
|
||||||
|
exec class args: compile
|
||||||
|
mvn exec:java -Dexec.args="{{args}}" -Dexec.mainClass={{class}}
|
||||||
|
# exec class args:
|
||||||
|
# java -cp target/app.jar {{class}} {{args}}
|
||||||
|
clean:
|
||||||
|
mvn clean
|
||||||
|
compile:
|
||||||
|
mvn compile
|
||||||
|
test: compile
|
||||||
|
mvn test
|
||||||
|
javadoc:
|
||||||
|
mvn javadoc:javadoc
|
||||||
|
package:
|
||||||
|
mvn package
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
<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>pp</groupId>
|
||||||
|
<artifactId>pp.08.01-ConcurrencyMonteCarloPiSeq</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.release>16</maven.compiler.release>
|
||||||
|
<lombok.version>edge-SNAPSHOT</lombok.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency><!-- für Unit-Tests -->
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.10.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für Lombok -->
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für net.jcip Annotationen -->
|
||||||
|
<groupId>net.jcip</groupId>
|
||||||
|
<artifactId>jcip-annotations</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<defaultGoal>clean compile</defaultGoal>
|
||||||
|
<plugins>
|
||||||
|
<plugin><!-- für Unit-Tests [mvn test] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn compile] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.9.0</version>
|
||||||
|
<configuration>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn exec:java] -->
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn javadoc:javadoc] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>3.5.0</version>
|
||||||
|
<configuration>
|
||||||
|
<show>private</show>
|
||||||
|
<locale>en_US</locale>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
public record InOutTuple(int in, int out) {
|
||||||
|
}
|
||||||
|
|
||||||
|
//record seit Java 16 (preview seit Java 14).
|
||||||
|
//identisch zu ...
|
||||||
|
//public final class InOutTuple {
|
||||||
|
// final private int in;
|
||||||
|
// final private int out;
|
||||||
|
//
|
||||||
|
// public InOutTuple(int in, int out) {
|
||||||
|
// this.in = in;
|
||||||
|
// this.out = out;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int in() {
|
||||||
|
// return this.in;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int out() {
|
||||||
|
// return this.out;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public String toString() {
|
||||||
|
// return String.format("InOutTuple [in=%s, out=%s]", this.in, this.out);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public int hashCode() {
|
||||||
|
// var prime = 31;
|
||||||
|
// var result = 1;
|
||||||
|
// result = (prime * result) + this.in;
|
||||||
|
// result = (prime * result) + this.out;
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public boolean equals(Object obj) {
|
||||||
|
// if (this == obj) {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
// if (!(obj instanceof InOutTuple)) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// var other = (InOutTuple) obj;
|
||||||
|
// if (this.in != other.in) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// if (this.out != other.out) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnung von pi durch Monte Carlo Verfahren: Vergleich der Anzahl von
|
||||||
|
* zufällig gewähten Punkten innerhalb eines Viertelkreises (Radius r = 1) bzw.
|
||||||
|
* innerhalb eines Quadrates (Kantenlänge 1) analog zu Fläche eines
|
||||||
|
* Viertelkreises (pi * r^2 / 4) mit der Fläche des Quadrates (1 = 1 * 1).
|
||||||
|
*
|
||||||
|
* Sequenzielle Berechnung (im Main-Thread)
|
||||||
|
*
|
||||||
|
* @author Sandro Leuchter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MonteCarloPiSeq {
|
||||||
|
static final int TOTAL_CYCLES = 10000000;
|
||||||
|
|
||||||
|
public static InOutTuple getResultMonteCarloPiDraw(int cycles) {
|
||||||
|
var in = 0;
|
||||||
|
var out = 0;
|
||||||
|
var r = new Random();
|
||||||
|
for (var i = 0; i < cycles; i++) {
|
||||||
|
var x = r.nextDouble();
|
||||||
|
var y = r.nextDouble();
|
||||||
|
if (Math.sqrt((x * x) + (y * y)) <= 1.0) {
|
||||||
|
in++;
|
||||||
|
} else {
|
||||||
|
out++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new InOutTuple(in, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main-Methode
|
||||||
|
*
|
||||||
|
* @param args Kommandozeilenparameter (nicht benutzt)
|
||||||
|
*/
|
||||||
|
public static void main(String... args) {
|
||||||
|
var result = getResultMonteCarloPiDraw(TOTAL_CYCLES);
|
||||||
|
var pi = ((4.0 * result.in()) / (result.in() + result.out()));
|
||||||
|
System.out.println(pi);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Laboraufgabe "Monte-Carlo-Algorithmus zur Annäherung von pi - sequenziell"
|
||||||
|
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-01-ConcurrencyMonteCarloPiSeq.html)
|
||||||
|
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-Solutions.html#laboraufgabe-monte-carlo-algorithmus-zur-annäherung-von-pi-sequenziell)
|
||||||
|
|
||||||
|
# Materialien
|
||||||
|
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/08-completableFuture.html)
|
||||||
|
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/08-completableFuture.html)
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Justfile (https://just.systems/) for starting Maven standard targets
|
||||||
|
|
||||||
|
default: clean compile package
|
||||||
|
|
||||||
|
exec class args: compile
|
||||||
|
mvn exec:java -Dexec.args="{{args}}" -Dexec.mainClass={{class}}
|
||||||
|
# exec class args:
|
||||||
|
# java -cp target/app.jar {{class}} {{args}}
|
||||||
|
clean:
|
||||||
|
mvn clean
|
||||||
|
compile:
|
||||||
|
mvn compile
|
||||||
|
test: compile
|
||||||
|
mvn test
|
||||||
|
javadoc:
|
||||||
|
mvn javadoc:javadoc
|
||||||
|
package:
|
||||||
|
mvn package
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
<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>pp</groupId>
|
||||||
|
<artifactId>pp.08.01-ConcurrencyMonteCarloPiSeq_solution</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.release>16</maven.compiler.release>
|
||||||
|
<lombok.version>edge-SNAPSHOT</lombok.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency><!-- für Unit-Tests -->
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.10.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für Lombok -->
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für net.jcip Annotationen -->
|
||||||
|
<groupId>net.jcip</groupId>
|
||||||
|
<artifactId>jcip-annotations</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin><!-- für Unit-Tests [mvn test] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn compile] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.9.0</version>
|
||||||
|
<configuration>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn exec:java] -->
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn javadoc:javadoc] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>3.5.0</version>
|
||||||
|
<configuration>
|
||||||
|
<show>private</show>
|
||||||
|
<locale>en_US</locale>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
public record InOutTuple(int in, int out) {
|
||||||
|
}
|
||||||
|
|
||||||
|
//record seit Java 16 (preview seit Java 14).
|
||||||
|
//identisch zu ...
|
||||||
|
//public final class InOutTuple {
|
||||||
|
// final private int in;
|
||||||
|
// final private int out;
|
||||||
|
//
|
||||||
|
// public InOutTuple(int in, int out) {
|
||||||
|
// this.in = in;
|
||||||
|
// this.out = out;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int in() {
|
||||||
|
// return this.in;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int out() {
|
||||||
|
// return this.out;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public String toString() {
|
||||||
|
// return String.format("InOutTuple [in=%s, out=%s]", this.in, this.out);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public int hashCode() {
|
||||||
|
// var prime = 31;
|
||||||
|
// var result = 1;
|
||||||
|
// result = (prime * result) + this.in;
|
||||||
|
// result = (prime * result) + this.out;
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public boolean equals(Object obj) {
|
||||||
|
// if (this == obj) {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
// if (!(obj instanceof InOutTuple)) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// var other = (InOutTuple) obj;
|
||||||
|
// if (this.in != other.in) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// if (this.out != other.out) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnung von pi durch Monte Carlo Verfahren: Vergleich der Anzahl von
|
||||||
|
* zufällig gewähten Punkten innerhalb eines Viertelkreises (Radius r = 1) bzw.
|
||||||
|
* innerhalb eines Quadrates (Kantenlänge 1) analog zu Fläche eines
|
||||||
|
* Viertelkreises (pi * r^2 / 4) mit der Fläche des Quadrates (1 = 1 * 1).
|
||||||
|
*
|
||||||
|
* Sequenzielle Berechnung (im Main-Thread)
|
||||||
|
*
|
||||||
|
* @author Sandro Leuchter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MonteCarloPiSeq {
|
||||||
|
static final int TOTAL_CYCLES = 10000000;
|
||||||
|
|
||||||
|
public static InOutTuple getResultMonteCarloPiDraw(int cycles) {
|
||||||
|
var in = 0;
|
||||||
|
var out = 0;
|
||||||
|
var r = new Random();
|
||||||
|
for (var i = 0; i < cycles; i++) {
|
||||||
|
var x = r.nextDouble();
|
||||||
|
var y = r.nextDouble();
|
||||||
|
if (Math.sqrt((x * x) + (y * y)) <= 1.0) {
|
||||||
|
in++;
|
||||||
|
} else {
|
||||||
|
out++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new InOutTuple(in, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main-Methode
|
||||||
|
*
|
||||||
|
* @param args Kommandozeilenparameter (nicht benutzt)
|
||||||
|
*/
|
||||||
|
public static void main(String... args) {
|
||||||
|
var now = System.currentTimeMillis();
|
||||||
|
var result = getResultMonteCarloPiDraw(TOTAL_CYCLES);
|
||||||
|
var time = System.currentTimeMillis() - now;
|
||||||
|
var pi = ((4.0 * result.in()) / (result.in() + result.out()));
|
||||||
|
System.out.println(pi + ", " + time + " ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Laboraufgabe "Monte-Carlo-Algorithmus zur Annäherung von pi – mit Future"
|
||||||
|
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-02-ConcurrencyMonteCarloPiFuture.html)
|
||||||
|
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-Solutions.html#laboraufgabe-monte-carlo-algorithmus-zur-annäherung-von-pi-mit-future)
|
||||||
|
|
||||||
|
# Materialien
|
||||||
|
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/08-completableFuture.html)
|
||||||
|
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/08-completableFuture.html)
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Justfile (https://just.systems/) for starting Maven standard targets
|
||||||
|
|
||||||
|
default: clean compile package
|
||||||
|
|
||||||
|
exec class args: compile
|
||||||
|
mvn exec:java -Dexec.args="{{args}}" -Dexec.mainClass={{class}}
|
||||||
|
# exec class args:
|
||||||
|
# java -cp target/app.jar {{class}} {{args}}
|
||||||
|
clean:
|
||||||
|
mvn clean
|
||||||
|
compile:
|
||||||
|
mvn compile
|
||||||
|
test: compile
|
||||||
|
mvn test
|
||||||
|
javadoc:
|
||||||
|
mvn javadoc:javadoc
|
||||||
|
package:
|
||||||
|
mvn package
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
<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>pp</groupId>
|
||||||
|
<artifactId>pp.08.02-ConcurrencyMonteCarloPiFuture</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.release>16</maven.compiler.release>
|
||||||
|
<lombok.version>edge-SNAPSHOT</lombok.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency><!-- für Unit-Tests -->
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.10.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für Lombok -->
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für net.jcip Annotationen -->
|
||||||
|
<groupId>net.jcip</groupId>
|
||||||
|
<artifactId>jcip-annotations</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin><!-- für Unit-Tests [mvn test] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn compile] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.9.0</version>
|
||||||
|
<configuration>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn exec:java] -->
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn javadoc:javadoc] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>3.5.0</version>
|
||||||
|
<configuration>
|
||||||
|
<show>private</show>
|
||||||
|
<locale>en_US</locale>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
public record InOutTuple(int in, int out) {
|
||||||
|
}
|
||||||
|
|
||||||
|
//record seit Java 16 (preview seit Java 14).
|
||||||
|
//identisch zu ...
|
||||||
|
//public final class InOutTuple {
|
||||||
|
// final private int in;
|
||||||
|
// final private int out;
|
||||||
|
//
|
||||||
|
// public InOutTuple(int in, int out) {
|
||||||
|
// this.in = in;
|
||||||
|
// this.out = out;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int in() {
|
||||||
|
// return this.in;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int out() {
|
||||||
|
// return this.out;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public String toString() {
|
||||||
|
// return String.format("InOutTuple [in=%s, out=%s]", this.in, this.out);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public int hashCode() {
|
||||||
|
// var prime = 31;
|
||||||
|
// var result = 1;
|
||||||
|
// result = (prime * result) + this.in;
|
||||||
|
// result = (prime * result) + this.out;
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public boolean equals(Object obj) {
|
||||||
|
// if (this == obj) {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
// if (!(obj instanceof InOutTuple)) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// var other = (InOutTuple) obj;
|
||||||
|
// if (this.in != other.in) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// if (this.out != other.out) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnung von pi durch Monte Carlo Verfahren: Vergleich der Anzahl von
|
||||||
|
* zufällig gewähten Punkten innerhalb eines Viertelkreises (Radius r = 1) bzw.
|
||||||
|
* innerhalb eines Quadrates (Kantenlänge 1) analog zu Fläche eines
|
||||||
|
* Viertelkreises (pi * r^2 / 4) mit der Fläche des Quadrates (1 = 1 * 1).
|
||||||
|
*
|
||||||
|
* Sequenzielle Berechnung (im Main-Thread)
|
||||||
|
*
|
||||||
|
* @author Sandro Leuchter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MonteCarloPiSeq {
|
||||||
|
static final int TOTAL_CYCLES = 10000000;
|
||||||
|
|
||||||
|
public static InOutTuple getResultMonteCarloPiDraw(int cycles) {
|
||||||
|
var in = 0;
|
||||||
|
var out = 0;
|
||||||
|
var r = new Random();
|
||||||
|
for (var i = 0; i < cycles; i++) {
|
||||||
|
var x = r.nextDouble();
|
||||||
|
var y = r.nextDouble();
|
||||||
|
if (Math.sqrt((x * x) + (y * y)) <= 1.0) {
|
||||||
|
in++;
|
||||||
|
} else {
|
||||||
|
out++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new InOutTuple(in, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main-Methode
|
||||||
|
*
|
||||||
|
* @param args Kommandozeilenparameter (nicht benutzt)
|
||||||
|
*/
|
||||||
|
public static void main(String... args) {
|
||||||
|
var now = System.currentTimeMillis();
|
||||||
|
var result = getResultMonteCarloPiDraw(TOTAL_CYCLES);
|
||||||
|
var time = System.currentTimeMillis() - now;
|
||||||
|
var pi = ((4.0 * result.in()) / (result.in() + result.out()));
|
||||||
|
System.out.println(pi + ", " + time + " ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Laboraufgabe "Monte-Carlo-Algorithmus zur Annäherung von pi – mit Future"
|
||||||
|
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-02-ConcurrencyMonteCarloPiFuture.html)
|
||||||
|
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-Solutions.html#laboraufgabe-monte-carlo-algorithmus-zur-annäherung-von-pi-mit-future)
|
||||||
|
|
||||||
|
# Materialien
|
||||||
|
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/08-completableFuture.html)
|
||||||
|
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/08-completableFuture.html)
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Justfile (https://just.systems/) for starting Maven standard targets
|
||||||
|
|
||||||
|
default: clean compile package
|
||||||
|
|
||||||
|
exec class args: compile
|
||||||
|
mvn exec:java -Dexec.args="{{args}}" -Dexec.mainClass={{class}}
|
||||||
|
# exec class args:
|
||||||
|
# java -cp target/app.jar {{class}} {{args}}
|
||||||
|
clean:
|
||||||
|
mvn clean
|
||||||
|
compile:
|
||||||
|
mvn compile
|
||||||
|
test: compile
|
||||||
|
mvn test
|
||||||
|
javadoc:
|
||||||
|
mvn javadoc:javadoc
|
||||||
|
package:
|
||||||
|
mvn package
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
<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>pp</groupId>
|
||||||
|
<artifactId>pp.08.02-ConcurrencyMonteCarloPiFuture_solution</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.release>16</maven.compiler.release>
|
||||||
|
<lombok.version>edge-SNAPSHOT</lombok.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency><!-- für Unit-Tests -->
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.10.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für Lombok -->
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für net.jcip Annotationen -->
|
||||||
|
<groupId>net.jcip</groupId>
|
||||||
|
<artifactId>jcip-annotations</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin><!-- für Unit-Tests [mvn test] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn compile] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.9.0</version>
|
||||||
|
<configuration>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn exec:java] -->
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn javadoc:javadoc] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>3.5.0</version>
|
||||||
|
<configuration>
|
||||||
|
<show>private</show>
|
||||||
|
<locale>en_US</locale>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
public record InOutTuple(int in, int out) {
|
||||||
|
InOutTuple add(InOutTuple other) {
|
||||||
|
return new InOutTuple(in() + other.in(), out() + other.out());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//record seit Java 16 (preview seit Java 14).
|
||||||
|
//identisch zu ...
|
||||||
|
//public final class InOutTuple {
|
||||||
|
// final private int in;
|
||||||
|
// final private int out;
|
||||||
|
//
|
||||||
|
// public InOutTuple(int in, int out) {
|
||||||
|
// this.in = in;
|
||||||
|
// this.out = out;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int in() {
|
||||||
|
// return this.in;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int out() {
|
||||||
|
// return this.out;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public String toString() {
|
||||||
|
// return String.format("InOutTuple [in=%s, out=%s]", this.in, this.out);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public int hashCode() {
|
||||||
|
// var prime = 31;
|
||||||
|
// var result = 1;
|
||||||
|
// result = (prime * result) + this.in;
|
||||||
|
// result = (prime * result) + this.out;
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public boolean equals(Object obj) {
|
||||||
|
// if (this == obj) {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
// if (!(obj instanceof InOutTuple)) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// var other = (InOutTuple) obj;
|
||||||
|
// if (this.in != other.in) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// if (this.out != other.out) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// InOutTuple add(InOutTuple other) {
|
||||||
|
// return new InOutTuple(in() + other.in(), out() + other.out());
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnung von pi durch Monte Carlo Verfahren: Vergleich der Anzahl von
|
||||||
|
* zufällig gewähten Punkten innerhalb eines Viertelkreises (Radius r = 1) bzw.
|
||||||
|
* innerhalb eines Quadrates (Kantenlänge 1) analog zu Fläche eines
|
||||||
|
* Viertelkreises (pi * r^2 / 4) mit der Fläche des Quadrates (1 = 1 * 1).
|
||||||
|
*
|
||||||
|
* Parallele Berechnung (mit Futures im FixedThreadPool)
|
||||||
|
*
|
||||||
|
* @author Sandro Leuchter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MonteCarloPiFuture {
|
||||||
|
static final int PARALLELISM = 8;
|
||||||
|
static final int TOTAL_CYCLES = 10000000;
|
||||||
|
|
||||||
|
public static InOutTuple getResultMonteCarloPiDraw(int cycles) {
|
||||||
|
var in = 0;
|
||||||
|
var out = 0;
|
||||||
|
var r = new Random();
|
||||||
|
for (var i = 0; i < cycles; i++) {
|
||||||
|
var x = r.nextDouble();
|
||||||
|
var y = r.nextDouble();
|
||||||
|
if (Math.sqrt((x * x) + (y * y)) <= 1.0) {
|
||||||
|
in++;
|
||||||
|
} else {
|
||||||
|
out++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new InOutTuple(in, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main-Methode
|
||||||
|
*
|
||||||
|
* @param args Kommandozeilenparameter (nicht benutzt)
|
||||||
|
* @throws ExecutionException
|
||||||
|
* @throws InterruptedException
|
||||||
|
*/
|
||||||
|
public static void main(String... args)
|
||||||
|
throws InterruptedException, ExecutionException {
|
||||||
|
var pool = Executors.newFixedThreadPool(PARALLELISM);
|
||||||
|
var futures = new LinkedList<Future<InOutTuple>>();
|
||||||
|
var now = System.currentTimeMillis();
|
||||||
|
for (var i = 0; i < PARALLELISM; i++) {
|
||||||
|
futures.add(pool.submit(() -> getResultMonteCarloPiDraw(
|
||||||
|
TOTAL_CYCLES / PARALLELISM)));
|
||||||
|
}
|
||||||
|
pool.shutdown();
|
||||||
|
var time = System.currentTimeMillis() - now;
|
||||||
|
var result = new InOutTuple(0, 0);
|
||||||
|
for (var f : futures) {
|
||||||
|
result = result.add(f.get());
|
||||||
|
}
|
||||||
|
var pi = ((4.0 * result.in()) / (result.in() + result.out()));
|
||||||
|
System.out.println(pi + ", " + time + " ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnung von pi durch Monte Carlo Verfahren: Vergleich der Anzahl von
|
||||||
|
* zufällig gewähten Punkten innerhalb eines Viertelkreises (Radius r = 1) bzw.
|
||||||
|
* innerhalb eines Quadrates (Kantenlänge 1) analog zu Fläche eines
|
||||||
|
* Viertelkreises (pi * r^2 / 4) mit der Fläche des Quadrates (1 = 1 * 1).
|
||||||
|
*
|
||||||
|
* Sequenzielle Berechnung (im Main-Thread)
|
||||||
|
*
|
||||||
|
* @author Sandro Leuchter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MonteCarloPiSeq {
|
||||||
|
static final int TOTAL_CYCLES = 10000000;
|
||||||
|
|
||||||
|
public static InOutTuple getResultMonteCarloPiDraw(int cycles) {
|
||||||
|
var in = 0;
|
||||||
|
var out = 0;
|
||||||
|
var r = new Random();
|
||||||
|
for (var i = 0; i < cycles; i++) {
|
||||||
|
var x = r.nextDouble();
|
||||||
|
var y = r.nextDouble();
|
||||||
|
if (Math.sqrt((x * x) + (y * y)) <= 1.0) {
|
||||||
|
in++;
|
||||||
|
} else {
|
||||||
|
out++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new InOutTuple(in, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main-Methode
|
||||||
|
*
|
||||||
|
* @param args Kommandozeilenparameter (nicht benutzt)
|
||||||
|
*/
|
||||||
|
public static void main(String... args) {
|
||||||
|
var now = System.currentTimeMillis();
|
||||||
|
var result = getResultMonteCarloPiDraw(TOTAL_CYCLES);
|
||||||
|
var time = System.currentTimeMillis() - now;
|
||||||
|
var pi = ((4.0 * result.in()) / (result.in() + result.out()));
|
||||||
|
System.out.println(pi + ", " + time + " ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Laboraufgabe "commonPool"
|
||||||
|
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-03-CommonPool.html)
|
||||||
|
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-Solutions.html#laboraufgabe-commonpool)
|
||||||
|
|
||||||
|
# Materialien
|
||||||
|
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/08-completableFuture.html)
|
||||||
|
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/08-completableFuture.html)
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
# Justfile (https://just.systems/) for starting Maven standard targets
|
||||||
|
|
||||||
|
default: clean compile package
|
||||||
|
|
||||||
|
par parallelism:
|
||||||
|
mvn exec:java -Dexec.mainClass=pp.CommonPoolTest -Djava.util.concurrent.ForkJoinPool.common.parallelism={{parallelism}}
|
||||||
|
|
||||||
|
exec class args: compile
|
||||||
|
mvn exec:java -Dexec.args="{{args}}" -Dexec.mainClass={{class}}
|
||||||
|
# exec class args:
|
||||||
|
# java -cp target/app.jar {{class}} {{args}}
|
||||||
|
clean:
|
||||||
|
mvn clean
|
||||||
|
compile:
|
||||||
|
mvn compile
|
||||||
|
test: compile
|
||||||
|
mvn test
|
||||||
|
javadoc:
|
||||||
|
mvn javadoc:javadoc
|
||||||
|
package:
|
||||||
|
mvn package
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
<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>pp</groupId>
|
||||||
|
<artifactId>pp.08.03-CommonPool</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.release>10</maven.compiler.release>
|
||||||
|
<lombok.version>edge-SNAPSHOT</lombok.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency><!-- für Unit-Tests -->
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.10.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für Lombok -->
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für net.jcip Annotationen -->
|
||||||
|
<groupId>net.jcip</groupId>
|
||||||
|
<artifactId>jcip-annotations</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin><!-- für Unit-Tests [mvn test] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn compile] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.9.0</version>
|
||||||
|
<configuration>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn exec:java] -->
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn javadoc:javadoc] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>3.5.0</version>
|
||||||
|
<configuration>
|
||||||
|
<show>private</show>
|
||||||
|
<locale>en_US</locale>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.ForkJoinPool;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class CommonPoolTest {
|
||||||
|
private static void sleep() {
|
||||||
|
try {
|
||||||
|
Thread.sleep(2000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void output(int task, String event) {
|
||||||
|
var thread = Thread.currentThread().getName();
|
||||||
|
System.out.printf("Task-%02d %s by %s\n", task, event, thread);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String... args)
|
||||||
|
throws InterruptedException, ExecutionException {
|
||||||
|
var cores = Runtime.getRuntime().availableProcessors();
|
||||||
|
var commonPool = ForkJoinPool.commonPool();
|
||||||
|
System.out.println("# Cores: " + cores);
|
||||||
|
System.out.println("# Threads: " + commonPool.getParallelism());
|
||||||
|
var futures = new LinkedList<Future<?>>();
|
||||||
|
for (var task : new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }) {
|
||||||
|
futures.add(commonPool.submit(() -> {
|
||||||
|
output(task, "started");
|
||||||
|
sleep();
|
||||||
|
output(task, "finished");
|
||||||
|
return task;
|
||||||
|
}));
|
||||||
|
output(task, "submitted");
|
||||||
|
}
|
||||||
|
for (var future : futures) {
|
||||||
|
output((int) future.get(), "delivered");
|
||||||
|
}
|
||||||
|
commonPool.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Laboraufgabe "Monte-Carlo-Algorithmus zur Annäherung von pi – mit CompletableFuture"
|
||||||
|
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-04-ConcurrencyMonteCarloPiCF.html)
|
||||||
|
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-Solutions.html#laboraufgabe-monte-carlo-algorithmus-zur-annäherung-von-pi-mit-completablefuture)
|
||||||
|
|
||||||
|
# Materialien
|
||||||
|
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/08-completableFuture.html)
|
||||||
|
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/08-completableFuture.html)
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Justfile (https://just.systems/) for starting Maven standard targets
|
||||||
|
|
||||||
|
default: clean compile package
|
||||||
|
|
||||||
|
exec class args: compile
|
||||||
|
mvn exec:java -Dexec.args="{{args}}" -Dexec.mainClass={{class}}
|
||||||
|
# exec class args:
|
||||||
|
# java -cp target/app.jar {{class}} {{args}}
|
||||||
|
clean:
|
||||||
|
mvn clean
|
||||||
|
compile:
|
||||||
|
mvn compile
|
||||||
|
test: compile
|
||||||
|
mvn test
|
||||||
|
javadoc:
|
||||||
|
mvn javadoc:javadoc
|
||||||
|
package:
|
||||||
|
mvn package
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
<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>pp</groupId>
|
||||||
|
<artifactId>pp.08.04-ConcurrencyMonteCarloPiCF</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.release>16</maven.compiler.release>
|
||||||
|
<lombok.version>edge-SNAPSHOT</lombok.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency><!-- für Unit-Tests -->
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.10.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für Lombok -->
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für net.jcip Annotationen -->
|
||||||
|
<groupId>net.jcip</groupId>
|
||||||
|
<artifactId>jcip-annotations</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin><!-- für Unit-Tests [mvn test] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn compile] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.9.0</version>
|
||||||
|
<configuration>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn exec:java] -->
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn javadoc:javadoc] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>3.5.0</version>
|
||||||
|
<configuration>
|
||||||
|
<show>private</show>
|
||||||
|
<locale>en_US</locale>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
public record InOutTuple(int in, int out) {
|
||||||
|
InOutTuple add(InOutTuple other) {
|
||||||
|
return new InOutTuple(in() + other.in(), out() + other.out());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//record seit Java 15 (preview in Java 14).
|
||||||
|
//identisch zu ...
|
||||||
|
//public final class InOutTuple {
|
||||||
|
// final private int in;
|
||||||
|
// final private int out;
|
||||||
|
//
|
||||||
|
// public InOutTuple(int in, int out) {
|
||||||
|
// this.in = in;
|
||||||
|
// this.out = out;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int in() {
|
||||||
|
// return this.in;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int out() {
|
||||||
|
// return this.out;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public String toString() {
|
||||||
|
// return String.format("InOutTuple [in=%s, out=%s]", this.in, this.out);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public int hashCode() {
|
||||||
|
// var prime = 31;
|
||||||
|
// var result = 1;
|
||||||
|
// result = (prime * result) + this.in;
|
||||||
|
// result = (prime * result) + this.out;
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public boolean equals(Object obj) {
|
||||||
|
// if (this == obj) {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
// if (!(obj instanceof InOutTuple)) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// var other = (InOutTuple) obj;
|
||||||
|
// if (this.in != other.in) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// if (this.out != other.out) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// InOutTuple add(InOutTuple other) {
|
||||||
|
// return new InOutTuple(in() + other.in(), out() + other.out());
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnung von pi durch Monte Carlo Verfahren: Vergleich der Anzahl von
|
||||||
|
* zufällig gewähten Punkten innerhalb eines Viertelkreises (Radius r = 1) bzw.
|
||||||
|
* innerhalb eines Quadrates (Kantenlänge 1) analog zu Fläche eines
|
||||||
|
* Viertelkreises (pi * r^2 / 4) mit der Fläche des Quadrates (1 = 1 * 1).
|
||||||
|
*
|
||||||
|
* Sequenzielle Berechnung (im Main-Thread)
|
||||||
|
*
|
||||||
|
* @author Sandro Leuchter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MonteCarloPiSeq {
|
||||||
|
static final int TOTAL_CYCLES = 10000000;
|
||||||
|
|
||||||
|
public static InOutTuple getResultMonteCarloPiDraw(int cycles) {
|
||||||
|
var in = 0;
|
||||||
|
var out = 0;
|
||||||
|
var r = new Random();
|
||||||
|
for (var i = 0; i < cycles; i++) {
|
||||||
|
var x = r.nextDouble();
|
||||||
|
var y = r.nextDouble();
|
||||||
|
if (Math.sqrt((x * x) + (y * y)) <= 1.0) {
|
||||||
|
in++;
|
||||||
|
} else {
|
||||||
|
out++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new InOutTuple(in, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main-Methode
|
||||||
|
*
|
||||||
|
* @param args Kommandozeilenparameter (nicht benutzt)
|
||||||
|
*/
|
||||||
|
public static void main(String... args) {
|
||||||
|
var now = System.currentTimeMillis();
|
||||||
|
var result = getResultMonteCarloPiDraw(TOTAL_CYCLES);
|
||||||
|
var time = System.currentTimeMillis() - now;
|
||||||
|
var pi = ((4.0 * result.in()) / (result.in() + result.out()));
|
||||||
|
System.out.println(pi + ", " + time + " ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Laboraufgabe "Monte-Carlo-Algorithmus zur Annäherung von pi – mit CompletableFuture"
|
||||||
|
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-04-ConcurrencyMonteCarloPiCF.html)
|
||||||
|
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/08-Solutions.html#laboraufgabe-monte-carlo-algorithmus-zur-annäherung-von-pi-mit-completablefuture)
|
||||||
|
|
||||||
|
# Materialien
|
||||||
|
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/08-completableFuture.html)
|
||||||
|
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/08-completableFuture.html)
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Justfile (https://just.systems/) for starting Maven standard targets
|
||||||
|
|
||||||
|
default: clean compile package
|
||||||
|
|
||||||
|
exec class args: compile
|
||||||
|
mvn exec:java -Dexec.args="{{args}}" -Dexec.mainClass={{class}}
|
||||||
|
# exec class args:
|
||||||
|
# java -cp target/app.jar {{class}} {{args}}
|
||||||
|
clean:
|
||||||
|
mvn clean
|
||||||
|
compile:
|
||||||
|
mvn compile
|
||||||
|
test: compile
|
||||||
|
mvn test
|
||||||
|
javadoc:
|
||||||
|
mvn javadoc:javadoc
|
||||||
|
package:
|
||||||
|
mvn package
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
<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>pp</groupId>
|
||||||
|
<artifactId>pp.08.04-ConcurrencyMonteCarloPiCF_solution</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.release>16</maven.compiler.release>
|
||||||
|
<lombok.version>edge-SNAPSHOT</lombok.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency><!-- für Unit-Tests -->
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.10.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für Lombok -->
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency><!-- für net.jcip Annotationen -->
|
||||||
|
<groupId>net.jcip</groupId>
|
||||||
|
<artifactId>jcip-annotations</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin><!-- für Unit-Tests [mvn test] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn compile] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.9.0</version>
|
||||||
|
<configuration>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn exec:java] -->
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin><!-- [mvn javadoc:javadoc] -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>3.5.0</version>
|
||||||
|
<configuration>
|
||||||
|
<show>private</show>
|
||||||
|
<locale>en_US</locale>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
public record InOutTuple(int in, int out) {
|
||||||
|
InOutTuple add(InOutTuple other) {
|
||||||
|
return new InOutTuple(in() + other.in(), out() + other.out());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//record seit Java 15 (preview in Java 14).
|
||||||
|
//identisch zu ...
|
||||||
|
//public final class InOutTuple {
|
||||||
|
// final private int in;
|
||||||
|
// final private int out;
|
||||||
|
//
|
||||||
|
// public InOutTuple(int in, int out) {
|
||||||
|
// this.in = in;
|
||||||
|
// this.out = out;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int in() {
|
||||||
|
// return this.in;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int out() {
|
||||||
|
// return this.out;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public String toString() {
|
||||||
|
// return String.format("InOutTuple [in=%s, out=%s]", this.in, this.out);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public int hashCode() {
|
||||||
|
// var prime = 31;
|
||||||
|
// var result = 1;
|
||||||
|
// result = (prime * result) + this.in;
|
||||||
|
// result = (prime * result) + this.out;
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public boolean equals(Object obj) {
|
||||||
|
// if (this == obj) {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
// if (!(obj instanceof InOutTuple)) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// var other = (InOutTuple) obj;
|
||||||
|
// if (this.in != other.in) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// if (this.out != other.out) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// InOutTuple add(InOutTuple other) {
|
||||||
|
// return new InOutTuple(in() + other.in(), out() + other.out());
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
@ -0,0 +1,82 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnung von pi durch Monte Carlo Verfahren: Vergleich der Anzahl von
|
||||||
|
* zufällig gewähten Punkten innerhalb eines Viertelkreises (Radius r = 1) bzw.
|
||||||
|
* innerhalb eines Quadrates (Kantenlänge 1) analog zu Fläche eines
|
||||||
|
* Viertelkreises (pi * r^2 / 4) mit der Fläche des Quadrates (1 = 1 * 1).
|
||||||
|
*
|
||||||
|
* Parallele Berechnung (im Common Pool über Completable Future)
|
||||||
|
*
|
||||||
|
* @author Sandro Leuchter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MonteCarloPiCF1 {
|
||||||
|
static final int TOTAL_CYCLES = 10000000;
|
||||||
|
static final int PARALLELISM = 7;
|
||||||
|
|
||||||
|
public static InOutTuple getResultMonteCarloPiDraw(int cycles) {
|
||||||
|
var in = 0;
|
||||||
|
var out = 0;
|
||||||
|
var r = new Random();
|
||||||
|
for (var i = 0; i < cycles; i++) {
|
||||||
|
var x = r.nextDouble();
|
||||||
|
var y = r.nextDouble();
|
||||||
|
if (Math.sqrt((x * x) + (y * y)) <= 1.0) {
|
||||||
|
in++;
|
||||||
|
} else {
|
||||||
|
out++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new InOutTuple(in, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main-Methode
|
||||||
|
*
|
||||||
|
* @param args Kommandozeilenparameter (nicht benutzt)
|
||||||
|
*/
|
||||||
|
public static void main(String... args) {
|
||||||
|
var now = System.currentTimeMillis();
|
||||||
|
var result = CompletableFuture.supplyAsync(
|
||||||
|
() -> getResultMonteCarloPiDraw(TOTAL_CYCLES / PARALLELISM))
|
||||||
|
.thenCombineAsync(
|
||||||
|
CompletableFuture
|
||||||
|
.supplyAsync(() -> getResultMonteCarloPiDraw(
|
||||||
|
TOTAL_CYCLES / PARALLELISM)),
|
||||||
|
(InOutTuple x, InOutTuple y) -> x.add(y))
|
||||||
|
.thenCombineAsync(
|
||||||
|
CompletableFuture
|
||||||
|
.supplyAsync(() -> getResultMonteCarloPiDraw(
|
||||||
|
TOTAL_CYCLES / PARALLELISM)),
|
||||||
|
(InOutTuple x, InOutTuple y) -> x.add(y))
|
||||||
|
.thenCombineAsync(
|
||||||
|
CompletableFuture
|
||||||
|
.supplyAsync(() -> getResultMonteCarloPiDraw(
|
||||||
|
TOTAL_CYCLES / PARALLELISM)),
|
||||||
|
(InOutTuple x, InOutTuple y) -> x.add(y))
|
||||||
|
.thenCombineAsync(
|
||||||
|
CompletableFuture
|
||||||
|
.supplyAsync(() -> getResultMonteCarloPiDraw(
|
||||||
|
TOTAL_CYCLES / PARALLELISM)),
|
||||||
|
(InOutTuple x, InOutTuple y) -> x.add(y))
|
||||||
|
.thenCombineAsync(
|
||||||
|
CompletableFuture
|
||||||
|
.supplyAsync(() -> getResultMonteCarloPiDraw(
|
||||||
|
TOTAL_CYCLES / PARALLELISM)),
|
||||||
|
(InOutTuple x, InOutTuple y) -> x.add(y))
|
||||||
|
.thenCombineAsync(
|
||||||
|
CompletableFuture
|
||||||
|
.supplyAsync(() -> getResultMonteCarloPiDraw(
|
||||||
|
TOTAL_CYCLES / PARALLELISM)),
|
||||||
|
(InOutTuple x, InOutTuple y) -> x.add(y))
|
||||||
|
.join();
|
||||||
|
var time = System.currentTimeMillis() - now;
|
||||||
|
var pi = ((4.0 * result.in()) / (result.in() + result.out()));
|
||||||
|
System.out.println(pi + ", " + time + " ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,60 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ForkJoinPool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnung von pi durch Monte Carlo Verfahren: Vergleich der Anzahl von
|
||||||
|
* zufällig gewähten Punkten innerhalb eines Viertelkreises (Radius r = 1) bzw.
|
||||||
|
* innerhalb eines Quadrates (Kantenlänge 1) analog zu Fläche eines
|
||||||
|
* Viertelkreises (pi * r^2 / 4) mit der Fläche des Quadrates (1 = 1 * 1).
|
||||||
|
*
|
||||||
|
* Parallele Berechnung (im Common Pool über Completable Future)
|
||||||
|
*
|
||||||
|
* @author Sandro Leuchter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MonteCarloPiCF2 {
|
||||||
|
static final int TOTAL_CYCLES = 10000000;
|
||||||
|
static final int PARALLELISM = ForkJoinPool.commonPool().getParallelism();
|
||||||
|
|
||||||
|
public static InOutTuple getResultMonteCarloPiDraw(int cycles) {
|
||||||
|
var in = 0;
|
||||||
|
var out = 0;
|
||||||
|
var r = new Random();
|
||||||
|
for (var i = 0; i < cycles; i++) {
|
||||||
|
var x = r.nextDouble();
|
||||||
|
var y = r.nextDouble();
|
||||||
|
if (Math.sqrt((x * x) + (y * y)) <= 1.0) {
|
||||||
|
in++;
|
||||||
|
} else {
|
||||||
|
out++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new InOutTuple(in, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main-Methode
|
||||||
|
*
|
||||||
|
* @param args Kommandozeilenparameter (nicht benutzt)
|
||||||
|
*/
|
||||||
|
public static void main(String... args) {
|
||||||
|
var now = System.currentTimeMillis();
|
||||||
|
var resultStage = CompletableFuture.supplyAsync(
|
||||||
|
() -> getResultMonteCarloPiDraw(TOTAL_CYCLES / PARALLELISM));
|
||||||
|
for (var i = 1; i < PARALLELISM; i++) {
|
||||||
|
resultStage = resultStage.thenCombineAsync(
|
||||||
|
CompletableFuture
|
||||||
|
.supplyAsync(() -> getResultMonteCarloPiDraw(
|
||||||
|
TOTAL_CYCLES / PARALLELISM)),
|
||||||
|
(InOutTuple x, InOutTuple y) -> x.add(y));
|
||||||
|
}
|
||||||
|
var result = resultStage.join();
|
||||||
|
var time = System.currentTimeMillis() - now;
|
||||||
|
var pi = ((4.0 * result.in()) / (result.in() + result.out()));
|
||||||
|
System.out.println(pi + ", " + time + " ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
package pp;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnung von pi durch Monte Carlo Verfahren: Vergleich der Anzahl von
|
||||||
|
* zufällig gewähten Punkten innerhalb eines Viertelkreises (Radius r = 1) bzw.
|
||||||
|
* innerhalb eines Quadrates (Kantenlänge 1) analog zu Fläche eines
|
||||||
|
* Viertelkreises (pi * r^2 / 4) mit der Fläche des Quadrates (1 = 1 * 1).
|
||||||
|
*
|
||||||
|
* Sequenzielle Berechnung (im Main-Thread)
|
||||||
|
*
|
||||||
|
* @author Sandro Leuchter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MonteCarloPiSeq {
|
||||||
|
static final int TOTAL_CYCLES = 10000000;
|
||||||
|
|
||||||
|
public static InOutTuple getResultMonteCarloPiDraw(int cycles) {
|
||||||
|
var in = 0;
|
||||||
|
var out = 0;
|
||||||
|
var r = new Random();
|
||||||
|
for (var i = 0; i < cycles; i++) {
|
||||||
|
var x = r.nextDouble();
|
||||||
|
var y = r.nextDouble();
|
||||||
|
if (Math.sqrt((x * x) + (y * y)) <= 1.0) {
|
||||||
|
in++;
|
||||||
|
} else {
|
||||||
|
out++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new InOutTuple(in, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main-Methode
|
||||||
|
*
|
||||||
|
* @param args Kommandozeilenparameter (nicht benutzt)
|
||||||
|
*/
|
||||||
|
public static void main(String... args) {
|
||||||
|
var now = System.currentTimeMillis();
|
||||||
|
var result = getResultMonteCarloPiDraw(TOTAL_CYCLES);
|
||||||
|
var time = System.currentTimeMillis() - now;
|
||||||
|
var pi = ((4.0 * result.in()) / (result.in() + result.out()));
|
||||||
|
System.out.println(pi + ", " + time + " ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue