0

I need to implement multiprocessing on this code. Basically it makes a grid of nxn data in the using numpy linspace, and enters on a while loop checking a condition on the t parameter. On each run inside the while loop it calculates z for each point in the grid and refreshes the value of t. It runs until a certain condition for t is met. Then it just plots the data.

I've read about the multiprocessing package but really have no idea how to implement it. I've found the sources a bit obscure for newbies and couldn't find any example to base on.

Any comment is welcome, thanks in advance!

import numpy as np
import matplotlib.pyplot as plt
import time 

##################

def function(x,y,z):

    t=1

    while t<20:

        for i in range(n):
            for j in range(n):

                z[i,j]=z[i,j]+(i+j)/n**2

        t=t+1                                   ### this is just a dummy operation used to
        time.sleep(1.0)                         #### simulate the actual computations

    return z

def plot(x,y,z):

    plt.contourf(x,y,z,20,cmap="viridis")
    plt.colorbar()
    plt.show()

#################

start_time = time.time()

n=30
t=1

x=np.linspace(0,10,n)
y=np.linspace(0,10,n)
z=np.zeros((n,n))

function(x,y,z)
plot(x,y,z)

print("--- %s seconds ---" % (time.time() - start_time))
eulag
  • 11
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) from the intro tour. Stack Overflow is not a code-writing service, nor a personal tutorial resource. "Teach me how to write this block of code" is not a Stack Overflow issue. – Prune Apr 24 '20 at 23:34
  • Oh I'm sorry, I thought that if I posted every piece of code I unsuccesfully tried to run would be pointless and a waste of time for the reader. And by the way your phrasing is at least rude, I don't think it's a good way to answer newcomers like that if you want to keep a polite community. – eulag Apr 25 '20 at 00:41

0 Answers0