From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 9AA3CBBAF for ; Mon, 18 Jan 2010 10:06:06 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgcCAIKzU0tIDtydkGdsb2JhbACPc4FwghmHQT8BAQEBCQkMBxMDq2uBMoQOhlYBAgMFhC0Ein0 X-IronPort-AV: E=Sophos;i="4.49,295,1262559600"; d="scan'208";a="45089975" Received: from fg-out-1718.google.com ([72.14.220.157]) by mail1-smtp-roc.national.inria.fr with ESMTP; 18 Jan 2010 10:06:06 +0100 Received: by fg-out-1718.google.com with SMTP id e21so49769fga.3 for ; Mon, 18 Jan 2010 01:06:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:reply-to:in-reply-to :references:date:message-id:subject:from:to:content-type; bh=/ly8rcaRorggHYKmDpN9OV2hAn2aR4C0Ge/NwL8yx0w=; b=agbvJ+4LqGUZh0tv0my6EGyDx0ryznIf/haEz6eyiChDM5FaT5wm85jRXGn8OVWuy4 T2kSFfyfwDZxXi60YiGoO+Zhg6ZZ3aumUlGomWZKx0afrfhhLu8mOJ4bVI9Je1IgHy9k vFY+K4aLkbkTWp8dHSsx7DnXb7zCMAa9hWHgk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:reply-to:in-reply-to:references:date:message-id :subject:from:to:content-type; b=DkdaMKnUPWDMzIofktyuC8Wnh+R6bAQOasN89iBsqFrzgV61nXh6e61OrwuQemLJaN 8C64JLPsnS/h6CAz7qZnER+uWFwcMP5Mnv0WCldexM3u2g67OzO8O44TFQnVuiOMdY8r eCNkfHCQHCuJlfRQjHShoI3yTAymP4Xezr2Tk= MIME-Version: 1.0 Received: by 10.103.127.4 with SMTP id e4mr2888043mun.102.1263805566209; Mon, 18 Jan 2010 01:06:06 -0800 (PST) Reply-To: odalric.maillard@inria.fr In-Reply-To: <406c33611001150943o2aeb2429qefa22f1102699730@mail.gmail.com> References: <406c33611001150943o2aeb2429qefa22f1102699730@mail.gmail.com> Date: Mon, 18 Jan 2010 10:06:06 +0100 Message-ID: <406c33611001180106k678d21e8i34d2df8801da32c8@mail.gmail.com> Subject: Re: OCaml, C, Garbage Collector etc From: Odalric-Ambrym Maillard To: caml-list@yquem.inria.fr, Sylvain Le Gall Content-Type: multipart/alternative; boundary=0016e65c8896d6d0bd047d6ca9d3 X-Spam: no; 0.00; ocaml:01 wrappers:01 ocaml:01 camlparam:01 camlreturn:01 camlparam:01 val:01 val:01 camlreturn:01 alloc:01 wosize:01 printf:01 additionnal:01 initialized:01 initialized:01 --0016e65c8896d6d0bd047d6ca9d3 Content-Type: text/plain; charset=ISO-8859-1 Hello, I have reduced my problem to the following question : I have this code in C, these are the wrappers corresponding to the Ocaml functions. I call the three following functions from C code. They correspond respectively to functions of type : int -> unit, int -> unit and int -> float array -> float. * My concern is about the good (w.r.t gc) way of calling Ocaml functions with return type unit from C.* Thus where to put Camlparam, and Camlreturn and how etc... I have tried this but it fails too : ------------------------------------------------------------ void init_tree_c(int m) { CAMLparam0(); CAMLlocal2(val_m,res); val_m = Val_int(m); res=caml_callback(*closure_init_tree, val_m); CAMLreturn0; } void set_depth_c(int max_depth) { CAMLparam0(); CAMLlocal2(val_max_depth,res); val_max_depth = Val_int(max_depth); res=caml_callback(*closure_set_depth, val_max_depth); CAMLreturn0; } double psi_c(int m, int dim_x,double * x){ int d; CAMLparam0(); CAMLlocal3(val_m,val_x,res); val_m = Val_int(m); val_x = caml_alloc(dim_x * Double_wosize, Double_array_tag); for(d=0;d random_tree := Array.make m Empty;print_int m;print_string " trees initialized.";print_newline();), (function m -> Array.get !random_tree m;), (function d -> depth := d;print_string "Maximum depth initialized to ";print_int d;print_newline();), (function m -> (function x -> let t,v = aux (Array.get !random_tree m) !depth 0 1 (Array.length x) x in Array.set !random_tree m t; v)) );; ------------------------------------------------------------ Note that : random_tree contains an arrray of m trees, and that this variable is shared by the different functions. Of course there is the register code etc... Callback.register "init_tree_Ocaml" init_tree;; Callback.register "set_depth_Ocaml" set_depth;; Callback.register "psi_Ocaml" psi;; ... --0016e65c8896d6d0bd047d6ca9d3 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hello,

