• Non ci sono risultati.

multiprocessing and mpi4py

N/A
N/A
Protected

Academic year: 2021

Condividi "multiprocessing and mpi4py"

Copied!
16
0
0

Testo completo

(1)

multiprocessing and mpi4py

02 - 03 May 2012 ARPA PIEMONTE

(2)

Bibliography

multiprocessing

http://docs.python.org/library/multiprocessing.html

http://www.doughellmann.com/PyMOTW/multiprocessi ng/

mpi4py

http://mpi4py.scipy.org/docs/usrman/index.html

(3)

Global Interpreter Lock, GIL, allows only a single thread to run in the interpreter

this clearly kills the performance

There are different ways to overcome this limit and enhance the overall performance of the program

multiprocessing (python 2.6)

mpi4py (SciPy)

Introduction (1)

(4)

multiprocessing (1)

Basically it works by forking new processes and dividing the work among them exploiting (all) the cores of the system

import multiprocessing as mp def worker(num):

    """thread worker function"""

    print 'Worker:', num     return

if __name__ == '__main__':

    jobs = []

    for i in range(5):

        p = mp.Process(target=worker, args=(i,))         jobs.append(p)

        p.start()

(5)

multiprocessing (2)

processes can communicate to each other via queue, pipes

Poison pills to stop the process

Let's see an example...

(6)

multiprocessing (3)

import multiprocessing as mp import os

def worker(num, input_queue):

  """thread worker function"""

  print 'Worker:', num

  for inp in iter(input_queue.get, 'stop'):

    print 'executing %s' % inp

    os.popen('./mmul.x < '+inp+' >'+inp+'.out' )   return

if __name__ == '__main__':

  input_queue = mp.Queue() # queue to allow communication   for i in range(4):

    input_queue.put('in'+str(i))   # the queue contains the            name of the inputs  

  for i in range(4):

    input_queue.put('stop')  # add a poison pill for each              process

  for i in range(4):

    p = mp.Process(target=worker, args=(i, input_queue))     p.start()

(7)

Introduction (1)

mpi4py allows Python to sneak (its way) into HPC field

For example:

the infrastructure of the program (MPI, error handling, ...) can be written in Python

Resource-intensive kernels can still be

programmed in compiled languages (C, Fortran)

new-generation massive parallel HPC systems (like

bluegene) already have the Python interpreter on

their thousands of compute nodes

(8)

Introduction (2)

Some codes already have this infrastructure: i.e.

GPAW

(9)

Introduction (2)

(10)

What is MPI

the Message Passing Interface, MPI, is a standardized and portable message-passing system designed to

function on a wide variety of parallel computers.

Since its release, the MPI specification has become the leading standard for message-passing libraries for parallel computers.

mpi4py wraps the native MPI library

(11)

Performance

Message passing with mpi4py generally is close to C performance from medium to long size arrays

the overhead is about 5 % (near C-speed)

This performance may entitle you to skip C/Fortran code in favor of Python

To reach this communication performance you need

use special syntax

(12)

Performance (2)

t0 = MPI.Wtime()

data = numpy.empty(10**7,  dtype=float)

if rank == 0:

    data.fill(7) # with 7 else:

    pass 

MPI.COMM_WORLD.Bcast([data,  MPI.DOUBLE], root=0)

t1 = MPI.Wtime() ­ t0

t0 = MPI.Wtime()

if rank == 0: 

    data = numpy.empty(10**7,         dtype=float)

    data.fill(7) else:

    data = None  

data=MPI.COMM_WORLD.bcast(data , root=0)

t1 = MPI.Wtime() ­ t0

(13)

Performance (3)

numpy array comm tempo 0.068648815155 numpy array comm tempo 0.0634961128235 numpy array cPickle tempo 0.197563886642 numpy array cPickle tempo 0.18874502182

example on the left is about 3 times faster than that one on the right

The faster example exploits direct array data

communication of buffer-provider objects (e.g., NumPy arrays)

The slower example employs a pickle-based

(14)

Error handling

Error handling is supported. Errors originated in native MPI calls will throw an instance of the exception class Exception, which derives from standard exception

RuntimeError

(15)

mpi4py API

mpi4py API libraries can be found

the web site

http://mpi4py.scipy.org/docs/apiref/index.html

(16)

To summarize

mpi4py and multiprocessing can be a viable option to make your serial code parallel

Message passing performance are close to compiled languages (like C, Fortran)

Massive parallel systems, like bluegene, already have Python on their compute nodes

Riferimenti

Documenti correlati

Environments allow to have multiple, distinct, independent installations of Python, each one with its selection of installed packages:.. &gt;conda create -n

Environments allow to have multiple, distinct, independent installations of Python, each one with its selection of installed packages:.. &gt;conda create -n py2

The open function returns an object to operate on a file, depending on the mode with which it is opened. A mode is a combination of an open mode (r/w/x/a) and a I/O mode (t/b), and

● Strong Scaling: increase the number of processors p keeping the total problem size fixed. – The total amount of work

The module, which is freely available for download, allows describing the structure of a model using a syntax similar to that of popular modeling systems like GAMS, AIMMS or

Though it is not known whether “true” Lebesgue points for the triangle are symmetric (and computational results seem to say they are not, cf. [11, 22]), symmetry is a

The empirical results of the regression model provide two significant findings. The first finding is about how the bilateral trades are influenced , as pictured in Table 8