Scientific computing with OCaml

OCaml is suitable for prototyping scientific code and quickly turning the result into production-quality software that cannot crash.

While C++ is still figuring out closures, type inference and lacks a static type system for higher-level programming (templates are not typed!) the ML family of languages had all of that already figured out twenty years ago and OCaml remains a few quantum leaps ahead of C++ .

Unlike Haskell, OCaml uses an eager evaluation model like most programming languages, which makes its operation simple to understand and its performance predictable, while allowing side-effects to be inserted in existing code for debugging, interaction with the real world or imperative programming.

Scripting and scientific computing languages such as Python, MATLAB, Mathematica do not have sufficient speed to define algorithms that process large volumes of data when they cannot be expressed as combinations of library functions operating on large chunks.

They also do not have the robustness and abstraction facilities required for software engineering beyond the micro-scale.

Interfacing Fortran with OCaml

While OCaml is still one of the best overall programming languages, it is syntactically slightly inconvenient and gets a bit slow for tight numerical routines, especially if they are written in a functional style. Written in imperative style, compiled Ocaml numerical code usually is 2-3 times slower than single-core C,C or Fortran, and therefore usable and much faster than Python or Cython, but optimized, CPU-tuned Fortran can be faster and uses less memory. Fortran, with its native support for arrays of any reasonable rank, built-in bounds checking, vectorized operations and rich intrinsics remains the best programming language for such code. But it remains cumbersome as a general-purpose or systems programming language.

Using CTypes it is possible to easily call Fortran routines from OCaml, provided a few compilation and linking tricks described here are known. This combination of OCaml and Fortran provides, in my opinion, the best small-to-medium scale platform for production-quality scientific computing.

2017-05-13