Question

Ubuntu 13.04, Rust 0.6. I'm trying to use the Rust FFI to operate with openblas. The following code doesn't work as expected.

use core::io::println;
use core::libc::{c_int, c_float};
use core::vec::raw::to_ptr;

extern mod openblas {
    /*  Single precision dot product.
        sdot takes 5 args: number of elements,
                           pointer to a,
                           storage spacing for elements of a,
                           pointer to b,
                           storage spacing for elements of b
        returns a^T . b
    */
    fn cblas_sdot(N: c_int, x: *c_float, incx: c_int,
                  y: *c_float, incy: c_int) -> *c_float;
}

pub fn sdot(a: &[c_float], b: &[c_float]) -> *c_float {
    unsafe {
        openblas::cblas_sdot(3 as c_int, to_ptr(a), 1 as c_int,
                             to_ptr(b), 1 as c_int)
    }
}

fn main() {
    let a = [1. as c_float, 2. as c_float, 4. as c_float];
    let b = [1. as c_float, 2. as c_float, 5. as c_float];
    let x = sdot(a, b);
    println(fmt!("%?", x));

}

edit: I expected it to print 25, as a borrowed pointer. I got 140125321300160, which might be the address of the float.

Was it helpful?

Solution

The cblas_sdot function is not supposed to return a pointer.

From BLAS Reference:

cblas_sdot

Computes the dot product of two vectors (single-precision).

float cblas_sdot(const int N, const float *X, const int incX, const float *Y, const int incY);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top