[plt-scheme] FFI Problem

From: Jon Rafkind (workmin at ccs.neu.edu)
Date: Sat May 13 09:30:37 EDT 2006

(_ptr o <type>) will generate an object for you on the fly during the 
call to the C library so you dont need to supply a variable to the 
function call. You do need to give an output lambda to _fun which will 
return all the output variables:

(define foo (get-ffi-obj "foo" foo-lib
                       (_fun (x : (_ptr o _byte))
                                 (y : (_ptr o _byte))
                                 _int
                                 -> _void
                                 -> (values x y))))

(define-values (x y) (foo 2))

You could also ignore any output variables, as is often the case with 
X11 functions. In your case you are saying you only need to supply the 
f64 vectors and the void* y parameter.

Jens Axel Søgaard wrote:

> I have a "can't see the tree for the forest"-problem.
>
> The function dgemv ought to accept 11 arguments, but will
> only accept 3.
>
> > (require blas)
> . procedure ffi-wrapper:dgemv: expects 3 arguments,
> given 12: 0 0 4 4 1.0 #<struct:f64vector> 4 #<struct:f64vector>
> 1 0.0 #<struct:f64vector> 1
>
> Where is my mistake?
>
>
>
> (module blas mzscheme
>  (provide (all-defined))
>
>  (require (lib "foreign.ss"))
>  (unsafe!)
>
>  (define blas-lib
>    (ffi-lib "c:/libgoto_katmai-r0.99-3.dll"))
>
>  ; void dgemv(const char *trans, int *m, int *n, double *alpha,
>  ;                    void *a, int *lda, void *x, int *incx,
>  ;                  double *beta, void *y, int *incy);
>
>  (define blas:dgemv
>    (get-ffi-obj "dgemv" blas-lib
>                 (_fun (_ptr o  _byte)   ; char *trans
>                       (_ptr o  _int)    ; int *m
>                       (_ptr o  _int)    ; int *n
>                       (_ptr o  _double) ; double *alpha
>                       _f64vector        ; void *a         the input 
> matrix
>                       (_ptr o  _int)    ; int *lda
>                       _f64vector         ; void *x         input 
> vector of doubles
>                       (_ptr o  _int)    ; int *incx
>                       (_ptr o  _double) ; double *beta
>                       (_ptr io _void)   ; void *y
>                       (_ptr o  _int)    ; int *incy
>                       ->
>                       _void)
>                 (lambda ()
>                   (lambda (x)
>                     (error 'foolib
>                            "your installed foolib version does not 
> provide \"foo\"")))))
>
>  ; void dgemv(const char *trans, int *m, int *n, double *alpha,
>  ;                    void *a, int *lda, void *x, int *incx,
>  ;                  double *beta, void *y, int *incy);
>  ;
>  ; The parameters are as follows:
>  ;
>  ; trans
>  ;   is a single character indicating the form of the input matrix a, 
> where:
>  ;      * 'N' or 'n' indicates that a is to be used in the computation
>  ;      * 'T' or 't' indicates that the transpose of a is to be used 
> in the computation
>  ;
>  ; m
>  ;    represents:
>  ;
>  ;        * the number of rows in input matrix a
>  ;        * the length of vector y, if 'N' or 'n' is used for the 
> trans parameter
>  ;        * the length of vector x, if 'T' or 't' is used for the 
> trans parameter
>  ;
>  ;    The number of rows must be greater than or equal to zero, and 
> less than the leading
>  ;    dimension of the matrix a (specified in lda)
>  ; n
>  ;    represents:
>  ;
>  ;        * the number of columns in input matrix a
>  ;        * the length of vector x, if 'N' or 'n' is used for the 
> trans parameter
>  ;        * the length of vector y, if 'T' or 't' is used for the 
> trans parameter
>  ;
>  ;    The number of columns must be greater than or equal to zero.
>  ; alpha
>  ;    is the scaling constant for matrix a
>  ; a
>  ;    is the input matrix of float (for sgemv) or double (for dgemv) 
> values
>  ; lda
>  ;    is the leading dimension of the array specified by a. The 
> leading dimension
>  ;    must be greater than zero. The leading dimension must be greater 
> than or
>  ;    equal to 1 and greater than or equal to the value specified in m.
>  ; x
>  ;    is the input vector of float (for sgemv) or double (for dgemv) 
> values.
>  ; incx
>  ;    is the stride for vector x. It can have any value.
>  ; beta
>  ;    is the scaling constant for vector y
>  ; y
>  ;    is the output vector of float (for sgemv) or double (for dgemv) 
> values.
>  ; incy
>  ;    is the stride for vector y. It must not be zero.
>  ;
>  ; Note:
>  ;  Vector y must have no common elements with matrix a or vector x; 
> otherwise, the results are unpredictable.
>
>  ; (error)
>
>  ;(define order blas:ColMajor)
>  ;(define transa blas:NoTrans)
>
>  (define order 0)
>  (define transa 0)
>
>  (define m 4); /* Size of Column ( the number of rows ) */
>  (define n 4); /* Size of Row ( the number of columns ) */
>  (define lda 4); /* Leading dimension of 5 * 4 matrix is 5 */
>  (define incx 1)
>  (define incy 1)
>  (define alpha 1.0)
>  (define beta 0.0)
>
>  (define a                ; column-major order!
>    (list->f64vector
>     '(1.0 2.0 3.0 4.0
>           1.0 1.0 1.0 1.0
>           3.0 4.0 5.0 6.0
>           5.0 6.0 7.0 8.0) ))
>
>  (define x (list->f64vector '(1.0 2.0 1.0 1.0)))
>  (define y (list->f64vector '(0.0 0.0 0.0 0.0)))
>
>  (blas:dgemv  order transa m n alpha a lda x incx beta y incy)
>  )
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>


Posted on the users mailing list.