0

I have a for loop which calculates a number in each iteration and sets it as a new element in a zeros array. How to parallelize this loop with mpi4py? Help is much appreciated! Code is given below.


    b = 1000
    R = numpy.linspace(2, 2.5, b)
    z = numpy.zeros_like(R)
    for i in range(len(z)):
        z[i] = calculate(R[i]) #pseudocode


There is some example of parallelizing a for loop which adds numbers on this website ([https://computationalmechanics.in/parallelizing-for-loop-in-python-with-mpi/]), but I don't even know how to begin! (Beside the part on the beggining which I understand, setting the rank, size, etc).

3
  • 1
    Why mpi4py in particular? I'd start with seeing if you can numba.jit the calculate function, and if it's not fast enough then, try e.g. plain old multiprocessing. Commented Nov 14, 2023 at 10:23
  • As an aside: z = [calculate(x) for x in numpy.linspace(2, 2.5, b)] would be more succinct... Commented Nov 14, 2023 at 10:23
  • I guess that works too, though even multiprocessing is abstract and not understandable to me. Also, there is no single calculate function, that actually represents like 5 separate functions and like 100 lines of code but agreed, that's more succinct. Commented Nov 14, 2023 at 10:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.