diff options
author | Julian T <julian@jtle.dk> | 2020-04-06 12:45:48 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-04-06 13:03:47 +0200 |
commit | 0f5844da5e872ac20ea145eaa682238ec8c99385 (patch) | |
tree | 4d075d540a56ec143efe8db30edbee10b0673afc /sem4/hpp/miniproject | |
parent | a2e57f7df5aed74991002b4940c7f1152d83235b (diff) |
First version of multiprocessing
Not working
Diffstat (limited to 'sem4/hpp/miniproject')
-rw-r--r-- | sem4/hpp/miniproject/multiproc.py | 107 | ||||
-rw-r--r-- | sem4/hpp/miniproject/optimised.py | 63 |
2 files changed, 141 insertions, 29 deletions
diff --git a/sem4/hpp/miniproject/multiproc.py b/sem4/hpp/miniproject/multiproc.py new file mode 100644 index 0000000..be39326 --- /dev/null +++ b/sem4/hpp/miniproject/multiproc.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 + +import numpy as np +import matplotlib.pyplot as plt +import time +import multiprocessing as mp +from multiprocessing import RawArray + +# c-mesh limits +limitre = ( -2, 1 ) +limitim = ( -1.5, 1.5 ) + + +def worker(gridchunk, s, step, T, l): + global rs + # Preallocate z array + rschunk = rs[s : s + step] + + z = np.zeros(rschunk.shape) + print(f"rschunk.shape: {rschunk.shape}, gridchunk.shape: {gridchunk.shape}, T: {T}, l: {l}") + + # Calculate ι for all complex numbers + for i in range(l): + # This will generate warnings for some of the values rising above T. + # Because these values are above T they are not used, thus the warnings + # can be ignored + z = z*z + gridchunk + + # This will generate 1 in all the places + # where z < T and zeros elsewhere + below = (np.abs(z) < T) + + # Add this to the result + # Because the ones that pass T are 0 + # they will stop counting. + # + # If a specific z never reaches >= T its value in rs will + # be l + rschunk += below + + np.divide(rschunk, l, out=rschunk) + +def mangel(pre, pim, T, l, workers): + """ + Calculate the mangelbrot image with multiple processes + (pre, pim) discribes the image size. Use T and l to tune the mangelbrot + + This will split the image in horizontal parts and distribute it + between the workers. + Because the result array is row major, data will be nicely together if + the workers work with rows not columns. + + Pre must be devisible by workers. + + The result is saved in rs. Sorry couln't get numpy references through to the process as arguments + + :param pre: Number of real numbers used + :param pim: Number of imaginary numbers + :param T: Mangelbrot threshold + :param l: Iterations + :param workers: Number of workers. + """ + + # Used to calculate c-mesh + re = np.linspace(limitre[0], limitre[1], pre) + im = np.linspace(limitim[0], limitim[1], pim) + + # Calculate c-mesh + grid = np.add.outer(re, 1j * im) + + # Calculate the partition variables + step = int(pre / workers) + + + # Loop chunks and start the workers + wl = [] + for s in range(0, pre, step): + gridchunk = grid[s : s + step] + p = mp.Process(target=worker, args=(gridchunk, s, step, T, l)) + wl.append(p) + p.start() + + # Wait for them to be done + for p in wl: + p.join() + + return rs + +rs = np.full((500, 500), 0.5) + +start = time.time() +arr = mangel(500, 500, 2, 100, 1) +end = time.time() + +plt.imshow(arr, cmap=plt.cm.hot, vmin=0, vmax=1) +plt.savefig("mult.png") + +print(f"Took {end - start} seconds") +""" + p = mp.Process(target=worker, args=(rs[s:s + step], grid[s:s + step], T, l)) + wl.append(p) + p.start() + + # Wait for them to be done + for p in wl: + p.join() +""" diff --git a/sem4/hpp/miniproject/optimised.py b/sem4/hpp/miniproject/optimised.py index 64af72d..2d128c9 100644 --- a/sem4/hpp/miniproject/optimised.py +++ b/sem4/hpp/miniproject/optimised.py @@ -8,53 +8,58 @@ import time limitre = ( -2, 1 ) limitim = ( -1.5, 1.5 ) -def lota(c, T, l): - z = 0 - for i in range(l): - nz = z*z + c - - # Check if we found or z - if np.abs(nz) > T: - break - - z = nz - else: - # If we did not find z, use l - return l - - return np.abs(z) - def mangel(pre, pim, T, l): + """ + Calculate the mangelbrot image + (pre, pim) discribes the image size. Use T and l to tune the mangelbrot + This function uses the global variables limitre and limitim to determine + the c-mesh range. + + :param pre: Number of real numbers used + :param pim: Number of imaginary numbers + :param T: Mangelbrot threshold + :param l: Iterations + """ # Preallocate result array and z array rs = np.zeros((pre, pim)) z = np.zeros((pre, pim)) - # Calculate C matrix + # Used to calculate c-mesh re = np.linspace(limitre[0], limitre[1], pre) im = np.linspace(limitim[0], limitim[1], pim) - # Calculate C by multiplying the scalers in. Remember to move it to the beggining og the c-mesh limit + # Calculate c-mesh grid = np.add.outer(re, 1j * im) + # Calculate ι for all complex numbers for i in range(l): + # This will generate warnings for some of the values rising above T. + # Because these values are above T they are not used, thus the warnings + # can be ignored z = z*z + grid - # Extract all the ones that are under the threshold + # This will generate 1 in all the places + # where z < T and zeros elsewhere below = (np.abs(z) < T) + # Add this to the result + # Because the ones that pass T are 0 + # they will stop counting. + # + # If a specific z never reaches >= T its value in rs will + # be l rs += below - - rs[ rs==rs.max() ] = l + rs /= l - + return rs -start = time.time() -arr = mangel(500, 500, 2, 100) -end = time.time() +if __name__ == "__main__": + start = time.time() + arr = mangel(500, 500, 2, 100) + end = time.time() -plt.imshow(arr, cmap=plt.cm.hot, vmin=0, vmax=1) -plt.savefig("test.png") -plt.savefig("test.pdf") + plt.imshow(arr, cmap=plt.cm.hot, vmin=0, vmax=1) + plt.savefig("opt.png") -print(f"Took {end - start} seconds") + print(f"Took {end - start} seconds") |