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=2.8 required=5.0 tests=DNS_FROM_RFC_POST,SPF_NEUTRAL 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 B40C9BC37 for ; Mon, 31 Aug 2009 16:09:44 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlMCAFp2m0rRVdS6kWdsb2JhbACPfIpoPwEBAQEHDQoHEwOtM45hAQMCBIQWBYFVhx0 X-IronPort-AV: E=Sophos;i="4.44,305,1249250400"; d="scan'208";a="35235791" Received: from mail-vw0-f186.google.com ([209.85.212.186]) by mail1-smtp-roc.national.inria.fr with ESMTP; 31 Aug 2009 16:09:43 +0200 Received: by vws16 with SMTP id 16so4008867vws.1 for ; Mon, 31 Aug 2009 07:09:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type:content-transfer-encoding; bh=p1nZ8NGHBj3QckmrF3QrPFSa4alTOKZtjlf0IjSV1Sc=; b=GjFmBybzb5z/T/Hk10kW17Dtq78uMjXvB7fQ1gFxskV9U7jUlKLDfzmx9CqC/UhtsO tZ2vnRw7a9hLsjwC+hXIoLtiggb8cj8FFzUCI7PnuxefHAabM/KGiY71JCWmrpCPPxr2 smJYKWFpjM847XN1XwhYg2BLnd7b+Q5+vrcRk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=S/judvuJ0kt8lmpaR30qKmjQJqSIGZpE5cQY9vYuksJ1yfDDAwsvuLZivQBHAtdIS1 IVXTbdNBstjfdXAfjviVwm4qDYafVU6VlxWhVjNvkWgAXCAlPLWpSHqCjcbjcEkqyGU6 z0ZHgoKf+ZE5TiCMtKdBhB2UC6R2JwLyTq4Ro= MIME-Version: 1.0 Sender: till.varoquaux@gmail.com Received: by 10.220.69.89 with SMTP id y25mr6399997vci.67.1251727782917; Mon, 31 Aug 2009 07:09:42 -0700 (PDT) In-Reply-To: References: Date: Mon, 31 Aug 2009 10:09:42 -0400 X-Google-Sender-Auth: 67a834417393f53a Message-ID: <9d3ec8300908310709l638fcf94l6a92e1ac4c98fb7f@mail.gmail.com> Subject: Re: [Caml-list] Optimizing Float Ref's From: Till Varoquaux To: Yaron Minsky Cc: Will M Farr , "" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; ref's: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 ocamlopt:01 matrices:01 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 t= he > behaviour you expect. > > Come to think of it, I wonder if it would be better to implement ref on t= op > 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. =C2=A0The code was >> >> let mmmul store m1 m2 =3D >> =C2=A0let (ni,nk) =3D dims m1 and >> =C2=A0 =C2=A0 (nk2,nj) =3D dims m2 and >> =C2=A0 =C2=A0 (sni,snj) =3D dims store in >> =C2=A0assert(nk=3Dnk2); >> =C2=A0assert(ni=3Dsni); >> =C2=A0assert(nj=3Dsnj); >> =C2=A0for i =3D 0 to ni - 1 do >> =C2=A0 let row1 =3D m1.(i) and >> =C2=A0 =C2=A0 =C2=A0 srow =3D store.(i) in >> =C2=A0 for j =3D 0 to nj - 1 do >> =C2=A0 =C2=A0 let sum =3D ref 0.0 in =C2=A0 (* Un-boxed float ref? *) >> =C2=A0 =C2=A0 for k =3D 0 to nk - 1 do >> =C2=A0 =C2=A0 =C2=A0 let row2 =3D Array.unsafe_get m2 k in >> =C2=A0 =C2=A0 =C2=A0 let x =3D Array.unsafe_get row1 k and >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 y =3D Array.unsafe_get row2 j in >> =C2=A0 =C2=A0 =C2=A0 sum :=3D !sum +. x*.y >> =C2=A0 =C2=A0 done; >> =C2=A0 =C2=A0 Array.unsafe_set srow j !sum >> =C2=A0 done >> =C2=A0done; >> =C2=A0store >> >> (I compiled with ocamlopt.) =C2=A0It multiplies the matrices (represente= d as >> arrays of arrays of floats) m1 and m2 together and puts the result into = the >> matrix store. =C2=A0Profiling the code, I noticed a call to caml_modify = during >> the execution of this function! =C2=A0Turns out that the culprit was the= float >> ref "sum". =C2=A0Changing to the following code (which eliminates the fl= oat 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 =3D >> =C2=A0let (ni,nk) =3D dims m1 and >> =C2=A0 =C2=A0 (nk2,nj) =3D dims m2 in >> =C2=A0for i =3D 0 to ni - 1 do >> =C2=A0 let row1 =3D m1.(i) and >> =C2=A0 =C2=A0 =C2=A0 srow =3D store.(i) in >> =C2=A0 for j =3D 0 to nj - 1 do >> =C2=A0 =C2=A0 srow.(j) <- 0.0; >> =C2=A0 =C2=A0 for k =3D 0 to nk - 1 do >> =C2=A0 =C2=A0 =C2=A0 let row2 =3D Array.unsafe_get m2 k in >> =C2=A0 =C2=A0 =C2=A0 let x =3D row1.(k) and >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 y =3D row2.(j) in >> =C2=A0 =C2=A0 =C2=A0 srow.(j) <- srow.(j) +. x*.y >> =C2=A0 =C2=A0 done >> =C2=A0 done >> =C2=A0done; >> =C2=A0store >> >> But, I thought that float ref's were automatically unboxed by the compil= er >> when they didn't escape the local context. =C2=A0Is 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? =C2=A0Any enlightenment would be appre= ciated. >> >> 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 >