Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAGICL extensions refactor #125

Merged
merged 46 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
a13da59
first step in MAGICL refactor
stylewarning Jan 30, 2021
6a6733a
isolate MAGICL-TRANSCENDENTAL and expokit, add EXPM/LOGM to MAGICL
stylewarning Jan 30, 2021
3838b02
completely contain expokit as an extension
stylewarning Jan 30, 2021
fa9331b
organize file structure, delete stale files
stylewarning Jan 30, 2021
16525db
separate out lapack and blas
stylewarning Jan 30, 2021
ca0dad7
fix typo
stylewarning Jan 30, 2021
d196a7f
add MAGICL/FANCY system
stylewarning Jan 30, 2021
8eb19aa
include magicl-csd.lisp in system
stylewarning Jan 30, 2021
621dfec
get examples working
stylewarning Jan 30, 2021
0b768b0
get tests to work
stylewarning Jan 30, 2021
c28ff06
mention extensions in README
stylewarning Jan 30, 2021
acd15fb
add backend function and implementation framework
stylewarning Mar 19, 2021
68ee356
re-export add'l backend symbols
stylewarning Mar 20, 2021
90ecb4e
push new default backends to the front
stylewarning Mar 20, 2021
5431fc5
document how to use backend functions
stylewarning Mar 20, 2021
891b4d7
define :BLAS and :LAPACK backend separately
stylewarning Mar 21, 2021
47c704f
remove MAGICL/FANCY and make MAGICL/CORE
stylewarning Mar 21, 2021
347baa9
add DEFINE-EXTENSIBLE-FUNCTION
stylewarning Mar 21, 2021
21bb55c
fix minor formatting and typos
stylewarning Mar 21, 2021
5265ce0
export more backend framework symbols
stylewarning Mar 21, 2021
1d49beb
warn when defining backend functions outside of MAGICL
stylewarning Mar 21, 2021
033b8ee
put LAPACK into backend framework
stylewarning Mar 21, 2021
09b39d8
better docs
stylewarning Mar 21, 2021
a1fbd99
fix typos
stylewarning Mar 21, 2021
c14c140
check symbol's package more directly
stylewarning Mar 22, 2021
2c44830
only generate backend-function if it doesn't exist
stylewarning Mar 22, 2021
163c132
fix bug in non-quoted uninterned symbol
stylewarning Mar 22, 2021
a1eb8e3
clean up expokit packages
stylewarning Mar 22, 2021
688310f
fix docs typos
stylewarning Mar 22, 2021
a42fde6
use DEFINE-EXTENSIBLE-FUNCTION for EXPM
stylewarning Mar 22, 2021
0434460
delete useless empty :components
stylewarning Mar 22, 2021
2f7794a
delete nickname stuff
stylewarning Mar 22, 2021
d9e2722
add dummy magicl-transcendental system for backward compat
stylewarning Mar 22, 2021
7cce767
make compatible MAGICL-TRANSCENDENTAL package
stylewarning Mar 22, 2021
68a4a5b
remove ASDF deprecated function
stylewarning Mar 22, 2021
b1957ea
simplify magicl-gen bindings location
stylewarning Mar 22, 2021
1d3f2e6
move pkg MAGICL-TRANSCENDENTAL to separate file
stylewarning Mar 22, 2021
2bc945d
fix typos
stylewarning Mar 22, 2021
9bd2fb4
move fortran frobbing to separate doc
stylewarning Mar 22, 2021
c542d97
fix typo
stylewarning Mar 22, 2021
8658be0
re-export PRINT-AVAILABILITY-REPORT
stylewarning Mar 22, 2021
06bcc40
make sure libblas and liblapack are tracked properly
stylewarning Mar 22, 2021
f19abcd
fix symbol tracking bug in libraries
stylewarning Mar 22, 2021
c17bdb9
fix more magicl-gen bugs
stylewarning Mar 23, 2021
205e019
regenerate BLAS/LAPACK bindings from 3.9.0 sources
stylewarning Mar 23, 2021
790a13c
document DEFINE-COMPATIBLE-... macro
stylewarning Mar 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
transcendental/libexpokit.dylib
transcendental/libexpokit.so
**/*.dylib
**/*.so
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

