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.5 required=5.0 tests=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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 5795EBC37 for ; Mon, 31 Aug 2009 16:52:14 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArQAAOaAm0oSBwdQkWdsb2JhbACPfIsnAQEBAQkLCgcTBa4LhWWISoQaBYFV X-IronPort-AV: E=Sophos;i="4.44,305,1249250400"; d="sig'?scan'208";a="35238387" Received: from biscayne-one-station.mit.edu ([18.7.7.80]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 31 Aug 2009 16:51:52 +0200 Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by biscayne-one-station.mit.edu (8.13.6/8.9.2) with ESMTP id n7VEpndP006574 for ; Mon, 31 Aug 2009 10:51:49 -0400 (EDT) Received: from SYDNEYPACIFIC-THREE-NINETY-THREE.MIT.EDU (SYDNEYPACIFIC-THREE-NINETY-THREE.MIT.EDU [18.95.6.138]) (authenticated bits=0) (User authenticated as farr@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id n7VEpmLu023770 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT) for ; Mon, 31 Aug 2009 10:51:48 -0400 (EDT) Message-Id: <54396DD1-659F-48D1-BC45-66A4FA2C6BB8@mit.edu> From: Will M Farr To: caml-list@inria.fr In-Reply-To: <9d3ec8300908310709l638fcf94l6a92e1ac4c98fb7f@mail.gmail.com> Content-Type: multipart/signed; protocol="application/pgp-signature"; micalg=pgp-sha1; boundary="Apple-Mail-19-692684672" Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v936) Subject: Re: [Caml-list] Optimizing Float Ref's Date: Mon, 31 Aug 2009 10:51:47 -0400 References: <9d3ec8300908310709l638fcf94l6a92e1ac4c98fb7f@mail.gmail.com> X-Pgp-Agent: GPGMail 1.2.0 (v56) X-Mailer: Apple Mail (2.936) X-Scanned-By: MIMEDefang 2.42 X-Spam: no; 0.00; ref's:01 unboxing:01 arrays:01 arrays:01 unboxed:01 yaron:01 minsky:01 yminsky:01 unboxed:01 mutable:01 unboxing:01 runtime:01 ocaml:01 dims:01 dims:01 X-Attachments: type="application/pgp-signature" name="PGP.sig" name="PGP.sig" This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --Apple-Mail-19-692684672 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Thanks to all who have replied. I didn't realize that references were always boxed (I thought that, internally, a reference was implemented as a one-element array, and therefore float refs would benefit from the automatic unboxing of float arrays). That's good to know. Will On Aug 31, 2009, at 10:09 AM, Till Varoquaux wrote: > True. All float records and float arrays are unboxed so modifications > don't have to pass through caml_modify. A single cell array will still > have dynamic bound checking so I would recommend going for the record. > Till > > On Sun, Aug 30, 2009 at 3:43 PM, Yaron Minsky > wrote: >> Float refs are not unboxed automatically, because refs >> Are polymorphic containers. If you create your own pseudo-ref, >> i.e., a >> record with a single mutable float field, then I believe you should >> get the >> behaviour you expect. >> >> Come to think of it, I wonder if it would be better to implement >> ref on top >> of a single-cell array, since then everyone would get the float >> unboxing >> whenever applicable. I imagine there is some runtime overhead to >> this, >> though. >> >> y >> >> >> 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 >> --Apple-Mail-19-692684672 content-type: application/pgp-signature; x-mac-type=70674453; name=PGP.sig content-description: This is a digitally signed message part content-disposition: inline; filename=PGP.sig content-transfer-encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.12 (Darwin) iEYEARECAAYFAkqb44MACgkQ1IoZbWY+dGz5qQCgm44B5fOkpGnlAlLaiLthIDRi MToAoIZdTwfmalmL5QPSPC2ZhcoSdBnG =BNQB -----END PGP SIGNATURE----- --Apple-Mail-19-692684672--