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

dylib open/call/close io functions, initial ffi api #394

Merged
merged 8 commits into from
Jun 25, 2024
Merged

Conversation

enricozb
Copy link
Contributor

@enricozb enricozb commented Jun 20, 2024

Overview

Adds DL_OPEN, DL_CALL, and DL_CLOSE IO functions.

C Runtime

Example usage looks like this: A user first defines some C functions they want to invoke through HVM at runtime:

#include "src/hvm.h"
#include <stdio.h>

Port add_two_nums(Net* net, Book* book, Port argm) {
  Tup tup = readback_tup(net, book, argm, 2);
  u32 num1 = get_u24(get_val(tup.elem_buf[0]));
  u32 num2 = get_u24(get_val(tup.elem_buf[1]));

  printf("adding numbers, %u and %u\n", num1, num2);

  return new_port(NUM, new_u24(num1 + num2));
}

Port print_num(Net* net, Book* book, Port argm) {
  u32 num = get_u24(get_val(argm));

  printf("printing number %u\n", num);

  return new_port(ERA, 0);
}

Functions must have the signature Port (my_func)(Net*, Book*, Port).

This file must be compiled as a shared library (.so). For example, gcc -shared my-funcs.c -o my-funcs.so. The file can then be loaded and symbols can be accessed. In Bend this looks like:

def main():
  x = 123
  y = 456

  with IO_T:
    dl <- call_io("DL_OPEN", ("./my-funcs.so", 0))
    res <- call_io("DL_CALL", (dl, "add_two_nums", x, y))
    * <- call_io("DL_CALL", (dl, "print_num", res))
    * <- call_io("DL_CLOSE", dl)

    return 42

C Compiled Mode

When compiling a generated HVM C file, you must use the -rdynamic flag to enable the shared library to access symbols from the main binary. For example,

cargo run -r -- gen-c testing-ffi.hvm > testing-ffi.c
gcc -rdynamic -lm testing-ffi.c -o testing-ffi

CUDA Runtime

The FFI is a little different, the above C file would look like this instead:

#include "src/hvm.cuh"
#include <stdio.h>

Port add_two_nums(GNet* gnet, Port argm) {
  Tup tup = gnet_readback_tup(gnet, argm, 2);
  u32 num1 = get_u24(get_val(tup.elem_buf[0]));
  u32 num2 = get_u24(get_val(tup.elem_buf[1]));

  printf("adding numbers, %u and %u\n", num1, num2);

  return new_port(NUM, new_u24(num1 + num2));
}

Port print_num(GNet* net, Port argm) {
  u32 num = get_u24(get_val(argm));

  printf("printing number %u\n", num);

  return new_port(ERA, 0);
}

And functions must have the signature Port (my_func)(GNet*, Port).

CUDA Compiled Mode

When compiling a generated HVM C file, you must use the -rdynamic flag to the host compiler to enable the shared library to access symbols from the main binary. For example,

cargo run -r -- gen-cu testing-ffi.hvm > testing-ffi.cu
nvcc --compiler-options=-rdynamic testing-ffi.c -o testing-ffi

HVM FFI API

Not everything is exposed to users at the moment, we expose

  • numeric readback and creation functions (since these are allocation-free)
  • readback_str, readback_tup, readback_bytes, and inject_bytes

See hvm.h for users of the C runtime.
See hvm.cuh for users of the CUDA runtime.

@HigherOrderBot

This comment has been minimized.

@enricozb enricozb changed the title dylib open/call/close io functions dylib open/call/close io functions, initial ffi api Jun 20, 2024
@HigherOrderBot

This comment has been minimized.

@enricozb enricozb marked this pull request as ready for review June 20, 2024 13:25
@HigherOrderBot

This comment has been minimized.

@HigherOrderBot
Copy link
Collaborator

Perf run for 0fc5635:

compiled
========

file            runtime         main            (local)       
==============================================================
sort_bitonic    c                        5.53s           4.28s
                cuda                     0.24s           0.23s
--------------------------------------------------------------
sum_rec         c                        1.42s           1.42s
                cuda                     0.14s           0.14s
--------------------------------------------------------------
sum_tree        c                        0.12s           0.13s
                cuda                     0.11s           0.10s
--------------------------------------------------------------
tuples          c                        3.72s           4.16s
                cuda                   timeout         timeout
--------------------------------------------------------------

interpreted
===========

file            runtime         main            (local)       
==============================================================
sort_bitonic    c                        6.48s           4.42s
                cuda                     0.24s           0.24s
                rust                   timeout         timeout
--------------------------------------------------------------
sum_rec         c                        1.83s           2.03s
                cuda                     0.14s           0.13s
                rust                    13.69s          14.10s
--------------------------------------------------------------
sum_tree        c                        0.25s           0.17s
                cuda                     0.08s           0.08s
                rust                     0.83s           0.84s
--------------------------------------------------------------
tuples          c                        2.52s           2.51s
                cuda                   timeout         timeout
                rust                     3.76s           3.82s
--------------------------------------------------------------


@enricozb enricozb merged commit 512eaef into main Jun 25, 2024
4 checks passed
@kings177 kings177 added this to the HVM IO lib v0 milestone Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants