Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

python - How to get return value of different functions in a for loop with multiprocessing

I'm currently making some numerical solver for current simulation. To make my code faster, I made a function that returns the result of elementwise matrix multiplication, and gradient... and so on.

    def mmul(A, B, procname, return_dict):
        return_dict[procname] = np.multiply(A,B)
    def mgrad(A, procname, return_dict):
        return_dict[procname] = np.gradient(A/dx)
    def madd(A, B, procname, return_dict):
        return_dict[procname] = A+B

Now here's the body of the code. I first made a dictionary(return_dict) and store the results for each processing units, and get the values(Vgrad, Pgrad, Psquare) from the dictionary.

    for k in range(0,max_iter-1, 1):
        #0. Firstly generate all of the auxiliay calculation arrays 
        post_V, post_p, Vij_coeff = np.zeros((3, lx, ly), dtype = float)

        # Calculate carrier density of the next step
        processes = []
        #----------------------------                                 # Const/mtx for calculating p
        manager = multiprocessing.Manager()
        return_dict = manager.dict()
        p0 = multiprocessing.Process(target = mgrad, args = (V, Vgrad, return_dict))
        processes.append(p0)
        p0.start()
        p1 = multiprocessing.Process(target = mgrad, args = (p, Pgrad, return_dict))
        processes.append(p1);p1.start()
        p2 = multiprocessing.Process(target = mmul, args = (p,p, Psquare, return_dict))
        processes.append(p2);p2.start()
        for process in processes:
            process.join()
        Vgrad = return_dict['Vgrad']
        Pgrad = return_dict['Pgrad']
        Psquare = return_dict['Psquare']

However, this code makes the error below

    PicklingError: Can't pickle <function mgrad at 0x000002776C3614C8>: it's not the same object as __main__.mgrad

Is there any solutions to get the calculated value of the function, while running in multiprocessor?

question from:https://stackoverflow.com/questions/65878819/how-to-get-return-value-of-different-functions-in-a-for-loop-with-multiprocessin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...