From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr 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 32D4FBBAF for ; Wed, 4 Feb 2009 13:41:45 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqkCAOgbiUmTIMAki2dsb2JhbACBbpJAAQEBCgsKGLtChBYG X-IronPort-AV: E=Sophos;i="4.37,378,1231110000"; d="scan'208";a="23558956" Received: from max.feld.cvut.cz ([147.32.192.36]) by mail1-smtp-roc.national.inria.fr with ESMTP; 04 Feb 2009 13:41:44 +0100 Received: from localhost (unknown [192.168.200.4]) by max.feld.cvut.cz (Postfix) with ESMTP id 10AC519F343B for ; Wed, 4 Feb 2009 13:41:44 +0100 (CET) X-Virus-Scanned: IMAP AMAVIS Received: from max.feld.cvut.cz ([192.168.200.1]) by localhost (styx.feld.cvut.cz [192.168.200.4]) (amavisd-new, port 10044) with ESMTP id 1AscmEHMJ-KB for ; Wed, 4 Feb 2009 13:41:42 +0100 (CET) Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34]) by max.feld.cvut.cz (Postfix) with ESMTP id 196FC19F3405 for ; Wed, 4 Feb 2009 13:41:42 +0100 (CET) Received: from localhost.fel.cvut.cz (meyer.felk.cvut.cz [147.32.84.20]) by imap.feld.cvut.cz (Postfix) with ESMTPSA id 05C1DFA003 for ; Wed, 4 Feb 2009 13:41:41 +0100 (CET) To: caml-list@yquem.inria.fr Subject: Ocaml <-> Matlab interface Reply-To: Jan Kybic From: Jan Kybic Date: Wed, 04 Feb 2009 13:41:41 +0100 Message-ID: <87myd2nzlm.fsf@fel.cvut.cz> User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Spam: no; 0.00; ocaml:01 bremond:01 interfacing:01 ocaml:01 matrices:01 segfaults:01 genarray:01 failwith:01 failwith:01 genarray:01 unix:01 matlab:01 matlab:01 argument:02 match:02 Good afternoon, I was using Ocamlmex (by Maurice Bremond) for interfacing between Matlab and Ocaml. This interface is very elegant, nice to work with and very complete. Unfortunately, I was getting quite frequent "segmentation faults" from Matlab when trying to pass matrices there and back. Probably some issues with memory management between Ocaml and Matlab. I am posting below the relevant excerpts from my code in case somebody can spot the problem. Anyway, I have created an alternative Ocaml<->Matlab interface. Matlab and Ocaml run as separate processes and communicate using Unix named pipes (created using mkfifo). This is slower and less complete than Ocamlmex solution but it is nevertheless usable and also quite stable (no segfaults). The code is very 'alpha' at the moment but if somebody is interested in using or improving it, just send me an email. I plan to release the source code when it matures a little. Yours, Jan Kybic -- ------------------------------------------------------------------------- Jan Kybic tel. +420 2 2435 5721 http://cmp.felk.cvut.cz/~kybic ICQ 200569450 To get an array from Matlab: let speed_f = match Mex.mxGetData speedm with | Mex.FLOAT32 s -> ( try array2_of_genarray s with Invalid_argument _ -> failwith "Input is not a 2D array." ) | _ -> failwith "Input is not float32 (class single)." in To pass an array to Matlab: let mxArray_of_floatarray f = let n,m = (Array2.dim1 f, Array2.dim2 f) in let r = Mex.mxCreateNumericArray [| n ; m |] Mex.DOUBLE_CLASS Mex.REAL in let a = Array2.create float64 fortran_layout n m in for j = 1 to m do for i = 1 to n do a.{i,j} <- f.{i-1,j-1} ; done ; done ; Mex.mxSetData r (Mex.FLOAT64 (genarray_of_array2 a)) ; r