Created
December 7, 2011 10:31
-
-
Save anonymous/1442320 to your computer and use it in GitHub Desktop.
Modifying OCaml Bigarray from C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Bigarray | |
external mutation_test : (float, float64_elt, c_layout) Array2.t -> unit = "mutation_test" | |
let a = Array2.create float64 c_layout 2 2 | |
let () = | |
Array2.set a 0 0 1.; | |
Array2.set a 0 1 2.; | |
Array2.set a 1 0 3.; | |
Array2.set a 1 1 4.; | |
mutation_test a; (* print elements and change a[0][0] to 5.0 *) | |
Printf.printf "\nmutated value: %f\n" (Array2.get a 0 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: run opt | |
run: main.run | |
opt: main.opt | |
OCAMLC=ocamlfind ocamlc -annot | |
OCAMLOPT=ocamlfind ocamlopt | |
# C | |
%.o: %.c | |
$(OCAMLOPT) -c $< | |
dllmatrix.so: matrix.o | |
ocamlmklib -o matrix $< | |
#OCaml bytecode | |
%.cmo: %.ml | |
$(OCAMLC) -c $< | |
main.cma: main.cmo dllmatrix.so | |
$(OCAMLC) -a -o $@ $< -dllib -lmatrix -package bigarray -linkpkg | |
main.run: main.cma main.cmo | |
$(OCAMLC) -custom -o $@ $^ libmatrix.a | |
#OCaml optcode | |
%.cmx: %.ml | |
$(OCAMLOPT) -c $< | |
libmatrix.a: main.cmx dllmatrix.so | |
$(OCAMLOPT) -a -o main.cmxa $< -cclib -lmatrix | |
main.opt: main.cmx libmatrix.a | |
$(OCAMLOPT) -o $@ $^ -ccopt -L./ -package bigarray -linkpkg | |
.PHONY: clean all run opt | |
clean: | |
rm -f *.[oa] *.so *.cm[ixoa] *.cmxa *.out *~ *.run *.opt *.annot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <caml/mlvalues.h> | |
#include <caml/bigarray.h> | |
value mutation_test(value bigarray){ // print elements of a OCaml bigarray and mutate (0,0) element. | |
int i; | |
int dimx = Bigarray_val(bigarray)->dim[0]; | |
int dimy = Bigarray_val(bigarray)->dim[1]; | |
printf("dim: %d, %d\n", dimx, dimy); | |
double *array = Data_bigarray_val(bigarray); | |
for (i = 0; i < dimx * dimy; ++i) | |
printf("val: %f\n", array[i]); | |
array[0] = 5.0; | |
printf("a[0][0] was changed to 5.0 in the C code\n"); | |
return Val_unit; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dim: 2, 2 | |
val: 1.000000 | |
val: 2.000000 | |
val: 3.000000 | |
val: 4.000000 | |
a[0][0] was changed to 5.0 in the C code | |
mutated value: 5.000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment