Hello,
I followed this sample and everything compiles but dune exec ./bin/main.exe seems to fail silently. I did see the first print statement earlier.
open Ctypes
let () =
let size' = Unsigned.Size_t.of_int 4 in
let fp = CArray.of_list float [1.;1.;1.;1.] in
let conf = 0 in
let _ = C.Functions.init_distance_functions conf in
Printf.printf "DEBUG";
Printf.printf " %f" ( C.Functions.simd_euclidean (CArray.start fp) (CArray.start fp) size' );
This dune file looks complicated but I don’t see any error anywhere.
(executable
(public_name vectordb)
(name main)
; ctypes backward compatibility shims warn sometimes; suppress them
(flags (:standard -w -9-27))
(libraries ctypes ctypes-foreign )
(ctypes
(external_library_name libvector)
(build_flags_resolver
; Change the -I to point to the directory containing your header files,
; which you need for the ctypes struct definitions
(vendored
(c_flags :standard -Wall -Werror -I../libvector)
(c_library_flags :standard -Wall -Werror )))
(headers
(include "simd_distance.h" "simd_ops.h"))
(type_description
(instance Types)
(functor Type_description))
(function_description
; Set this to sequential if the library is not thread-safe
(concurrency unlocked)
(instance Functions)
(functor Function_description))
(generated_types Types_generated)
(generated_entry_point C))
(foreign_archives vectordb))
(data_only_dirs libvector)
(rule
(deps
(source_tree ../libvector))
(targets libvectordb.a dllvectordb.so)
(action
(no-infer
(progn
(chdir
../libvector
(run make))
(copy ../libvector/libvectordb.a libvectordb.a)
(copy ../libvector/dllvectordb.so dllvectordb.so)
))))
The Makefile is
CC = /opt/homebrew/opt/llvm/bin/clang
# CFLAGS = -O3 -march=armv9-a+sme -mcpu=apple-m4 -framework Accelerate
CFLAGS = -O3 -arch arm64 -mcpu=apple-m4 -framework Accelerate
all: libvectordb.a dllvectordb.so
simdd.o: simd_distance.c simd_distance.h
$(CC) $(CFLAGS) -c -o simdd.o simd_distance.c
simdo.o: simd_ops.c simd_ops.h
$(CC) $(CFLAGS) -c -o simdo.o simd_ops.c
libvectordb.a: simdd.o simdo.o
ar rcs $@ simdd.o simdo.o
@echo "Signing library..."
codesign -s - $@
dllvectordb.so: libvectordb.a
$(CC) -shared -o $@ $^
clean:
rm -f *.o *.a
I am signing my binary now because later I want to use M4’s Scalable Vector Extensions..
The C code is
float euclidean_fallback(const float* a, const float* b, size_t n) {
float sum = 0.0f;
for (size_t i = 0; i < n; i++) {
float diff = a[i] - b[i];
sum += diff * diff;
}
return sqrtf(sum);
}
function_description.ml
(* function_description.ml *)
open Ctypes
module Types = Types_generated
module Functions (F : Ctypes.FOREIGN) = struct
open F
let simd_euclidean = foreign "simd_euclidean" (ptr float @-> ptr float @-> Ctypes.size_t @-> returning float)
let init_distance_functions = foreign "init_distance_functions" (int @-> returning void)
end
I don’t see the printed values now.
Thanks.