quil-lang/magicl

Revise LAPACK bindings

braised-babbage opened this issue · 0 comments

Current bindings produce defuns for which various LAPACK output values (e.g. mutated INFO variables) are lost to a Lisp caller. E.g.

(COMMON-LISP:DEFUN %DGETRF (M N A LDA IPIV INFO)
  (COMMON-LISP:DECLARE (COMMON-LISP:INLINE %%DGETRF)
                       (COMMON-LISP:TYPE (COMMON-LISP:SIGNED-BYTE 32) M)
                       (COMMON-LISP:TYPE (COMMON-LISP:SIGNED-BYTE 32) N)
                       (COMMON-LISP:TYPE
                        (COMMON-LISP:SIMPLE-ARRAY COMMON-LISP:DOUBLE-FLOAT) A)
                       (COMMON-LISP:TYPE (COMMON-LISP:SIGNED-BYTE 32) LDA)
                       (COMMON-LISP:TYPE
                        (COMMON-LISP:SIMPLE-ARRAY (COMMON-LISP:SIGNED-BYTE 32)
                         (COMMON-LISP:*))
                        IPIV)
                       (COMMON-LISP:TYPE (COMMON-LISP:SIGNED-BYTE 32) INFO))
  (CFFI:WITH-FOREIGN-OBJECTS ((M-REF6702 ':INT32) (N-REF6703 ':INT32)
                              (LDA-REF6705 ':INT32) (INFO-REF6707 ':INT32))
    (COMMON-LISP:SETF (CFFI:MEM-REF M-REF6702 :INT32) M)
    (COMMON-LISP:SETF (CFFI:MEM-REF N-REF6703 :INT32) N)
    (COMMON-LISP:SETF (CFFI:MEM-REF LDA-REF6705 :INT32) LDA)
    (COMMON-LISP:SETF (CFFI:MEM-REF INFO-REF6707 :INT32) INFO)
    (MAGICL.CFFI-TYPES:WITH-ARRAY-POINTERS ((A-REF6704 A) (IPIV-REF6706 IPIV))
      (%%DGETRF M-REF6702 N-REF6703 A-REF6704 LDA-REF6705 IPIV-REF6706
                INFO-REF6707))))

(from https://github.com/rigetti/magicl/blob/feature/magicl-ng/src/bindings/lapack02-cffi.lisp#L3725).
Mutations to INFO within the defun body will not be visible to callers.

It seems that any workaround is going to require rebuilding the lapack bindings.

  • One option is to change them to explicitly take boxed values by default (say, a length 1 vector), and then users need to manage boxing/unboxing around the call site. (barf)
  • Another option is to change the bindings so that the functions return the cffi:mem-ref values corresponding to the defun arguments. This is kind of heavyweight but shouldn't break existing code. Then callers can just check the last of the return values (corresponding to INFO).

There are probably other approaches.

Originally posted by @kilimanjaro in #63