【问题标题】:Call BLAS ddot routine from SBCL从 SBCL 调用 BLAS ddot 例程
【发布时间】:2015-07-16 10:46:33
【问题描述】:

我正在尝试从 SBCL 调用 BLAS ddot 例程。

基于:

我想出了以下脚本:

(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))))

但是,下面的脚本:

(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*)

产生以下错误:

arithmetic error FLOATING-POINT-INVALID-OPERATION signalled
   [Condition of type FLOATING-POINT-INVALID-OPERATION]

有什么提示吗?

【问题讨论】:

    标签: common-lisp blas sbcl dot-product


    【解决方案1】:

    找到了。感谢布拉格查尔斯大学的 Miroslav Urbanek 提供的提示。

    -(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)
    

    ddot 例程旨在返回一个双精度值,而不是一个 void。结果变量是无用的。 你意识到它们之后,事情变得如此明显:-)

    【讨论】:

      【解决方案2】:

      我知道它不能直接回答您的问题,但您是否尝试过使用已编写的 Blas 绑定?例如Matlisp 已经为dot 提供了一个lispy 接口

      【讨论】:

        猜你喜欢
        • 2017-10-31
        • 1970-01-01
        • 2011-09-20
        • 1970-01-01
        • 2016-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多