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.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Preview
Unable to display preview. Download preview PDF.
References
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.
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.
F2PY software package. http://cens.ioc.ee/projects/f2py2e.
H. P. Langtangen. Scripting utilities for [5], 2006. http://folk.uio.no/hpl/scripting.
H. P. Langtangen. Python Scripting for Computational Science. Springer, 3rd edition, 2008.
Matlab code vectorization guide. http://www.mathworks.com/support/tech-notes/1100/1109.html, 2004.
Netlib repository of numerical software. http://www.netlib.org.
Psyco home page. http://psyco.sourceforge.net/, 2004.
PyMPI software package. http://sourceforge.net/projects/pympi, 2004.
PyPar software package. http://datamining.anu.edu.au/~ole/pypar, 2004.
P. Ramachandran. Performance of various Python implementations for solving the 2D Laplace equation. http://www.scipy.org/PerformancePython.
SciPy software package. http://www.scipy.org.
Software for running the computational experiments in the present paper. http://folk.uio.no/xingca/python/efficiency2/.
G. van Rossum and F. L. Drake. Extending and Embedding the Python Interpreter. http://docs.python.org/ext/ext.html.
Weave. http://www.scipy.org/documentation/weave. Part of the SciPy package.
Author information
Authors and Affiliations
Editor information
Editors and Affiliations
Rights 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
DOI: https://doi.org/10.1007/978-3-540-79409-7_23
Publisher Name: Springer, Berlin, Heidelberg
Print ISBN: 978-3-540-79408-0
Online ISBN: 978-3-540-79409-7
eBook Packages: Mathematics and StatisticsMathematics and Statistics (R0)