petalisp
2024-10-12
Elegant High Performance Computing
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.
0.1Getting Started
- Install Lisp and a suitable IDE. If unsure, pick Portacle.
- Download Petalisp via Quicklisp.
- Check out some of the examples.
0.2Showcases
Petalisp is still under development, so the following examples may stillchange slightly. Nevertheless they give a good glimpse on what programmingwith 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)))))
0.3Performance
Coming soon!
0.4Frequently Asked Questions
0.4.1Is 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 forworking 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.
0.4.2Do I have to program Lisp to use Petalisp?
Not necessarily. Not everyone has the time to learn Common Lisp. That iswhy 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.
0.4.3How can I get Emacs to indent Petalisp code nicely?
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))
0.4.4Why is Petalisp licensed under AGPL?
I am aware that this license prevents some people from using orcontributing to this piece of software, which is a shame. But unfortunatelythe majority of software developers have not yet understood that- In a digital world, free software is a necessary prerequisite for a freesociety.
- When developing software, open collaboration is way more efficient thancompetition.
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.
0.4.5Why is Petalisp written in Common Lisp?
I couldn't wish for a better tool for the job. Common Lisp is extremelyrich in features, standardized, fast, safe and mature. The Lisp communityis amazing and there are excellent libraries for almost every imaginabletask.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.