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=1.3 required=5.0 tests=AWL,SPF_SOFTFAIL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id D29A0BC37 for ; Mon, 31 Aug 2009 13:41:21 +0200 (CEST) X-IronPort-AV: E=Sophos;i="4.44,305,1249250400"; d="scan'208";a="31949353" Received: from espone.inrialpes.fr (HELO [194.199.21.24]) ([194.199.21.24]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 31 Aug 2009 13:41:21 +0200 Message-ID: <4A9BB62E.3030907@inrialpes.fr> Date: Mon, 31 Aug 2009 13:38:22 +0200 From: Bertrand Jeannet User-Agent: Mozilla Thunderbird 1.0.2-6 (X11/20050513) X-Accept-Language: en-us, en MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Re: Optimizing Float Ref's (Yaron Minsky) References: <20090831100004.59CF5BC37@yquem.inria.fr> In-Reply-To: <20090831100004.59CF5BC37@yquem.inria.fr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; jeannet:01 jeannet:01 inrialpes:01 ref's:01 yaron:01 minsky:01 unboxing:01 ocaml:01 dims:01 dims:01 ocamlopt:01 matrices:01 arrays:01 arrays:01 ref's:01 The problem may not only be unboxing, but also allocation. In your first code, you allocate two new cells each time you enter in the j-loop (one for the boxed float, and one for the reference cell "sum" pointing to it). You should first move the line "let sum = ref 0.0" before the i-loop and just write at the same place "sum := 0". I will not be as efficient as your second version (because you still have boxing), but it should still be much better. Bertrand > On Aug 28, 2009, at 4:32 PM, Will M Farr wrote: > >> Hello all, >> >> I'm running OCaml 3.11.1, and I noticed something strange in some >> native code for matrix multiply today. The code was >> >> let mmmul store m1 m2 = >> let (ni,nk) = dims m1 and >> (nk2,nj) = dims m2 and >> (sni,snj) = dims store in >> assert(nk=nk2); >> assert(ni=sni); >> assert(nj=snj); >> for i = 0 to ni - 1 do >> let row1 = m1.(i) and >> srow = store.(i) in >> for j = 0 to nj - 1 do >> let sum = ref 0.0 in (* Un-boxed float ref? *) >> for k = 0 to nk - 1 do >> let row2 = Array.unsafe_get m2 k in >> let x = Array.unsafe_get row1 k and >> y = Array.unsafe_get row2 j in >> sum := !sum +. x*.y >> done; >> Array.unsafe_set srow j !sum >> done >> done; >> store >> >> (I compiled with ocamlopt.) It multiplies the matrices (represented >> as arrays of arrays of floats) m1 and m2 together and puts the result >> into the matrix store. Profiling the code, I noticed a call to >> caml_modify during the execution of this function! Turns out that >> the culprit was the float ref "sum". Changing to the following code >> (which eliminates the float ref, and uses the <- and .( ) operators >> instead of unsafe_set and unsafe_get) eliminated that call, and sped >> things up tremendously: >> >> let mmmul store m1 m2 = >> let (ni,nk) = dims m1 and >> (nk2,nj) = dims m2 in >> for i = 0 to ni - 1 do >> let row1 = m1.(i) and >> srow = store.(i) in >> for j = 0 to nj - 1 do >> srow.(j) <- 0.0; >> for k = 0 to nk - 1 do >> let row2 = Array.unsafe_get m2 k in >> let x = row1.(k) and >> y = row2.(j) in >> srow.(j) <- srow.(j) +. x*.y >> done >> done >> done; >> store >> >> But, I thought that float ref's were automatically unboxed by the >> compiler when they didn't escape the local context. Is this a >> complier bug, is there a bad interaction with unsafe_get and >> unsafe_set, or is there something else going on that I don't >> understand? Any enlightenment would be appreciated. >> >> Thanks! >> Will >> _______________________________________________ >> Caml-list mailing list. Subscription management: >> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list >> Archives: http://caml.inria.fr >> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners >> Bug reports: http://caml.inria.fr/bin/caml-bugs > > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs -- Projet POP-ART, INRIA Rhône-Alpes Zirst - 655 avenue de l'Europe - Montbonnot F-38334 Saint Ismier Cedex Fax: +33 (0)4 76 61 52 52 Tel: +33 (0)4 76 61 52 76 Bertrand.Jeannet@inrialpes.fr