Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Multiprocessor support in OCaml

113 views
Skip to first unread message

Jason Ganetsky

unread,
Apr 22, 2007, 3:43:58 AM4/22/07
to caml...@yquem.inria.fr
Hi all,

I'm new to this list, and new to OCaml (although, have some experience with
SML).

Anyway, I have recently written an OCaml thread pool implementation, on top
of the Thread and Event modules. I did this for the purpose of exploiting an
SMP system I have, and was a disappointed to read today that OCaml doesn't
support multiprocessor systems.

I played around with it a little, and discovered that by liberally calling
Thread.yield, I do cajole my threads into running on multiple processors. Is
this behavior normal, or have I discovered a problem with the Thread module?
I'm certainly happy that I can get it to use my SMP... but I will stop it at
once if you tell me that this is unsafe.

-Jason

Richard Jones

unread,
Apr 22, 2007, 4:45:31 AM4/22/07
to Jason Ganetsky
On Sun, Apr 22, 2007 at 03:42:09AM -0400, Jason Ganetsky wrote:
> I'm new to this list, and new to OCaml (although, have some experience with
> SML).

There's a beginner's list:
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

> Anyway, I have recently written an OCaml thread pool implementation, on top
> of the Thread and Event modules. I did this for the purpose of exploiting an
> SMP system I have, and was a disappointed to read today that OCaml doesn't
> support multiprocessor systems.
>
> I played around with it a little, and discovered that by liberally calling
> Thread.yield, I do cajole my threads into running on multiple processors. Is
> this behavior normal, or have I discovered a problem with the Thread module?
> I'm certainly happy that I can get it to use my SMP... but I will stop it at
> once if you tell me that this is unsafe.

The garbage collector doesn't support concurrency, so there's a big
global lock around all OCaml code.

http://caml.inria.fr/pub/ml-archives/caml-list/2002/11/64c14acb90cb14bedb2cacb73338fb15.en.html

Rich.

--
Richard Jones
Red Hat

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Xavier Leroy

unread,
Apr 22, 2007, 6:31:20 AM4/22/07
to Jason Ganetsky
> Anyway, I have recently written an OCaml thread pool implementation, on
> top of the Thread and Event modules. I did this for the purpose of
> exploiting an SMP system I have, and was a disappointed to read today
> that OCaml doesn't support multiprocessor systems.

You are correct that OCaml *threads* do not exploit multiprocessing.
Basically, only one OCaml thread can run at a time.

You can still get parallelism in several ways. First, external C
libraries called from OCaml can run in parallel with OCaml code
provided the OCaml/C interface for these libraries makes uses of the
"blocking section" mechanism. Second, process-level parallelism works
very well with programs written in message-passing style, using e.g.
OcamlMPI or OCamlP3L.

> I played around with it a little, and discovered that by liberally
> calling Thread.yield, I do cajole my threads into running on multiple
> processors.

This is an illusion. Thread.yield gives more opportunities to the OS
scheduler to reschedule a Caml thread on a different processor, but
you're not gaining parallelism this way and you might actually lose
performance (because of cache ping-pong effects and the like).

- Xavier Leroy

Erik de Castro Lopo

unread,
Apr 22, 2007, 7:00:02 AM4/22/07
to caml...@yquem.inria.fr
Jason Ganetsky wrote:

> Hi all,
>
> I'm new to this list, and new to OCaml (although, have some experience with
> SML).
>
> Anyway, I have recently written an OCaml thread pool implementation, on top
> of the Thread and Event modules. I did this for the purpose of exploiting an
> SMP system I have, and was a disappointed to read today that OCaml doesn't
> support multiprocessor systems.

For real multi-procesor parallelism, have a look at this:

http://www.pps.jussieu.fr/~dicosmo/ocamlp3l/

Haven't tried it myself, but its on my todo list.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo
+-----------------------------------------------------------+
"life is too long to be an expert at harmful things, including
such evilness as C++ and perl." -- Erik Naggum

Don Syme

unread,
Apr 22, 2007, 7:57:09 AM4/22/07
to Xavier Leroy, Jason Ganetsky

Just to mention there is a way of getting multiple concurrently executing OCaml threads in a program, which I discovered a while back: you can statically link multiple independent copies of the OCaml runtime, each into its own DLL (on Windows). This allows multiple independent OCaml threads to run concurrently.

I presume this technique works well enough for SMP up to 2-4 processors, though have never done any serious performance testing.

The OCaml programs must not, of course, trade OCaml values, but can communicate in-process by other means (e.g. shared C memory or some other message passing technique).

Regards,
Don

P.S. I've only used this technique on Windows.

Jon Harrop

unread,
Apr 22, 2007, 9:30:34 AM4/22/07
to caml...@yquem.inria.fr
On Sunday 22 April 2007 11:58, Erik de Castro Lopo wrote:
> For real multi-procesor parallelism, have a look at this:
>
> http://www.pps.jussieu.fr/~dicosmo/ocamlp3l/
>
> Haven't tried it myself, but its on my todo list.

Has anyone tries this? Got any demos? I've been meaning to look at it for a
while as well... :-)

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal
http://www.ffconsultancy.com/products/fsharp_journal/?e

Zheng Li

unread,
Apr 22, 2007, 1:33:09 PM4/22/07
to caml...@inria.fr

Hi,

I'm working on a process back-end of STM library. It's now supported by Google
SOC and expected to release after the summer (and maybe earlier). With it, you
will be able to do shared-memory (supposing that's the style your want)
parallel programming based on processes, which in turn gives you speedup.

If interested, you can have a taste first through the (vm)thread back-end
currently available (check my homepage below), though it won't really speed up
your program because of the well-known global lock of OCaml threads.

"Jason Ganetsky" <jason.g...@gmail.com> writes:
> Hi all,
> I'm new to this list, and new to OCaml (although, have some experience with

> SML).Anyway, I have recently written an OCaml thread pool implementation, on


> top of the Thread and Event modules. I did this for the purpose of exploiting
> an SMP system I have, and was a disappointed to read today that OCaml doesn't
> support multiprocessor systems.
>

> -Jason

--
Zheng Li
http://www.pps.jussieu.fr/~li

Jason Ganetsky

unread,
Apr 22, 2007, 1:54:26 PM4/22/07
to caml...@yquem.inria.fr
On 4/22/07, Jason Ganetsky <jason.g...@gmail.com> wrote:
>
> Well, the solution I'm going for now is to load all my data up, call
> Gc.Compact(), and then fork off child processes. The workload that I'm
> parallelizing is read-only... so I think this will work well with Linux
> copy-on-write forking.
> > http://www.pps.jussieu.fr/~li <http://www.pps.jussieu.fr/%7Eli>

Richard Jones

unread,
Apr 23, 2007, 4:11:28 AM4/23/07
to Jason Ganetsky
On 4/22/07, Jason Ganetsky <jason.g...@gmail.com> wrote:
>
>Well, the solution I'm going for now is to load all my data up, call
>Gc.Compact(), and then fork off child processes. The workload that I'm
>parallelizing is read-only... so I think this will work well with Linux
>copy-on-write forking.

You might also want to look at the Ancient library
(http://merjis.com/developers/ancient) which will allow you to share
data read-only between unrelated processes, backed by a file.

Rich.

--
Richard Jones
Red Hat

_______________________________________________

0 new messages