0

I am coding a Mandelbrot set generator and I have parallelized it with 4 subprocesses, each one calculate a different region of Mandelbrot set.

The parallelizing code is the following:

def run_threads(self):    # Parallelize calcs with 4 subprocesses
        with Pool(processes=4) as pool:
            p1 = pool.apply_async(self.run, (0, int(self.width / 2), 0, int(self.width / 2)))
            p2 = pool.apply_async(self.run, (0, int(self.width / 2), int(self.width / 2), self.width))
            p3 = pool.apply_async(self.run, (int(self.width / 2), self.width, 0, int(self.width / 2)))
            p4 = pool.apply_async(self.run, (int(self.width / 2), self.width, int(self.width / 2), self.width))

            r1 = (p1.get(timeout=10))
            r2 = (p2.get(timeout=10))
            r3 = (p3.get(timeout=10))
            r4 = (p4.get(timeout=10))

        #r2 = self.run(0, int(self.width / 2), int(self.width / 2), self.width)

        for i in range(0, int(self.width / 2)):
            for j in range(0, int(self.width / 2)):
                self.values[i, j] = r1[i,j]


        for i in range(0, int(self.width / 2)):
            for j in range(int(self.width / 2), self.width):
                self.values[i, j] = r2[i,j]


        for i in range(int(self.width / 2), self.width):
            for j in range(0, int(self.width / 2)):
                self.values[i, j] = r3[i,j]


        for i in range(int(self.width / 2), self.width):
            for j in range(int(self.width / 2), self.width):
                self.values[i, j] = r4[i,j]

but the output is this

The two correctly represented regions correspond to r1 and r4, so r2 and r3 arent working properly. If I run the commented code for r2, the output is now correct, so I dont know what I am doing wrong.

PS: self.values and values are the color of each pixel.

Thank you

Midepizzas
  • 11
  • 1
  • Is your `self.run` function doesn't change any properties? – Mikhail Berlinkov Dec 12 '18 at 20:09
  • it changes an array, but as subprocesses cant work with memory, also return that array and work with it – Midepizzas Dec 22 '18 at 10:40
  • As per [documentation](https://docs.python.org/3.4/library/multiprocessing.html?highlight=process#sharing-state-between-processes) one should avoid using shared state as far as possible. This is particularly true when using multiple processes. In your case, I think it's quite easy to rewrite your code so that it didn't use shared state. – Mikhail Berlinkov Dec 22 '18 at 14:14

0 Answers0