Fairness of ocaml native-code scheduler?

Fairness of ocaml native-code scheduler?

Post by Vincenzo C » Wed, 20 Oct 2004 00:12:01


Hi all, first of all I solved all my troubles with multiple threads calling
C calling back ocaml again, it works like a charm, and thanks to all who
helped :)

Now I see that if I spawn a couple of threads calculating the fibonacci
function, bytecode runs fairly, while native-code allows the first created
thread to eat the whole CPU. I have ocaml 3.08-r1 and my simple test is:

let rec fib x = if x < 2 then 1 else fib (x-1) + fib (x-2)

let _ = Thread.create (fun x -> fib x;Printf.printf "Thread1 done\n%!") 42
let _ = Thread.create (fun x -> fib x;Printf.printf "Thread2 done\n%!") 20

let _ = Thread.delay 30.0

I can put any number I want in place of 20, even less than 2,but thread1
will always eat all the cpu, then the system will print "Tread2 done" and
"Thread1 done" in a short sequence, which probably means that what gets
blocked is I/O. Any advice? Bytecode is perfect, if I set the two number
equals they get out in a varying order, if one of the two is smaller than
the other it comes out first.

Apart from this, which should not be a crucial point, is it me or aren't
there options to control optimization (like -O in gcc and ghc) in ocamlopt?

Bye

Vincenzo

--
Please notice that my e-mail address has been altered to prevent spam
 
 
 

Fairness of ocaml native-code scheduler?

Post by Olivier An » Wed, 20 Oct 2004 17:57:15


Vincenzo Ciancia [Mon, 18 Oct 2004]:
> Now I see that if I spawn a couple of threads calculating the
> fibonacci function, bytecode runs fairly, while native-code allows
> the first created thread to eat the whole CPU. I have ocaml 3.08-r1
> and my simple test is:
>
> let rec fib x = if x < 2 then 1 else fib (x-1) + fib (x-2)
>
> let _ = Thread.create (fun x -> fib x;Printf.printf "Thread1 done\n%!") 42
> let _ = Thread.create (fun x -> fib x;Printf.printf "Thread2 done\n%!") 20
>
> let _ = Thread.delay 30.0
>
> I can put any number I want in place of 20, even less than 2,but thread1
> will always eat all the cpu, then the system will print "Tread2 done" and
> "Thread1 done" in a short sequence, which probably means that what gets
> blocked is I/O. Any advice? Bytecode is perfect, if I set the two number
> equals they get out in a varying order, if one of the two is smaller than
> the other it comes out first.

I think the problem comes from the fact that your fib function is too
simple : it never use the caml runtime. Releasing the global mutex can
only be done by the caml runtime, so if you have a long piece of code
that does not allocate anything, scheduling cannot occur (and signals
aren't delivered).

> Apart from this, which should not be a crucial point, is it me or
> aren't there options to control optimization (like -O in gcc and
> ghc) in ocamlopt?

The only performance-related options of ocamlopt I know of are -unsafe
(disable string and array bound checks), -inline (set aggressiveness
of inlining) and -ffast-math (use CPU instructions for trig and exp
functions).

--
Olivier