Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Approximating π using parallelisation #57

Open
dpshelio opened this issue Dec 15, 2022 · 0 comments
Open

Approximating π using parallelisation #57

dpshelio opened this issue Dec 15, 2022 · 0 comments
Labels
week10 Performance

Comments

@dpshelio
Copy link
Contributor

dpshelio commented Dec 15, 2022

This exercise builds on #54. It is part of a series that looks at execution time of different ways to calculate π using the same Monte Carlo approach.

This exercise uses the Message Passing Interface (MPI) to accomplish this approximation of π. The code is already written, and you can find it in calc_pi_mpi.py in the pi_calculation repository, on the class branch. Your job is to install MPI, and measure how much time it takes to complete in comparison to #54.

MPI

MPI allows parallelisation of computation. An MPI program consists of multiple processes, existing within a group called a communicator. The default communicator contains all available processes and is called MPI_COMM_WORLD.

Each process has its own rank and can execute different code. A typical way of using MPI is to divide the computation into smaller chunks, have each process deal with a chunk, and have one "main" process to coordinate this and gather all the results. The processes can communicate with each other in pre-determined ways as specified by the MPI protocol -- for example, sending and receiving data to a particular process, or broadcasting a message to all processes.

Preparation

We are going to run the original (non-numpy) version in parallel, and compare it to the non-parallel version.

We will be using mpi4py, a Python library that gives us access to MPI functionality.

Install mpi4py using conda:

conda install mpi4py -c conda-forge

or pip:

pip install mpi4py

On windows you will also need to install MS MPI.

The MPI version of the code is available at calc_pi_mpi.py. Look at the file and try to identify what it is doing -- it's fine if you don't understand all the details! Can you see how the concepts in the brief description of MPI above are reflected in the code?

Execution

  1. Run the MPI version as:
    mpiexec -n 4 python calc_pi_mpi.py
    The -n argument controls how many processes you start.
  2. Increase the number of points and proceses, and compare the time it takes against the normal version. Note that to pass arguments to the python file (like -np below), we have to give those after the file name.
    mpiexec -n 4 python calc_pi_mpi.py -np 10_000_000
    python calc_pi.py -np 10_000_000 -n 1 -r 1
    Tip: To avoid waiting for a long time, reduce the number of repetitions and iterations of timeit (1 and 1 in this example)
  3. Think of these questions:
    • Is the MPI-based implementation faster than the basic one?
    • Is it faster than the numpy-based implementation?
    • When (for what programs or what settings) might it be faster/slower?
    • How different is this version to the original? How easy is it to adapt to using MPI?
@dpshelio dpshelio added the week10 Performance label Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
week10 Performance
Projects
None yet
Development

No branches or pull requests

1 participant