Skip to main content

On the Efficiency of Python for High-Performance Computing: A Case Study Involving Stencil Updates for Partial Differential Equations

  • Conference paper
Modeling, Simulation and Optimization of Complex Processes

Abstract

The purpose of this paper is to assess the loss of computational efficiency that may occur when scientific codes are written in the Python programming language instead of Fortran or C. Our test problems concern the application of a seven-point finite stencil for a three-dimensional, variable coefficient, Laplace operator. This type of computation appears in lots of codes solving partial differential equations, and the variable coefficient is a key ingredient to capture the arithmetic complexity of stencils arising in advanced multi-physics problems in heterogeneous media.

Different implementations of the stencil operation are described: pure Python loops over Python arrays, Psyco-acceleration of pure Python loops, vectorized loops (via shifted slice expressions), inline C++ code (via Weave), and migration of stencil loops to Fortran 77 (via F2py) and C. The performance of these implementations are compared against codes written entirely in Fortran 77 and C. We observe that decent performance is obtained with vectorization or migration of loops to compiled code. Vectorized loops run between two and five times slower than the pure Fortran and C codes. Mixed-language implementations, Python-Fortran and Python-C, where only the loops are implemented in Fortran or C, run at the same speed as the pure Fortran and C codes.

At present, there are three alternative (and to some extent competing) implementations of Numerical Python: numpy, numarray, and Numeric. Our tests uncover significant performance differences between these three alternatives. Numeric is fastest on scalar operations with array indexing, while numpy is fastest on vectorized operations with array slices.

We also present parallel versions of the stencil operations, where the loops are migrated to C for efficiency, and where the message passing statements are written in Python, using the high-level pypar interface to MPI. For the current test problems, there are hardly any efficiency loss by doing the message passing in Python. Moreover, adopting the Python interface of MPI gives a more elegant parallel implementation, both due to a simpler syntax of MPI calls and due to the efficient array slicing functionality that comes with Numerical Python.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 129.00
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 169.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  1. X. Cai and H. P. Langtangen. Parallelizing PDE solvers using the Python programming language. In A.M. Bruaset and A. Tveito, editors, Numerical Solution of Partial Differential Equations on Parallel Computers, volume 51 of Lecture Notes in Computational Science and Engineering, pages 295–325. Springer-Verlag, 2006.

    Google Scholar 

  2. X. Cai, H. P. Langtangen, and H. Moe. On the performance of the Python programming language for serial and parallel scientific computations. Scientific Programming, 13(1):31–56, 2005.

    Google Scholar 

  3. F2PY software package. http://cens.ioc.ee/projects/f2py2e.

  4. H. P. Langtangen. Scripting utilities for [5], 2006. http://folk.uio.no/hpl/scripting.

  5. H. P. Langtangen. Python Scripting for Computational Science. Springer, 3rd edition, 2008.

    Google Scholar 

  6. Matlab code vectorization guide. http://www.mathworks.com/support/tech-notes/1100/1109.html, 2004.

  7. Netlib repository of numerical software. http://www.netlib.org.

  8. Psyco home page. http://psyco.sourceforge.net/, 2004.

  9. PyMPI software package. http://sourceforge.net/projects/pympi, 2004.

  10. PyPar software package. http://datamining.anu.edu.au/~ole/pypar, 2004.

  11. P. Ramachandran. Performance of various Python implementations for solving the 2D Laplace equation. http://www.scipy.org/PerformancePython.

  12. SciPy software package. http://www.scipy.org.

  13. Software for running the computational experiments in the present paper. http://folk.uio.no/xingca/python/efficiency2/.

  14. G. van Rossum and F. L. Drake. Extending and Embedding the Python Interpreter. http://docs.python.org/ext/ext.html.

  15. Weave. http://www.scipy.org/documentation/weave. Part of the SciPy package.

Download references

Author information

Authors and Affiliations

Authors

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2008 Springer-Verlag Berlin Heidelberg

About this paper

Cite this paper

Langtangen, H.P., Cai, X. (2008). On the Efficiency of Python for High-Performance Computing: A Case Study Involving Stencil Updates for Partial Differential Equations. In: Bock, H.G., Kostina, E., Phu, H.X., Rannacher, R. (eds) Modeling, Simulation and Optimization of Complex Processes. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-540-79409-7_23

Download citation

Publish with us

Policies and ethics