gpt4 book ai didi

pointers - 从 Rust FFI 中检索一个 float 指针

转载 作者:行者123 更新时间:2023-11-29 08:13:07 24 4
gpt4 key购买 nike

Ubuntu 13.04,Rust 0.6。我正在尝试使用 Rust FFI 来操作 openblas。以下代码未按预期工作。

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

}

编辑:我希望它打印 25,作为借用的指针。得到140125321300160,可能是 float 的地址。

最佳答案

cblas_sdot 函数不应返回指针。

来自 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);

关于pointers - 从 Rust FFI 中检索一个 float 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16716877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com