Can OCaml use more than one processor core?

Isn't it important to do this for maximum speed?

Edit:

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

R. Harrop wrote (Jan 9, 2011):
New features being added to the language, such as the top-notch modules in OCaml 3.12, are nowhere near as valuable as multi-cores might have been.

+3


source to share


2 answers


Yes it is possible; to do this, you should use a multiprocessing model, in which you run multiple processes to compute the computation yourself and then combine the results.

The easiest way is to use a system call Unix.fork

to fork your program into two processes. This is described, for example, in the online book Unix System Programming at OCaml . If the computation you want to split across cores has a simple structure (iteration, mapping by input pool), Parmap is a library that will allow you to easily benefit from parallelism simply by changing some function calls in your application (if it is already well structured ). If you want to do more complex things (direct access to shared memory structures, message boxes ...), the Ocaml-net project supports many convenient features through Netmulticore .



If you want to do distributed programming (programs running on a cluster of multiple machines), the OcamlMPI library provides support for the well-known MPI Distributed Transfer Protocol. There is also a more experimental and high-level JoCaml extension that takes an interesting, more exploratory approach to parallel communication.

Note that unless you have specific performance limits, or if the application is inherently sequential, there is no point in trying to parallelize some computation (at the expense of higher storage costs in sync accounting), in the latter case due to Amdahl's Law .

+6


source


If your parallel code is generating a lot of data, then there is no easy way to put it back in OCaml. The traditional workaround is for fork processes and marshals to fall back to the parent process, but your parent process then deserializes all data in one core, reallocating everything on its own heap. This is highly inefficient for multicore and means that OCaml cannot express efficient implementations of most parallel algorithms, including pmap

.



0


source







All Articles