caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: wester@ilt.fhg.de
Cc: caml-list@inria.fr
Subject: Re: Floating point array computations
Date: Thu, 1 Feb 2001 14:57:05 +0100	[thread overview]
Message-ID: <20010201145705.A1034@pauillac.inria.fr> (raw)
In-Reply-To: <200101251034.LAA06543@ilt.fhg.de>; from wester@ilt.fhg.de on Thu, Jan 25, 2001 at 11:34:32AM +0100

> In sample code form the ICFP 200 contest I found:
> type v = float array
> is array a build in type?

Yes, it is.

> The functions main1 .. main4 below do the same but take different time for it
> (compiled with ocamlopt). I expected main2 to be the fastest but main3 is 
> fastest. main3 300 300 1000 takes on my PC (450 Mhz PI, 520 MB RAM) 
> 26 sec, this is about twice the time of C++ and about the same as Java.
> Can somone tell me why main3 is faster than main2 (36 sec) although
> main3 makes function calls to step?

Here are two possible explanations.  First, with modern processors,
minor variations in code placement (i.e. insert or delete unused code)
can result in performance improvements or degradations by as much as 20%,
due to cache effects, alignment constraints on instruction fetches,
and what not.  (Predicting accurately the performance of a processor
such as the PIII is extremely hard; a few Intel engineers are rumored
to be able to do it :-)

Second, on a register-poor architecture such as the IA32, spilling and
reloading (moving values between registers and stack locations)
happens often and has a significant performance impact.  Since OCaml
follows a callee-save model, function calls act as natural spill hint,
forcing all live registers to be spilled to the stack before the call,
then reloaded on demand after the call.

In your "main3" test, the function "step" runs in exactly the 7
available registers, thus the call to "step" causes a nearly perfect
placement of spills: spill everything live before the inner loop; run
the inner loop in registers; reload the spilled data.

In "main2", there is no such function call to help the compiler, and
the placement of spills and reloads it chooses is not as good
(although still reasonable).

> I also tried Array.unsafe_get/set in main4. I expected this to be even faster
> because I have seen it in the ICFP contest sample code. But actually 
> it is much slower (about 100 sec). Did I make anything wrong?

You hit a rather subtle point in the OCaml compiler, so don't feel too
bad about it.  Array.unsafe_get/set are not quite regular Caml functions,
but rather predefined operators (like + or +.) with special
compilation schemes (e.g. open-coding and type specialization) *when
the operator is fully applied*.  This means that

        Array.unsafe_get a i

will generate optimized code.  But the following rebinding

        let get = Array.unsafe_get

where Array.unsafe_get is not fully applied, gets compiled as

        let get x y = Array.unsafe_get x y

so subsequent calls to get may incur the cost of a function call
(although ocamlopt will actually inline the function at point of call).
More subtly, the definition of "get" is polymorphic, hence no type
specialization occurs, and it happens that accesses to float arrays
are compiled much more efficiently if the array is statically known to
have type "float array".  

In summary, for maximal performance, you should not write

        let get = Array.unsafe_get
        ... get ... get ...

but instead write

        ... Array.unsafe_get ... Array.unsafe_get ...

With this modification, your "main4" example runs a bit faster than
"main3" (20-25% faster), because Array.unsafe_get/set do not perform
array bounds checking.  However, this is unsafe: an error in your
code, causing an out-of-bound access, can very easily crash the
program.  So, this is a high price to pay in terms of reliability for
a relatively small gain in performance, and I'd advise against it.

> Would Bigarray perform better? 

I tried to rewrite your code using a 2-d bigarray, and it was clearly
slower.  Accessing a 2-d bigarray involves
        two bound checks
        one integer multiplication
        one integer addition
        one load,
while accessing a float array array (as returned by Array.make_matrix)
involves
        two bound checks
        two integer additions
        two loads
On the Pentium at least, the integer multiplication is much slower
than the load.  Moreover, OCaml doesn't perform hoisting of loop
invariant subexpressions, meaning that the multiplication is performed
at each iteration of the inner loop.  And since the multiplication is
performed "under the hood" by the compiler, it cannot be manually
hoisted out of the inner loop, like you did in main2 and main3 for 
hoisting the outer array access (1 bound check, 1 addition, 1 load).

> I would be very appriciative for your help to clarify this points.

I hope this helps, despite the rather technical explanations.

- Xavier Leroy



      reply	other threads:[~2001-02-02 15:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-01-25 10:34 wester
2001-02-01 13:57 ` Xavier Leroy [this message]

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=20010201145705.A1034@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=wester@ilt.fhg.de \
    /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).