COMPILED FUNCTION TOOLS TUTORIAL

Introduction

The Mathematica compiler provides an important way both to speed up and also to work with Mathematica computations. It does this by taking assumptions about the computations and rewriting them in more efficient ways. These assumptions limit the full generality of Mathematica but are chosen to enhance important classes of computations. For example, computations involving machine-precision arithmetic are enhanced.

In addition to providing speed enhancements for Mathematica, the compiler is also useful since it can analyze and process Mathematica computations. This makes it the basis for useful features such as generating external code.

This tutorial will cover details about how the compiler works. It should help you to use it more effectively, for example, showing how to improve the performance of your computations.

Basics

A key way to use the Mathematica compiler is with the function Compile.

In[12]:=
Click for copyable input
Out[12]=

You can use the compiled function as any other Mathematica function, passing it real number input.

In[13]:=
Click for copyable input
Out[13]=

However, if you pass the compiled function an input that is not a real number, a message is issued. Mathematica still returns a result, but this has not used the compiler.

In[14]:=
Click for copyable input
Out[14]=

Comparison with Function

You can compare a compiled function with Function. They are created with a similar command structure.

In[6]:=
Click for copyable input

For machine inputs, they return the same result.

In[8]:=
Click for copyable input
Out[8]=
In[9]:=
Click for copyable input
Out[9]=

It is faster to use CompiledFunction.

In[17]:=
Click for copyable input
Out[17]=
In[18]:=
Click for copyable input
Out[18]=

If the input is not a machine input, the compiled function issues a message before returning the result.

In[11]:=
Click for copyable input
Out[11]=
In[12]:=
Click for copyable input
Out[12]=

Compiled Function Internals

In general, Compile creates a CompiledFunction expression that contains a sequence of simple instructions for evaluating the compiled function. The instructions are chosen to be those that can be executed very efficiently by modern computer hardware and software libraries. The contents of the compiled function can be displayed by the input form.

In[19]:=
Click for copyable input
Out[20]//InputForm=

You can get a view into how the compiled function executes with the package. This is a key part of the Mathematica code generation system, but it can also be useful to show you what the compiled function does.

To use the package you have to load it.

In[21]:=
Click for copyable input

CompilePrint will show details of the compiled function.

In[22]:=
Click for copyable input
Out[22]=

Calling the Compiler

The Mathematica compiler is called whenever a CompiledFunction executes. These are created by calls to the Compile function. In addition, a number of functions will attempt to use the compiler as part of their operation.

One important class includes many numerical computation functions, such as NDSolve, FindRoot, and FindMinimum. Typically, these have a Compiled option that can be used to enable or disable use of the compiler. The default operation of these functions is make some analysis of the initial settings and the problem and use the compiler if these indicate it would be beneficial.

The following calls NDSolve but does it without using the compiler.

In[28]:=
Click for copyable input
Out[28]=

Another class of uses of the compiler is from programming functions such as Table or Map. These analyze the input and use the compiler if this can be done without changing the result and if the time taken to create the compiled function does not cost too much.

In the following, the compiler is used. You can detect that the compiler was used because the result is a packed array.

In[64]:=
Click for copyable input
Out[64]=

Benefits of the Compiler

This section summarizes some of the benefits of the compiler.

Faster Computation

A major benefit of the compiler is that it allows certain computations to be faster.

The following creates two expressions, using CompiledFunction and Function.

In[1]:=
Click for copyable input

It is faster to use CompiledFunction.

In[17]:=
Click for copyable input
Out[17]=
In[18]:=
Click for copyable input
Out[18]=

CompiledFunction computations can be made faster still by using the code generator and by parallel computation.

Code Generation

The compiler works by creating a sequence of simple instructions for evaluating the compiled function. Mathematica contains a virtual machine that can process these instructions. However, it can also use these instructions to create entire functions and programs written in other languages.

The following shows how to use ExportString to convert a CompiledFunction into a C code fragment.

In[6]:=
Click for copyable input
Out[8]=

Parallel Computation

Modern CPU architectures commonly provide multiple cores and these can be used by the compiler to execute a compiled function in parallel. The computations all run efficiently in multiple threads of execution, which is possible because of the simple instructions that the compiler generates.

The following demonstrates a compiled function running in parallel.

In[6]:=
Click for copyable input
Out[8]=

This shows the operation running sequentially; it takes twice as long.

In[9]:=
Click for copyable input
Out[10]=

Typically, $ProcessorCount is used to determine how many threads to be used.

In[11]:=
Click for copyable input
Out[11]=

For this computer $ProcessorCount is 2 and this is why the parallel computation was twice the speed of the sequential computation.

New to Mathematica? Find your learning path »
Have a question? Ask support »