I am trying to call the BLAS ddot routine from SBCL.
Based on:
- the ddot documentation (http://www.netlib.org/lapack/explore-html/d5/df6/ddot_8f.html),
- its source code (http://www.netlib.org/lapack/explore-html/d5/df6/ddot_8f_source.html),
- some additional hint (https://orion.math.iastate.edu/docs/cmlib/blas/ddot),
- a working example of calling the dgemm routine (Matrix-multiplication using BLAS from Common Lisp)
I came up with the following script:
(load-shared-object "libblas.so.3")
(declaim (inline ddot))
(define-alien-routine ("ddot_" ddot) void
(n int :copy)
(dx (* double))
(incx int :copy)
(dy (* double))
(incy int :copy))
(defun pointer (array)
(sap-alien (sb-sys:vector-sap (array-storage-vector array)) (* double)))
(defun dot (dx dy)
(unless (= (length dx) (length dy))
(error "Vectors length does not match"))
(let ((n (length dx))
(result 0.0d0))
(sb-sys:with-pinned-objects (dx dy result)
(ddot n (pointer dx) 1 (pointer dy) 1))))
However, the following script:
(defvar *a* (make-array 4 :initial-element 1.0d0 :element-type 'double-float))
(defvar *b* (make-array 4 :initial-element 2.0d0 :element-type 'double-float))
(dot *a* *b*)
produces the following error:
arithmetic error FLOATING-POINT-INVALID-OPERATION signalled
[Condition of type FLOATING-POINT-INVALID-OPERATION]
Any hint?