Skip to content

Commit 4b116c2

Browse files
committed
added a data-parallel version of Mandelbrot calculation using collections from javaslang, performs horribly though
1 parent 6d2dd35 commit 4b116c2

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.mgu.parallel.data.mandelbrot;
2+
3+
import javaslang.collection.Array;
4+
import javaslang.collection.Seq;
5+
import javaslang.concurrent.Future;
6+
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
10+
public class Mandelbrot {
11+
12+
private static final int DEFAULT_MAX_ITERATIONS = 1_000;
13+
14+
private static final double XR = -2.0;
15+
16+
private static final double XI = 1.25;
17+
18+
private static final double YR = 1.2;
19+
20+
private static final double YI = -1.25;
21+
22+
private final ExecutorService executorService;
23+
24+
private final int width;
25+
26+
private final int height;
27+
28+
public Mandelbrot(final int width, final int height) {
29+
this(width, height, Executors.newFixedThreadPool(8));
30+
}
31+
32+
public Mandelbrot(final int width, final int height, final ExecutorService executorService) {
33+
this.width = width;
34+
this.height = height;
35+
this.executorService = executorService;
36+
}
37+
38+
public Future<Seq<Integer>> computeParallel(final Array<Integer> withIndicesInCells) {
39+
return Future.traverse(executorService, withIndicesInCells, this::singleComputation);
40+
}
41+
42+
private Future<Integer> singleComputation(final Integer index) {
43+
final int x = index % width;
44+
final int y = index / width;
45+
46+
final double xc = XR + (XI - XR) * x / width;
47+
final double yc = YR + (YI - YR) * y / height;
48+
49+
return Future.of(() -> mandelbrot(xc, yc));
50+
}
51+
52+
private Integer mandelbrot(final double xc, final double yc) {
53+
return mandelbrot(xc, yc, DEFAULT_MAX_ITERATIONS);
54+
}
55+
56+
private Integer mandelbrot(final double xc, final double yc, final int maxIterations) {
57+
Integer i = 0;
58+
double x = 0.0;
59+
double y = 0.0;
60+
while (x * x + y * y < 4 && i < maxIterations) {
61+
double xt = x * x - y * y + xc;
62+
double yt = 2 * x * y + yc;
63+
x = xt;
64+
y = yt;
65+
i++;
66+
}
67+
return i;
68+
}
69+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.mgu.parallel.data.mandelbrot;
2+
3+
import javaslang.collection.Array;
4+
import org.openjdk.jmh.annotations.BenchmarkMode;
5+
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
6+
import org.openjdk.jmh.annotations.Mode;
7+
import org.openjdk.jmh.annotations.OutputTimeUnit;
8+
import org.openjdk.jmh.annotations.State;
9+
10+
import java.util.concurrent.TimeUnit;
11+
12+
@State
13+
public class MandelbrotBenchmark {
14+
15+
private static final int LENGTH_SIDE_A = 1_000;
16+
17+
private static final int LENGTH_SIDE_B = 1_000;
18+
19+
private final Mandelbrot mandelbrot;
20+
21+
public MandelbrotBenchmark() {
22+
this.mandelbrot = new Mandelbrot(LENGTH_SIDE_A, LENGTH_SIDE_B);
23+
}
24+
25+
@GenerateMicroBenchmark
26+
@BenchmarkMode(Mode.AverageTime)
27+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
28+
public void runMandelbrotDataParallel() {
29+
final Array<Integer> iterationsPerCoordinate = Array.range(0, LENGTH_SIDE_A * LENGTH_SIDE_B);
30+
mandelbrot.computeParallel(iterationsPerCoordinate);
31+
}
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.mgu.parallel.data.mandelbrot;
2+
3+
import org.openjdk.jmh.Main;
4+
5+
public class MandelbrotStarter {
6+
7+
public static void main(String[] args) {
8+
Main.main(args);
9+
}
10+
}

src/main/java/com/mgu/parallel/task/mandelbrot/MandelbrotBenchmark.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,29 @@
1111
@State
1212
public class MandelbrotBenchmark {
1313

14+
private static final int LENGTH_SIDE_A = 1_000;
15+
16+
private static final int LENGTH_SIDE_B = 1_000;
17+
1418
private final Mandelbrot mandelbrot;
1519

1620
public MandelbrotBenchmark() {
17-
this.mandelbrot = new Mandelbrot(4000, 4000);
21+
this.mandelbrot = new Mandelbrot(LENGTH_SIDE_A, LENGTH_SIDE_B);
1822
}
1923

2024
@GenerateMicroBenchmark
2125
@BenchmarkMode(Mode.AverageTime)
2226
@OutputTimeUnit(TimeUnit.MILLISECONDS)
2327
public void runMandelbrotSequential() {
24-
final int[] iterationsPerCoordinate = new int[4000*4000];
28+
final int[] iterationsPerCoordinate = new int[LENGTH_SIDE_A * LENGTH_SIDE_B];
2529
mandelbrot.computeSequential(iterationsPerCoordinate, 0, iterationsPerCoordinate.length);
2630
}
2731

2832
@GenerateMicroBenchmark
2933
@BenchmarkMode(Mode.AverageTime)
3034
@OutputTimeUnit(TimeUnit.MILLISECONDS)
3135
public void runMandelbrotParallel() {
32-
final int[] iterationsPerCoordinate = new int[4000*4000];
36+
final int[] iterationsPerCoordinate = new int[LENGTH_SIDE_A * LENGTH_SIDE_B];
3337
mandelbrot.computeParallel(iterationsPerCoordinate, 0, iterationsPerCoordinate.length);
3438
}
3539
}

0 commit comments

Comments
 (0)