From 8b506b6433c2cc786b587912d2c8aac85857dcc1 Mon Sep 17 00:00:00 2001 From: Alberto Venturini Date: Sat, 16 Sep 2017 11:50:53 +0200 Subject: [PATCH] Added ThreadedJuliaSetCalculator --- src/ForkJoinJuliaSetCalculator.java | 7 ++- src/JuliaSetPanel.java | 2 +- src/MainClass.java | 6 +-- src/ThreadedJuliaSetCalculator.java | 75 +++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 src/ThreadedJuliaSetCalculator.java diff --git a/src/ForkJoinJuliaSetCalculator.java b/src/ForkJoinJuliaSetCalculator.java index 8e90b01..f502224 100644 --- a/src/ForkJoinJuliaSetCalculator.java +++ b/src/ForkJoinJuliaSetCalculator.java @@ -55,7 +55,10 @@ public class ForkJoinJuliaSetCalculator implements JuliaSetCalculator { int middle = (startInclusive + endExclusive) / 2; JuliaTask task1 = new JuliaTask(startInclusive, middle); JuliaTask task2 = new JuliaTask(middle, endExclusive); - invokeAll(task1, task2); + //invokeAll(task1, task2); + task1.fork(); + task2.compute(); + task1.join(); // Interestingly, if I comment out this join, it seems to work ok??? } } } @@ -70,7 +73,7 @@ public class ForkJoinJuliaSetCalculator implements JuliaSetCalculator { System.out.println(endTime - startTime); return iterations; - + } } diff --git a/src/JuliaSetPanel.java b/src/JuliaSetPanel.java index e3627ae..f252fcf 100644 --- a/src/JuliaSetPanel.java +++ b/src/JuliaSetPanel.java @@ -11,7 +11,7 @@ public class JuliaSetPanel extends JPanel { public JuliaSetPanel(final JuliaSetCalculator juliaSetCalculator, final int width, final int height) { this.juliaSetCalculator = juliaSetCalculator; - setPreferredSize(new Dimension(1200, 800)); + setPreferredSize(new Dimension(width, height)); setBackground(Color.white); } diff --git a/src/MainClass.java b/src/MainClass.java index 17f3a92..308e4d9 100644 --- a/src/MainClass.java +++ b/src/MainClass.java @@ -5,9 +5,9 @@ public class MainClass { public static void main(String[] args) { - int width = 1200; - int height = 800; - JuliaSetCalculator juliaSetCalculator = new ForkJoinJuliaSetCalculator(width, height); + int width = 1600; + int height = 1000; + JuliaSetCalculator juliaSetCalculator = new ThreadedJuliaSetCalculator(width, height); JuliaSetPanel juliaSetPanel = new JuliaSetPanel(juliaSetCalculator, width, height); SwingUtilities.invokeLater(() -> { diff --git a/src/ThreadedJuliaSetCalculator.java b/src/ThreadedJuliaSetCalculator.java new file mode 100644 index 0000000..da379c3 --- /dev/null +++ b/src/ThreadedJuliaSetCalculator.java @@ -0,0 +1,75 @@ +public class ThreadedJuliaSetCalculator implements JuliaSetCalculator { + + private int width; + private int height; + private int[][] iterations; + + public ThreadedJuliaSetCalculator(final int width, final int height) { + this.width = width; + this.height = height; + iterations = new int[width][height]; + } + + @Override + public int[][] calculate(int maxIterations, double zoom, double cx, double cy, double moveX, double moveY) { + + class JuliaTask implements Runnable { + + private int startInclusive; + private int endExclusive; + + JuliaTask(int startInclusive, int endExclusive) { + this.startInclusive = startInclusive; + this.endExclusive = endExclusive; + } + + @Override + public void run() { + double zx, zy; + + for(int x = startInclusive; x < endExclusive; x++) { + for (int y = 0; y < height; y++) { + zx = 1.5 * (x - width / 2) / (0.5 * zoom * width) + moveX; + zy = (y - height / 2) / (0.5 * zoom * height) + moveY; + int i = maxIterations; + while (zx * zx + zy * zy < 4 && i > 0) { + double tmp = zx * zx - zy * zy + cx; + zy = 2.0 * zx * zy + cy; + zx = tmp; + i--; + } + iterations[x][y] = i; + } + } + } + } + + long startTime = System.currentTimeMillis(); + + int NUM_THREADS = 128; + Thread[] threads = new Thread[NUM_THREADS]; + int step = width / NUM_THREADS; + + for(int i = 0; i < NUM_THREADS; i++) { + int start = i * step; + int end = (i + 1) * step; + if(end > width || i == NUM_THREADS - 1) end = width; + threads[i] = new Thread(new JuliaTask(start, end)); + threads[i].start(); + } + + for(int i = 0; i < NUM_THREADS; i++) { + try { + threads[i].join(); + } catch(Exception e) { + // No! + } + } + + long endTime = System.currentTimeMillis(); + System.out.println(endTime - startTime); + + return iterations; + } + +}