caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Joel Reymont <joelr1@gmail.com>
To: caml-list <caml-list@inria.fr>
Subject: [Caml-list] optimizing numerical code
Date: Wed, 18 May 2011 20:35:43 +0200	[thread overview]
Message-ID: <346B52D2-EE21-43D1-B41E-3AEB3BBF0013@gmail.com> (raw)

Consider the following two functions that I'm trying to optimize.

Why is there an allocation for every iteration of the loop in divergence1 but not in divergence2?

What else can I do to make divergence2 faster, apart from using -unsafe?

	Thanks in advance, Joel

---

let js_divergence1 v1 v2 =
  let acc = ref 0. in
  for i = 0 to (Array.length v1) - 1 do
    let x = v1.(i)
    and y = v2.(i) in
    let m = 0.5 *. (x +. y) in
    let d1 = x *. log (x /. m) 
    and d2 = y *. log (y /. m) in
    acc := !acc +. d1 +. d2
  done;
  (!acc)

let js_divergence2 v1 v2 =
  let sum1 = ref 0.
  and sum2 = ref 0. in
  for i = 0 to (Array.length v1) - 1 do
    let x = v1.(i)
    and y = v2.(i) in
    let m = 2. /. (x +. y) in
    let d1 = x *. log (x *. m) 
    and d2 = y *. log (y *. m) in
    sum1 := !sum1 +. d1;
    sum2 := !sum2 +. d2;
  done;
  !sum1 +. !sum2

ocamlopt -dcmm

(function camlFoo__js_divergence1_1030 (v1/1031: addr v2/1032: addr)
 (let acc/1033 "camlFoo__3"
   (let (i/1034 1 bound/1066 (+ (or (>>u (load (+a v1/1031 -8)) 9) 1) -2))
     (catch
       (if (> i/1034 bound/1066) (exit 17)
         (loop
           (let
             (x/1067
                (seq (checkbound (>>u (load (+a v1/1031 -8)) 9) i/1034)
                  (load float64u (+a (+a v1/1031 (<< i/1034 2)) -4)))
              y/1068
                (seq (checkbound (>>u (load (+a v2/1032 -8)) 9) i/1034)
                  (load float64u (+a (+a v2/1032 (<< i/1034 2)) -4)))
              m/1069 (*f 0.5 (+f x/1067 y/1068))
              d1/1070 (*f x/1067 (extcall "log" (/f x/1067 m/1069) float))
              d2/1071 (*f y/1068 (extcall "log" (/f y/1068 m/1069) float)))
             (assign acc/1033
                       (alloc 1277
                         (+f (+f (load float64u acc/1033) d1/1070) d2/1071))))
           (let i/1065 i/1034 (assign i/1034 (+ i/1034 2))
             (if (== i/1065 bound/1066) (exit 17) []))))
     with(17) []))
   acc/1033))

(function camlFoo__js_divergence2_1040 (v1/1041: addr v2/1042: addr)
 (let (sum1/1055 0. sum2/1056 0.)
   (let (i/1045 1 bound/1058 (+ (or (>>u (load (+a v1/1041 -8)) 9) 1) -2))
     (catch
       (if (> i/1045 bound/1058) (exit 16)
         (loop
           (let
             (x/1059
                (seq (checkbound (>>u (load (+a v1/1041 -8)) 9) i/1045)
                  (load float64u (+a (+a v1/1041 (<< i/1045 2)) -4)))
              y/1060
                (seq (checkbound (>>u (load (+a v2/1042 -8)) 9) i/1045)
                  (load float64u (+a (+a v2/1042 (<< i/1045 2)) -4)))
              m/1061 (/f 2. (+f x/1059 y/1060))
              d1/1062 (*f x/1059 (extcall "log" (*f x/1059 m/1061) float))
              d2/1063 (*f y/1060 (extcall "log" (*f y/1060 m/1061) float)))
             (assign sum1/1055 (+f sum1/1055 d1/1062))
             (assign sum2/1056 (+f sum2/1056 d2/1063)))
           (let i/1057 i/1045 (assign i/1045 (+ i/1045 2))
             (if (== i/1057 bound/1058) (exit 16) []))))
     with(16) []))
   (alloc 1277 (+f sum1/1055 sum2/1056))))

--------------------------------------------------------------------------
- for hire: mac osx device driver ninja, kernel extensions and usb drivers
---------------------+------------+---------------------------------------
http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont
---------------------+------------+---------------------------------------





             reply	other threads:[~2011-05-18 18:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-18 18:35 Joel Reymont [this message]
2011-05-18 18:50 ` Alain Frisch
2011-05-19  8:24   ` Alain Frisch
2011-05-19  8:37     ` Joel Reymont
2011-05-19 10:59       ` Alain Frisch
2011-05-19 12:40         ` Gerd Stolpmann
2011-06-09  9:02           ` Alain Frisch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=346B52D2-EE21-43D1-B41E-3AEB3BBF0013@gmail.com \
    --to=joelr1@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).