Home

Awesome

MAGICL

Matrix Algebra proGrams In Common Lisp by Rigetti Computing. (née FLAIL: Finally, Linear Algebra In Lisp!)

(Note: The high-level interface is experimental and subject to change.)

Requirements

MAGICL has two main systems:

The system MAGICL/CORE requires:

The system MAGICL, on the other hand, requires several foreign dependencies not shipped with MAGICL, like:

Detailed instructions on how to install libffi and BLAS/LAPACK can be found here.

Currently this library is SBCL-, CCL- and ECL-only. The non-portable code is in with-array-pointers.lisp and magicl.lisp.

Installation

First ensure you have the necessary requirements installed, as described in the previous section.

To install MAGICL, clone this repository into your Quicklisp's local-projects folder. You can quickly check where this is by running sbcl and evaluating ql:*local-project-directories*. Once installed, confirm that MAGICL is working properly by running the tests, as described in the next section.

Lisp-Only vs Accelerated MAGICL

Extensions

MAGICL/CORE only uses pure ANSI Common Lisp code. If you wish to accelerate it or extend the functionality, you may load MAGICL extensions. These extensions typically install new backends to MAGICL functions. The available extensions are:

For backwards compatibility, MAGICL loads every extension under the kitchen sink. This may change in future versions of MAGICL! If you depend on an extension, depend on it explicitly!

If you use extensions, you'll need the requisite C/Fortran libraries. Expokit will automatically build for you, as its source is included in the distribution of MAGICL.

Backends

Accelerated functionality is installed with a notion called "backends". A backend is a name of a group of functionality, typically denoted by a symbol or keyword. The :lisp backend is the default one, and several backends can be active all at once. Each extension above adds a new backend. The current backends are:

In most cases, one does not need to concern themselves with backends; MAGICL functionality should "just work" and dispatch to the appropriate backend. However, the programmer always has control, even dynamically in the program, of which backends should be used at a given time with the magicl.backends:with-backends macro. For instance,

(magicl.backends:with-backends (:blas :lisp)
  ;; ... code ...
  )

says that the code should be executed, always preferring :blas-accelerated functions, and using :lisp-implemented functions as a fall-back.

(magicl.backends:with-backends (:lisp)
  ;; ... code ...
  )

says to only use :lisp-implemented functions, even if other backends are loaded.

The active backends can be found with the function magicl.backends:active-backends, which lists the backends to use in priority order.

One can be even finer-grained than with-backends. Given a function f which has many backend implementations, one can get a specific implementation by using the function:

(magicl.backends:backend-implementation 'f :backend-name)

For instance

(magicl.backends:backend-implementation 'magicl:csd :lapack)

will give the implementation of the cosine-sine decomposition function in LAPACK. This can be called in exactly the same way magicl:csd can be called.

In backend-implementation, if both the function name and the backend name are (quoted) constants, this will be looked up at compile-time, which is useful for writing efficient code that does not dispatch. But note that by doing this, with-backends will not be respected.

Testing MAGICL

You can run the MAGICL tests from your Lisp REPL with:

(asdf:test-system :magicl)

You currently need all of the extensions working for the tests to run.

High-level Interface

See the high-level doc for an extensive discussion and comparison of MAGICL functions with those of MATLAB and NumPy.

Developer's Guide: How to Add New Functions

See the developer how-to to understand how to add new functionality to MAGICL.

Fortran Bindings

See the Fortran Functions on how to re-generate the Fortran bindings from the original BLAS, LAPACK, and Expokit reference code.

See the same document for how to query for available Fortran functions in the currently loaded dynamic libraries.

History and Credits

MAGICL development started at Rigetti Computing by Robert Smith and Joe Lin in 2017.

CL-BLAPACK is a library developed by Ryan Rifkin and Evan Monroig. Rigetti Computing created a fork of this library and renamed it MAGICL, and made significant changes that departed from the original design, including:

The most important common design decision between CL-BLAPACK and MAGICL is allowing direct access to the Fortran library functions by way of automatically generated Lisp bindings from the reference sources.