Draw this picture as quickly as possible!
To determine if $c$ is in the Mandelbrot set $M$:
If $z_n$ remains bounded, $c$ is in $M$; otherwise $c$ is not in $M$.
Make a grid of pixels!
Choose parameters:
Given a complex number $c$:
Given a complex number $c$:
Input:
Output:
Goal:
mandelbrot.zip
public static float getValue (ComplexNumber c) {
ComplexNumber z = new ComplexNumber(0, 0);
int iter = 0;
while (iter < MAX_ITER && z.modulus() <= MAX_MODULUS) {
z = z.times(z).plus(c);
iter++;
}
return (float) (MAX_ITER - iter) / MAX_ITER;
}
private void updateBitmap () {
for (int i = 0; i < BOX_WIDTH; i++) {
for (int j = 0; j < BOX_HEIGHT; j++) {
ComplexNumber c = getValueFromIndices(i, j);
float val = Mandelbrot.getValue(c);
bitmap[i][j] = colorMap(val);
}
}
}
One thread per task
Created Thread
s and ran them in parallel
Runnable
interfacestart
instancesjoin
to wait until threads finishThread
s has significant overhead
When tasks are fairly homogenous (e.g., computing $\pi$, shortcuts) previous approach is good
A nice Java feature: thread pools
Executor
interface
void execute(Runnable command)
methodExecutorService
interface:
ExecutorService
ImplementationsFrom java.util.concurrent.Executors
:
newFixedThreadPool(int nThreads)
newSingleThreadExecutor()
newCachedThreadPool()
Define tasks
public class MyTask implements Runnable {
...
public void run () {
...
}
}
Create a pool, e.g., fixed thread pool
int nThreads = ...;
ExecutorService pool = Exercutors.newFixedThreadPool(nThreads);
Create and execute tasks
MyTask task = new MyTask(...);
pool.execute(task);
Shutting down the pool
pool.shutdown();
Wait for all pending processes to complete (like join()
method)
try {
pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
// do nothing
}
Shortcuts from Lab 02:
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
float min = Float.MAX_VALUE;
for (int k = 0; k < size; ++k) {
float x = matrix[i][k]; float y = matrix[k][j];
float z = x + y;
if (z < min)
min = z;
}
shortcuts[i][j] = min;
}
}
For fixed row i
, col j
:
float min = Float.MAX_VALUE;
for (int k = 0; k < size; ++k) {
float x = matrix[i][k]; float y = matrix[k][j];
float z = x + y;
if (z < min)
min = z;
}
shortcuts[i][j] = min;
Approach 1:
size * size
threadsApproach 2:
availableProcessors()
executer-shortcuts.zip