English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Threaded ocaml code, native threads, and linux SMP
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-12-17 (02:10)
From: Julian <chasm@r...>
Subject: Threaded ocaml code, native threads, and linux SMP
I wrote the following CPU-bound function in order to test the
Thread module in ocaml:

    let rec loop pair = match pair with
              (100000, 50000) -> ()
            | (     i, 50000) -> loop (i+1, 0)
            | (     i,     j) -> loop (i, j+1) ;;

This function was used in two test programs:

* Version 1 *

    let t1, t2 = Thread.create loop (0,0),
                 Thread.create loop (0,0) ;;

    Thread.join t1;;
    Thread.join t2;;

* Version 2 *

    loop (0,0);
    loop (0,0);;

The test programs were each compiled with the following command:
(ocaml 3.08, debian sid, linux kernel 2.6):

    ocamlopt -thread -o <name> unix.cmxa threads.cmxa <name>.ml

I then tried running the two versions on an SMP machine. I found that
whereas I expected version 1 to run roughly twice as fast, they actually
took the same amount of time.

Closer inspection using "ps" revealed that only one processor was being
used. When I wrote a similar test program in C, calling the pthread
functions directly, the threads were run on separate CPUs as expected.

The "ldd" command reveals that the C version and the ocaml version were
relying on the same native pthread library.

So why doesn't the ocaml version use 2 processors?
Is it a flaw with my program?
Did I compile the program incorrectly?

Thanks for any help you can give,