Join us at Super Computing 2011!We invite you to visit us at the SC2011 conference in Seattle, Nov 14-17 at Booth #2040 See first-hand how we are enabling research discovery with Dell HPC solutions.
There was a recent article in Scientific Computing about scripting languages in HPC (http://www.scientificcomputing.com/articles-HPC-From-Scripting-to-Scaling-041210.aspx). The article talks about the rise in popularity of scripting languages for solving problems and why scripting is perhaps a good idea for researchers/scientists (I’ll just called them HPC-er’s). This article set off a great discussion within the HPC team at Dell especially myself and Glen Otero (http://www.delltechcenter.com/page/Science+and+Silicon%3A+Smarter+Conversations). So we conspired to tag-team a blog series on Python (www.python.org) in HPC. What tools are available for Python in HPC? How can you combine conventional compiled languages with Python for HPC applications? How can you write Python applications for HPC? And how can you combine Python and GPUs in HPC?We chose Python because we both have written code with it and are reasonably comfortable with the language. Perhaps more importantly, there are a huge number of tools, libraries, add-ons, etc. for Python, which make it attractive for HPC-er’s who are looking to put something together very quickly and simply. We aren’t saying that Python is the end-all, be-all of languages – we are just using it as an example of a scripting eco-system that has great potential for HPC applications.I’m going to start off the series with a quick overview of Python and highlight some of the tools/extensions/add-ons that are available. Glen is going to covering his favorite tools in Python and some aspects of parallel programming in Python and I will chime in as well. Then Glen and I will divide the hot world of GPU computing and he will cover CUDA and Python and I will cover OpenCL and Python. But overall we want to present how you can use Python, as the representative scripting tool base, in HPC.This series should be fun. If you are a Python coder you may learn something new about how to apply Python to HPC. If you aren’t a Python coder then maybe you will learn about what the possibilities are and can apply them to your scripting language or tool set or perhaps even switch over to the dark side with me and Luke… I mean Glen.Quick Introduction to Python:If you haven’t heard about Python, the one thing you have to remember is that it is named after Monty Python (http://en.wikipedia.org/wiki/Monty_Python%27s_Flying_Circus) and not the snake (that seems to be the #1 question for people new to the language). With that behind us, let’s take a brief look at Python.This introduction is not intended to be complete by any stretch of the imagination. There are many introductions and tutorials and books around Python. But to make sure we’re all on the same page, I’ll point some features (and quirks) of Python.Python is a very popular language that has a very clear and easy to use syntax. It has a very large library of functions and a VERY large number of add-ons, applications, etc. that can be interfaced with Python. It is an interpreted language, for the most part, with some attempts at a true compiler, but nothing that has developed popularity (yet).Python syntax is fairly clean but one of the quirks of the language is that instead of using curly braces, { }, to denote blocks of code, you use spaces. For example, for an if statement or a loop, you will use spaces (whitespace) to denote what code is in the code block and what isn’t. For example,g = 0.0;if (i > 0):a = (c*b)/a;d = a/1.14159;a = a*d;g = a;f = g; The last line in the code snippet, f = g, is not indented relative to the code above it. So it’s not considered part of the “if” block.Python has the range of typical operators one would expect in a language:
Python is also object-oriented so can create classes and objects and instantiate them, etc. Python has a range of data types as well:
With these data types you can create all kinds of data structures – pretty much anything you can imagine including multi-dimensional arrays, trees, heaps, hash data structures, linked-lists, and on and on.With Python there are a number of built-in “methods” that allow you to manipulate the data structures. If you aren’t into object-oriented programming, these are basically functions that operate on the data. For example, we can add data to a list very easily.a = []; # define “a” to be an arraya.append(1.0);a.append(2.0);a.append(3.0);The “method” is “append” which is basically adding the data to the end of the list (or array). Also notice that the pound symbol (#) is a comment.Python also comes with a shell for entering commands (think of it as “bash” or “csh” but it’s devoted solely to Python). The classic shell is called idle. But if you are in Linux, you can type the command Python and it will start up. The command prompt is typically “>>>” or three arrows. At the prompt you can type in Python commands and idle will execute them (pretty easy to do). The alternative is to create a “script” that can be executed. You create the script in an editor (vi or emacs for example) and save the script to a file (I typically name the file “something.py” where “something” is the name of the file and I end it with a “.py” so that I know the file is a Python script (you don’t have to do this but I recommend some way of tracking the scripts). Then at the top of the script you put something like the following: #!/usr/bin/pythonThis assumes that your python executable (really the idle shell) has the path, /usr/bin/python. Then you make the script executable and execute it,# chmod 770 something.py# ./something.py(Before anyone brings in security concerns about changing the mode of the file, I understand. If you have some notes on how to do this securely, please post it. Until then I’m trying to make things easy).That’s it! Pretty easy to create Python scripts and run them. Python Add-ons/Extensions:For HPC-er’s doing research or science with HPC, you might be interested in the wide range of add-ons for Python. I’m not going to list all of them (that’s way too long), but I will focus on a few of the more important ones.NumPy: (http://numpy.scipy.org/)NumPy are a set of extensions for Python that allow the creation and manipulation of multi-dimensional arrays or matrices. It includes a large number of functions that can operate on these objects which are arrays. Here’s a small snippet from the Numpy tutorial for a 2-D array.>>> b = array( [ (1.5,2,3), (4,5,6) ] )>>> barray(1.5, 2. , 3. ],[ 4. , 5. , 6.)Note that “>>>” is the prompt for the Python interpreter. In addition to being able to create array object there is a whole set of array functions available (far too numerous to list here).SciPy (http://www.scipy.org)SciPy is an Open Source library of scientific tools for Python, primarily using Numpy. It is probably THE key source of scientific tools for Python and has produced a wealth of tools and papers for Python. In addition, they sponsor a conference around scientific Python, appropriately called Scipy.The Scipy website also has a great set of links to other tools for Python: http://www.scipy.org/Topical_Software. Matplotlib: (http://matplotlib.sourceforge.net/)In addition to just performing computation, there is also a definite need for plotting results. Python has a very large number of plotting add-ons (here’s a partial list: http://www.scipy.org/Topical_Software#head-b98ffdb309ccce4e4504a25ea75b5c806e4897b6). One of the most popular packages is called matplotlib that produces very good 2D plots. From the website, “Matplotlib can be used in python scripts, the python and ipython shell (ala’ matlab or mathematica), web application servers, and six graphical user interface toolkits.” Here’s a sample image from the website of a very simple plot,