I have reduced my problem to the following question :

= I have this code in C, these are the wrappers corresponding to the Ocaml fu= nctions. I call the three following functions from C code. They correspond = respectively to functions of type : int -> unit,=A0 int -> unit and i= nt -> float array -> float.

My concern is about the good (w.r.t gc) way of calling Ocaml functio= ns with return type unit from C.


Thus where to put Camlparam, an= d Camlreturn and how etc... I have tried this but it fails too :
-------= -----------------------------------------------------
void init_tree_c(int m) {
=A0 CAMLparam0();
=A0 CAMLlocal2(val_m,res)= ;
=A0 val_m =3D Val_int(m);
=A0 res=3Dcaml_callback(*closure_init_tre= e, val_m);
=A0 CAMLreturn0;
}

void set_depth_c(int max_depth) = {
=A0 CAMLparam0();
=A0 CAMLlocal2(val_max_depth,res);
=A0 val_max_depth =3D Val_int(max_dep= th);
=A0 res=3Dcaml_callback(*closure_set_depth, val_max_depth);
=A0 = CAMLreturn0;
}

double psi_c(int m, int dim_x,double * x){
=A0 = int d;
=A0 CAMLparam0();
=A0 CAMLlocal3(val_m,val_x,res);
=A0 val_m =3D Val_= int(m);
=A0 val_x =3D caml_alloc(dim_x * Double_wosize, Double_array_tag= );
=A0 for(d=3D0;d<dim_x;d++){
=A0=A0=A0 //=A0=A0=A0 printf("= Stored %f",x[d]);
=A0=A0=A0 Store_double_field(val_x, d,x[d]);
=A0 }
=A0 res =3D caml_c= allback2( *closure_psi, val_m, val_x);
=A0 CAMLreturn(Double_val(res));<= br>=A0 }
------------------------------------------------------------

Here is= the corresponding OCaml Code (with an additionnal get_tree function):
(= I have added some printers for debug purpose).

------------------------------------------------------------
let (init_t= ree, get_tree, set_depth, psi) =3D
=A0 let random_tree =3D ref [|Empty|= ] and depth =3D ref 3 in
=A0=A0=A0 (
=A0=A0=A0=A0=A0 (function m ->= ; random_tree :=3D Array.make m Empty;print_int m;print_string " trees= initialized.";print_newline();),
=A0=A0=A0=A0=A0 (function m -> Array.get !random_tree m;),
=A0=A0=A0= =A0=A0 (function d -> depth :=3D d;print_string "Maximum depth init= ialized to ";print_int d;print_newline();),
=A0=A0=A0=A0=A0 (functi= on m -> (function x ->=A0=A0
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 let t,v =3D aux (Array.get !random_tree m) !d= epth 0 1 (Array.length x) x in
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 Array.set != random_tree m t;
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 v))
=A0=A0=A0 );;
------------------------------------------------------------
Note that := random_tree contains an arrray of m trees, and that this variable is share= d by the different functions.

Of course there is the register code= =A0 etc...

Callback.register "init_tree_Ocaml" init_tree;;
Callback.r= egister "set_depth_Ocaml" set_depth;;
Callback.register "= psi_Ocaml" psi;;
...

--0016e65c8896d6d0bd047d6ca9d3--