Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose hvm_c and hvm_cu in lib.rs #367

Open
developedby opened this issue May 31, 2024 · 2 comments
Open

Expose hvm_c and hvm_cu in lib.rs #367

developedby opened this issue May 31, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@developedby
Copy link
Member

For people who want to use HVM as a library, it would be useful to give access not only to the rust runtime, but to the C and CUDA runtimes as well.

I want to have this as an option in Bend so that we can provide users with a default HVM version that is guaranteed to work out of the box, removing the need to install HVM as an external dependency and making sure that users always have one option of HVM that is compatible with their version of Bend.

@developedby developedby added the enhancement New feature or request label May 31, 2024
@edusporto
Copy link
Contributor

edusporto commented May 31, 2024

Something like what I did in the interop branch can be used to do this:

HVM/src/main.rs

Lines 84 to 90 in 0e9c596

unsafe {
let layout = alloc::Layout::new::<interop::NetC>();
let net_ptr = alloc::alloc(layout) as *mut interop::NetC;
hvm_c(data.as_mut_ptr() as *mut u32, net_ptr, run_io);
// hvm_c(data.as_mut_ptr() as *mut u32, std::ptr::null(), run_io);
alloc::dealloc(net_ptr as *mut u8, layout);
}

net_ptr can then be read back into a Rust Net which could then be used as a library directly, without having to call hvm as a subprocess and read its stdout output.

I changed the original read back function to take an abstract NetReadback, which currently already works for the net coming from C and would take a bit more effort to work with the CUDA implementation.

HVM/src/ast.rs

Line 399 in 0e9c596

pub fn readback<N: interop::NetReadback>(net: &N, book: &hvm::Book) -> Option<Net> {

HVM/src/interop.rs

Lines 9 to 12 in 0e9c596

pub trait NetReadback {
fn enter(&self, var: Port) -> Port;
fn node_load(&self, loc: usize) -> Pair;
}

Is this an appropriate solution to this issue or are you looking for something else?

@developedby
Copy link
Member Author

Yes, except that hvm_c shouldn't print in that case. Otherwise I'd still need to call it as a subprocess to be able to intercept it's output (which is not that much of a problem)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants