Petalisp generates high performance code for parallel computers by JIT-compiling array definitions. It augments the existing general purpose programming language Common Lisp (and soon also Python) with parallelism and lazy evaluation.
- 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 lazy-transpose (A)
(lazy-reshape A (transform m n to n m)))
Example 2: matrix-matrix multiplication
(defun matrix-multiplication (A B)
(lazy-reduce #'+
(lazy #'*
(lazy-reshape A (transform m n to n m 1))
(lazy-reshape B (transform n k to n 1 k)))))
Example 3: the numerical Jacobi scheme in two dimensions
(defun lazy-jacobi-2d (grid iterations)
(let ((interior (interior grid)))
(if (zerop iterations) grid
(lazy-jacobi-2d
(lazy-fuse
x
(lazy #'* 0.25
(lazy #'+
(lazy-reshape x (transform i0 i1 to (+ i0 1) i1) interior)
(lazy-reshape x (transform i0 i1 to (- i0 1) i1) interior)
(lazy-reshape x (transform i0 i1 to i0 (+ i1 1)) interior)
(lazy-reshape x (transform i0 i1 to i0 (- i1 1)) interior))))
(- iterations 1)))))
Coming soon!
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.
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.
Put the following code in your initialization file:
(put 'lazy 'common-lisp-indent-function '(1 &rest 1))
(put 'lazy-reduce 'common-lisp-indent-function '(1 &rest 1))
(put 'lazy-multiple-value 'common-lisp-indent-function '(1 1 &rest 1))
(put 'lazy-reshape 'common-lisp-indent-function '(1 &rest 1))
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.
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.