Petalisp is an attempt to generate high performance code for parallel computers by JIT-compiling array definitions. It is not a full blown programming language, but rather a carefully crafted extension of Common Lisp that allows for extreme optimization and parallelization.
- Install Lisp and a suitable IDE. If unsure, pick Portacle.
- Download Petalisp via Quicklisp.
- Check out some of the examples.
Petalisp is still under development, so the following examples may still change slightly. Nevertheless they give a good glimpse on what programming with Petalisp will be like.
Example 1: transposing a matrix
(defun transpose (A) (reshape A (τ (m n) (n m))))
Example 2: matrix-matrix multiplication
(defun matrix-multiplication (A B) (β #'+ (α #'* (reshape A (τ (m n) (n m 1))) (reshape B (τ (n k) (n 1 k))))))
Example 3: the numerical Jacobi scheme in two dimensions
(defun jacobi-2d (grid iterations) (let ((interior (interior grid))) (if (zerop iterations) grid (jacobi-2d (fuse x (α #'* 0.25 (α #'+ (reshape x (τ (i0 i1) ((+ i0 1) i1)) interior) (reshape x (τ (i0 i1) ((- i0 1) i1)) interior) (reshape x (τ (i0 i1) (i0 (+ i1 1))) interior) (reshape x (τ (i0 i1) (i0 (- i1 1))) interior)))) (- iterations 1)))))
Frequently Asked Questions
Is Petalisp similar to NumPy?
NumPy is a widely used Python library for scientific computing on arrays. It provides powerful N-dimensional arrays and a variety of functions for working with these arrays.
Petalisp works on a more fundamental level. It provides even more powerful N-dimensional arrays, but just a few building blocks for working on them - element-wise function application, reduction, reshaping and array fusion.
So Petalisp is not a substitute for NumPy. However, it could be used to write a library that behaves like NumPy, but that is much faster and fully parallelized. In fact, writing such a library is one of my future goals.
Do I have to program Lisp to use Petalisp?
Not necessarily. Not everyone has the time to learn Common Lisp. That is why I am also working on some convenient Python bindings for Petalisp.
But: If you ever have time to learn Lisp, do it! It is an enlightening experience.
Why the Greek letters?
There are several reasons:
- It underlines the beautiful, mathematical nature of this programming model.
- The α and β functions are a tribute to Connection Machine Lisp.
- Compared to programming a computer with billions of transistors and terabytes of memory, typing Greek letters is easy. Right?
Note: Following community feedback, it is now also possible to write
b instead of
β. But as a convention, please use the
ASCII aliases only as a shortcut for scripting. If I ever see
being called in a Quicklisp project, I will remove them from the API :)
Why is Petalisp licensed under AGPL?
I am aware that this license prevents some people from using or contributing to this piece of software, which is a shame. But unfortunately the majority of software developers have not yet understood that
- In a digital world, free software is a necessary prerequisite for a free society.
- When developing software, open collaboration is way more efficient than competition.
So as long as distribution of non-free software is socially accepted, copyleft licenses like the AGPL seem to be the lesser evil.
That being said, I am willing to discuss relicensing on an individual basis.
Why is Petalisp written in Common Lisp?
I couldn’t wish for a better tool for the job. Common Lisp is extremely rich in features, standardized, fast, safe and mature. The Lisp community is amazing and there are excellent libraries for almost every imaginable task.
To illustrate why Lisp is particularly well suited for a project like Petalisp, consider the following implementation of a JIT-compiler for mapping a function over a vector of a certain element type:
(defun vector-mapper (element-type) (compile nil `(lambda (fn vec) (declare (function fn) (type (simple-array ,element-type (*)) vec) (optimize (speed 3) (safety 0))) (loop for index below (length vec) do (symbol-macrolet ((elt (aref vec index))) (setf elt (funcall fn elt)))))))
Not only is this JIT-compiler just 8 lines of code, it is also 20 times faster than invoking GCC or Clang on a roughly equivalent piece of C code.