Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call BLAS ddot routine from SBCL

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?

like image 578
Danny Zuko Avatar asked Jul 16 '15 10:07

Danny Zuko


1 Answers

Found it. Credits to Miroslav Urbanek from Charles University in Prague for the hint.

-(define-alien-routine ("ddot_" ddot) void
+(define-alien-routine ("ddot_" ddot) 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)
+  (let ((n (length dx)))
+    (sb-sys:with-pinned-objects (dx dy)

The ddot routine is meant to return a double, not a void. And the result variable is useless. Things are so obvious after you realize them :-)

like image 76
Danny Zuko Avatar answered Sep 30 '22 02:09

Danny Zuko