caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] HLVM stuff
Date: Sun, 27 Sep 2009 20:25:39 +0100	[thread overview]
Message-ID: <200909272025.39995.jon@ffconsultancy.com> (raw)
In-Reply-To: <BB742A9E-F98D-482D-8DA8-B3A2C5F71CD4@refined-audiometrics.com>

On Sunday 27 September 2009 18:43:24 David McClain wrote:
> ... as a specific example, I remember dealing with stacks of images
> taken from an infrared imaging sensor -- perhaps 256 images, each of
> which is, say 512 x 512 pixels. I needed to obtain median pixel values
> from the stack and produce one "median image", as well as perform
> thresholding, masking, perhaps some dilation and erosion. And, also
> get the image 2-D FFT image.
>
> So there we are dealing with an aggregate of 2^26 8-bit pixel values.
> Surely, the majority of time is spent processing pixels, and not
> dispatching over some polymorphic type...

No. That is actually an excellent example of exactly what I was talking about.

The solution most obvious to me would be something like:

  let median_image (images: _ [,] []) =
    let n = images.Length
    assert(n > 0)
    let h, w = images.[0].GetLength 0, images.[0].GetLength 1
    Array2D.parallelInit n h w (fun x y ->
      kthSmallest (n/2) (fun i -> images.[i].[y, x]))

where the "kthSmallest" and "Array2D.parallelInit" functions are both 
polymorphic. The former handles implicit sequences of any comparable type and 
the latter handles 2D arrays of any element type. This use of polymorphic 
functions would be slow in OCaml: you would have to inline and type 
specialize those functions by hand to get the best performance OCaml has to 
offer. OCaml's representation and handling of ints is inefficient so your 
performance will always be dire. Furthermore, OCaml cannot leverage 
multicores efficiently because you're returning a non-trivial result that 
would have to be marshalled in O(n) time.

In F#, you can mark the "kthSmallest" function "inline" to completely remove 
the overhead of the higher-order function and its closure argument. The 
parallelInit function uses the "n" to estimate a good initial batching for 
the parallel tasks and uses exponential backoff to avoid spending too much 
time spawning tasks. The result would be near optimal performance on my 8 
core with minimal effort.

OCaml is a hugely productive high-level language but many abstractions do 
incur severe performance penalties in OCaml and polymorphism is one of them. 
This is highly relevant to technical computing where the ability to write 
efficient code with minimal effort is extremely valuable.

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


  reply	other threads:[~2009-09-27 19:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-27 17:43 David McClain
2009-09-27 19:25 ` Jon Harrop [this message]
2009-09-27 21:58   ` [Caml-list] " David McClain
2009-09-27 23:14     ` Jon Harrop
2009-09-28  0:35       ` David McClain
2009-09-28  1:25         ` Jon Harrop
2009-10-13 22:18         ` Jon Harrop

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=200909272025.39995.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.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).