_Matrix Algebra proGrams In Common Lisp_ by [Rigetti Computing](http://www.rigetti.com). (née FLAIL: _Finally, Linear Algebra In Lisp!_)

(**Note**: The high-level interface is experimental and subject to rapid change.)
(**Note**: The high-level interface is experimental and subject to change.)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐌


## Requirements

* SBCL (> 1.3.19) or CCL (>= 1.11) on AMD64
* quicklisp

If you'll want to use BLAS/LAPACK for extended functionality, you'll need:

* libffi
* BLAS and LAPACK

Expand All @@ -29,18 +32,34 @@ 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

MAGICL by default only uses pure ANSI Common Lisp code. If you with to
accelerate it or extend the functionality, you may load *MAGICL
extensions*. The available ones are:

- `MAGICL/FANCY`: for every extension we've got under the kitchen sink
- `MAGICL/EXT-BLAS`: for BLAS functions
- `MAGICL/EXT-LAPACK`: for LAPACK functions
- `MAGICL/EXT-EXPOKIT`: for expokit (matrix `exp()`) functions

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

## Testing MAGICL

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

```
(ql:quickload :magicl-tests)
(asdf:test-system :magicl)
```

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth it to throw in a few easy tests that exercise magicl/core explicitly? More broadly, we should probably make an issue to test various backends explicitly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought in a separate test we could write test functions which iterate over the backends using with-backends or binding *backends* directly. I don't think I want to do it in this PR though. Made issue #128


## High-level Interface

See [high-level doc](doc/high-level.md).
See [high-level doc](doc/high-level.md) for an extensive discussion and comparison of MAGICL functions with those of MATLAB and NumPy.

## Showing Available Functions

Expand Down
10 changes: 3 additions & 7 deletions examples.lisp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
(defpackage #:magicl-examples
(:use #:common-lisp #:magicl-transcendental)
#+package-local-nicknames
(:local-nicknames (:blas :magicl.blas-cffi)
(:lapack :magicl.lapack-cffi)
(:expokit :magicl.expokit-cffi))
(:use #:common-lisp)
(:export #:dot-example
#:eigenvalue-example
#:qr-example
Expand Down Expand Up @@ -137,8 +133,8 @@
(format t "X*X^-1~%~a~%" id)))

(defun expm-example ()
(let* ((x (magicl:from-list (list 0 0 0 0 0 0 1.5 0 0 -1.5 0 0.5 0 0 -0.5 0) '(4 4) :type 'double-float))
(expx (magicl-transcendental:expm x))
(let* ((x (magicl:from-list (list 0 0 0 0 0 0 1.5 0 0 -1.5 0 0.5 0 0 -0.5 0) '(4 4) :type '(complex double-float)))
(expx (magicl:expm x))
(d (magicl:det expx)))
(format t "X~%~a~%" x)
(format t "e^X~%~a~%" expx)
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion magicl-examples.asd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
:description "MAGICL examples"
:maintainer "Rigetti Computing"
:author "Rigetti Computing"
:depends-on (#:magicl #:magicl-transcendental)
:depends-on (#:magicl/fancy)
:serial t
:components
((:file "examples")))
3 changes: 1 addition & 2 deletions magicl-tests.asd
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
:author "Rigetti Computing"
:depends-on (#:alexandria
#:uiop
#:magicl
#:magicl-transcendental
#:magicl/fancy
#:magicl-examples
#:fiasco)
:perform (asdf:test-op (o s)
Expand Down
61 changes: 0 additions & 61 deletions magicl-transcendental.asd

This file was deleted.

154 changes: 134 additions & 20 deletions magicl.asd
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,19 @@
:author "Rigetti Computing"
:version (:read-file-form "VERSION.txt")
:depends-on (#:alexandria
#:cffi
#:cffi-libffi
#:abstract-classes
#:policy-cond)
#:policy-cond

#:magicl/ext ; Allow extensions
)
:in-order-to ((asdf:test-op (asdf:test-op #:magicl-tests)))
:around-compile (lambda (compile)
(let (#+sbcl (sb-ext:*derive-function-types* t))
(funcall compile)))
:serial t
:pathname "src/"
:serial t
:components
((:file "packages")
(:file "load-libs")
(:file "with-array-pointers")
(:file "cffi-types")
(:module "bindings"
:components ((:file "lapack00-cffi")
(:file "lapack01-cffi")
(:file "lapack02-cffi")
(:file "lapack03-cffi")
(:file "lapack04-cffi")
(:file "lapack05-cffi")
(:file "lapack06-cffi")
(:file "lapack07-cffi")
(:file "blas-cffi")))
(:module "high-level"
:serial t
:components ((:file "util")
Expand All @@ -43,15 +31,141 @@
(:file "tensor")
(:file "matrix")
(:file "vector")
(:file "lapack-generics")
(:file "types/single-float")
(:file "types/double-float")
(:file "types/complex-single-float")
(:file "types/complex-double-float")
(:file "types/int32")
(:file "lapack-templates")
(:file "lapack-bindings")
(:file "constructors")
(:file "specialize-constructor")
(:file "polynomial-solver")))
(:file "magicl")))

;;; Extension common code

(asdf:defsystem #:magicl/ext
:description "Common code for extending MAGICL with foreign libraries."
:depends-on (#:cffi
#:cffi-libffi)
:serial t
:pathname "src/extensions/common"
:components
((:file "package")
(:file "library-tracking")
(:file "with-array-pointers")
(:file "cffi-types")))

;;; BLAS

(asdf:defsystem #:magicl/ext-blas
:description "Native BLAS routines in MAGICL."
:depends-on (#:magicl/ext
#:cffi)
:serial t
:pathname "src/"
:components
((:file "extensions/blas/package")
(:file "extensions/blas/load-libs")
(:module "bindings"
:components ((:file "blas-cffi")))))


;;; LAPACK

(asdf:defsystem #:magicl/ext-lapack
:description "Native LAPACK routines in MAGICL."
:depends-on (#:magicl
#:magicl/ext
#:magicl/ext-blas
#:cffi
#:policy-cond)
:serial t
:pathname "src/"
:components
((:file "extensions/lapack/package")
(:file "extensions/lapack/load-libs")
(:module "bindings"
:components ((:file "lapack00-cffi")
(:file "lapack01-cffi")
(:file "lapack02-cffi")
(:file "lapack03-cffi")
(:file "lapack04-cffi")
(:file "lapack05-cffi")
(:file "lapack06-cffi")
(:file "lapack07-cffi")))
(:module "extensions/lapack"
:components ((:file "lapack-generics")
(:file "lapack-templates")
(:file "lapack-bindings")
(:file "lapack-csd")))))


;;; EXPOKIT

;;; Adapted from commonqt's qt.asd
(defclass f->so (asdf:source-file)
()
(:default-initargs
:type "f"))

(defmethod output-files ((operation compile-op) (component f->so))
(values (list (make-pathname :name "libexpokit"
:type #-darwin "so" #+darwin "dylib"
:defaults (component-pathname component)))
t))

(defmethod perform ((operation load-op) (component f->so))
t)

(defmethod perform ((operation compile-op) (component f->so))
(flet ((nn (x) (uiop:native-namestring x)))
(let* ((fortran-file (component-pathname component))
(object-file (make-pathname :type "o" :defaults fortran-file))
(shared-object (make-pathname :type #+darwin "dylib" #-darwin "so"
:name "libexpokit"
:defaults fortran-file)))
(uiop:run-program
(list "gfortran" "-fPIC" "-std=legacy"
"-c"
(nn fortran-file)
"-o"
(nn object-file)))
(uiop:run-program
(list "gfortran" #+darwin "-dynamiclib" #-darwin "-shared"
"-o" (nn shared-object)
(nn object-file)
#+darwin "-lblas"
#+darwin "-llapack"))
(delete-file object-file))))


(asdf:defsystem #:magicl/ext-expokit
:description "Expokit for MAGICL."
:depends-on (#:alexandria
#:cffi
#:cffi-libffi
#:magicl
#:magicl/ext
#:magicl/ext-blas
#:magicl/ext-lapack)
:serial t
:components
((:module "expokit"
:components ((f->so "expokit")))
(:module "src/extensions/expokit"
:components ((:file "package")
(:file "load-libs")))
(:file "src/bindings/expokit-cffi")
(:file "src/extensions/expokit/expm")))


;;; Fancy - Load everything

(asdf:defsystem #:magicl/fancy
:description "MAGICL with all extensions."
:depends-on (#:magicl
#:magicl/ext-blas
#:magicl/ext-lapack
#:magicl/ext-expokit)
:serial t
:components ())
19 changes: 19 additions & 0 deletions src/extensions/blas/load-libs.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(in-package #:magicl.foreign-libraries)

(cffi:define-foreign-library libblas
#+:magicl.use-accelerate
(:darwin "libBLAS.dylib" :search-path #P"/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Versions/A/")
#-:magicl.use-accelerate
(:darwin (:or "/usr/local/opt/lapack/lib/libblas.dylib" "libblas.dylib" ))
#+:magicl.use-mkl
(:unix "libmkl_rt.so")
#-:magicl.use-mkl
(:unix (:or "libblas.so"
"libblas.so.3"))
(t (:default "libblas")))

(defvar *blas-loaded* nil)

(unless *blas-loaded*
(cffi:load-foreign-library 'libblas)
(setf *blas-loaded* t))
6 changes: 6 additions & 0 deletions src/extensions/blas/package.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

(defpackage #:magicl.blas-cffi
(:use)
#-package-local-nicknames
(:nicknames #:blas))

File renamed without changes.
Loading