to_the_crux
to_the_crux

Reputation: 267

Can an OCaml program use more than one processor core?

Isn't it important to be able to do that in order to attain maximal speed?

Edit:

Clojure, for example, has pmap, which uses more than one core.

Dr. Harrop wrote (Jan. 9, 2011):
The new features being added to the language, such as first-class modules in OCaml 3.12, are nowhere near as valuable as multicore capability would have been.

Upvotes: 4

Views: 751

Answers (2)

J D
J D

Reputation: 48697

If your parallel code generates a large amount of data then there is no easy way to get it back efficiently in OCaml. The traditional workaround is to fork processes and marshal results back to the parent process but your parent process then deserializes all of the data on a single core, reallocating everything on its own heap. That is very inefficient on a multicore and means that OCaml cannot express efficient implementations of most parallel algorithms including pmap.

Upvotes: 0

gasche
gasche

Reputation: 31459

Yes it can; for this, you should use a multi-processing model, where you program spawns multiple processes to do computation independently and then merge results.

The simplest way to do is to use the Unix.fork system call to fork your program into two processes. This is described for example in the online book Unix system programming in OCaml. If the computation you want to split across cores has a simple structure (iteration, mapping over a pool of inputs), Parmap is a library that will let you benefit from parallelism rather easily, just by changing some function calls in your application (if it is well structured already). If you want to do more sophisticated things (direct access to shared memory structures, message boxes...), the Ocaml-net project supports lot of convenient features through the Netmulticore library.

If you want to do distributed programming (programs that run on a cluster of several machines), the OcamlMPI library provides support for the well-known distributed message passing framework MPI. There is also the more experimental and high-level JoCaml extension, that uses an interesting, more researchy approach to concurrent communication.

Note that if you have no specific performance constraints, or if you application is inherently sequential, it makes no sense to bother to try to parallelize some computation (at the cost of the higher book-keeping overhead of synchronization), in the latter case because of Amdahl's law.

Upvotes: 6

Related Questions