mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-26 06:48:04 +02:00
misc
This commit is contained in:
parent
1cc20da665
commit
b18561fc54
19 changed files with 197 additions and 0 deletions
|
@ -73,6 +73,25 @@ die auf mehreren Prozessoren laufen.
|
|||
\end{beispiel}
|
||||
\footnotetext{WS 2013/2014, Kapitel 41, Folie 26}
|
||||
|
||||
\section{Beispiele}
|
||||
|
||||
Die folgenden Quelltexte wurden von Axel Busch erstellt.
|
||||
|
||||
Das folgende Programm läuft in ca. 4min und $\SI{36}{\second}$ Sekunden auf einem Kern einer
|
||||
Intel Pentium P6200 CPU:
|
||||
|
||||
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=SingleCorePrimeTest.java]{java}{scripts/java/SingleCorePrimeTest.java}
|
||||
|
||||
Der folgende Code Testet das ganze mit mehreren Kernen auf einer Intel Pentium
|
||||
P6200 CPU:
|
||||
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=MultipleCorePrimeTest.java]{java}{scripts/java/MultipleCorePrimeTest.java}
|
||||
|
||||
\begin{itemize}
|
||||
\item 1 thread: 4min 38s
|
||||
\item 2 threads: 3min 14s
|
||||
\item 4 threads: 2min 44s
|
||||
\item 8 threads: 2min 41s
|
||||
\end{itemize}
|
||||
|
||||
\section{Literatur}
|
||||
\begin{itemize}
|
||||
|
|
Binary file not shown.
|
@ -90,6 +90,16 @@ die Klasse.
|
|||
|
||||
Das folgende Wetter-Beispiel zeigt, wie man Aktoren benutzen kann.
|
||||
|
||||
\section{Weiteres}
|
||||
\texttt{def awaitAll(timeout: Long, fts: Future[Any]*):}\\
|
||||
\-\hspace{1.8cm}\texttt{List[Option[Any]]}\xindex{awaitAll}\\
|
||||
\-\hspace{0.8cm}$\leftharpoonup$ \texttt{scala.actors.Futures.\_}
|
||||
|
||||
Waits until either all futures are resolved or a given time span has passed. Results are collected in a list of options. The result of a future that resolved during the time span is its value wrapped in Some. The result of a future that did not resolve during the time span is None.
|
||||
|
||||
Note that some of the futures might already have been awaited, in which case their value is returned wrapped in Some. Passing a timeout of 0 causes awaitAll to return immediately.
|
||||
|
||||
|
||||
\section{Beispiele}
|
||||
\subsection{Wetter}
|
||||
Das folgende Script sendet parallel Anfragen über verschiedene ZIP-Codes an
|
||||
|
@ -99,6 +109,7 @@ die Yahoo-Server, parst das XML und extrahiert die Stadt sowie die Temperatur:
|
|||
|
||||
\section{Weitere Informationen}
|
||||
\begin{itemize}
|
||||
\item \url{http://www.scala-lang.org/api}
|
||||
\item \url{http://docs.scala-lang.org/style/naming-conventions.html}
|
||||
\end{itemize}
|
||||
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,78 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class MultipleCorePrimeTest {
|
||||
public static void count(int target, int threads)
|
||||
throws InterruptedException, TimeoutException {
|
||||
ExecutorService executor =
|
||||
Executors.newFixedThreadPool(threads);
|
||||
List<FutureTask<Integer>> taskList =
|
||||
new ArrayList<FutureTask<Integer>>();
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
for (int i = 1; i <= threads; ++i) {
|
||||
int ilen = target / threads;
|
||||
|
||||
/* Test following intervall for primes */
|
||||
final int start = (i - 1) * ilen;
|
||||
final int end = (i != threads)
|
||||
? i * ilen - 1
|
||||
: target;
|
||||
FutureTask<Integer> task =
|
||||
new FutureTask<Integer>(
|
||||
new Callable<Integer>() {
|
||||
@Override
|
||||
public Integer call() {
|
||||
int count = 0;
|
||||
for (int i = start; i <= end;
|
||||
++i) {
|
||||
if (SingleCorePrimeTest.
|
||||
isPrime(i))
|
||||
++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
});
|
||||
taskList.add(task);
|
||||
executor.submit(task);
|
||||
}
|
||||
|
||||
executor.shutdown();
|
||||
if (!executor.awaitTermination(10,
|
||||
TimeUnit.MINUTES)) {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
final long endTime = System.currentTimeMillis();
|
||||
int count = 0;
|
||||
for (int i = 0; i < taskList.size(); ++i) {
|
||||
try {
|
||||
count += taskList.get(i).get();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.out.println(threads + " thread: "
|
||||
+ (endTime - startTime) + " ms");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
final int target = 100_000_000;
|
||||
try {
|
||||
count(target, 1);
|
||||
count(target, 2);
|
||||
count(target, 4);
|
||||
count(target, 8);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,27 @@
|
|||
import java.util.Arrays;
|
||||
|
||||
public class SieveOfErasthostenes {
|
||||
|
||||
public static boolean[] sieveIt(int n) {
|
||||
boolean[] sieve = new boolean[n+1];
|
||||
Arrays.fill(sieve, true);
|
||||
sieve[0] = false;
|
||||
sieve[1] = false;
|
||||
for (int i=2; i <= Math.sqrt(n); i++) {
|
||||
if (sieve[i]) {
|
||||
for (int c = i*i; c <= n; c += i) {
|
||||
sieve[c] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return sieve;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
final int n = 100_000_000;
|
||||
final long startTime = System.currentTimeMillis();
|
||||
sieveIt(n);
|
||||
final long endTime = System.currentTimeMillis();
|
||||
System.out.println(endTime-startTime);
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,27 @@
|
|||
/* @author Axel Busch */
|
||||
public class SingleCorePrimeTest {
|
||||
|
||||
public static boolean isPrime(int n) {
|
||||
if (n < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 2; i <= Math.sqrt(n); ++i) {
|
||||
if (n % i == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int target = 10_000_000;
|
||||
long start = System.currentTimeMillis();
|
||||
for (int i = 2; i <= target; ++i) {
|
||||
isPrime(i);
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println(end-start);
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,35 @@
|
|||
import scala.actors.Futures._
|
||||
/** @author Axel Busch */
|
||||
object EratosthenesFutures {
|
||||
def parallel(end : Int, threads: Int) : Array[Boolean] = {
|
||||
val sieve = Array.fill[Boolean](end+1)(true)
|
||||
for (i <- 2 to scala.math.sqrt(end).toInt) {
|
||||
if (sieve(i)) {
|
||||
val nextComposite = i*i
|
||||
val compositesPerThread = (end-nextComposite+1)/(threads*i)
|
||||
val tasks = for (t <- 0 until threads) yield future {
|
||||
val begin = nextComposite + t * i *
|
||||
compositesPerThread
|
||||
val finish = if (t+1 == threads) end
|
||||
else nextComposite + (t+1) * i *
|
||||
compositesPerThread
|
||||
assert (begin % i == 0)
|
||||
for (composite <- begin to finish by i) {
|
||||
sieve(composite) = false
|
||||
}
|
||||
}
|
||||
awaitAll(20000L, tasks: _*);
|
||||
}
|
||||
}
|
||||
sieve
|
||||
}
|
||||
def main(args: Array[String]) = {
|
||||
val end = 100000000
|
||||
for (threads <- List(1,2,4,8)) {
|
||||
val startTime = System.currentTimeMillis();
|
||||
parallel(end,threads);
|
||||
val endTime = System.currentTimeMillis();
|
||||
println(threads + " thread: " + (endTime - startTime) + " ms");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue