(*^
::[ Information =
"This is a Mathematica Notebook file. It contains ASCII text, and can be
transferred by email, ftp, or other text-file transfer utility. It should
be read or edited using a copy of Mathematica or MathReader. If you
received this as email, use your mail application or copy/paste to save
everything from the line containing (*^ down to the line containing ^*)
into a plain text file. On some systems you may have to give the file a
name ending with ".ma" to allow Mathematica to recognize it as a Notebook.
The line below identifies what version of Mathematica created this file,
but it can be opened using any other version as well.";
FrontEndVersion = "Macintosh Mathematica Notebook Front End Version 2.2";
MacintoshStandardFontEncoding;
fontset = title, inactive, noPageBreakBelow, nohscroll, preserveAspect,
groupLikeTitle, center, M7, bold, e8, 24, "Times";
fontset = subtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect,
groupLikeTitle, center, M7, bold, e6, 18, "Times";
fontset = subsubtitle, inactive, noPageBreakBelow, nohscroll,
preserveAspect, groupLikeTitle, center, M7, italic, e6, 14, "Times";
fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect,
groupLikeSection, grayBox, M22, bold, a20, 18, "Times";
fontset = subsection, inactive, noPageBreakBelow, nohscroll,
preserveAspect, groupLikeSection, blackBox, M19, bold, a15, 14, "Times";
fontset = subsubsection, inactive, noPageBreakBelow, nohscroll,
preserveAspect, groupLikeSection, whiteBox, M18, bold, a12, 12, "Times";
fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,
12, "Times";
fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 10, "Times";
fontset = input, noPageBreakInGroup, preserveAspect, groupLikeInput, M42,
N23, bold, B65535, L-5, 12, "Courier";
fontset = output, output, inactive, noPageBreakInGroup, preserveAspect,
groupLikeOutput, M42, N23, L-5, 12, "Courier";
fontset = message, inactive, noPageBreakInGroup, preserveAspect,
groupLikeOutput, M42, N23, R65535, L-5, 12, "Courier";
fontset = print, inactive, noPageBreakInGroup, preserveAspect,
groupLikeOutput, M42, N23, L-5, 12, "Courier";
fontset = info, inactive, noPageBreakInGroup, preserveAspect,
groupLikeOutput, M42, N23, B65535, L-5, 12, "Courier";
fontset = postscript, PostScript, formatAsPostScript, output, inactive,
noPageBreakInGroup, preserveAspect, groupLikeGraphics, M7, l34, w282, h287,
12, "Courier";
fontset = name, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,
italic, 10, "Geneva";
fontset = header, inactive, noKeepOnOnePage, preserveAspect, M7, 12, "Times";
fontset = leftheader, inactive, L2, 12, "Times";
fontset = footer, inactive, noKeepOnOnePage, preserveAspect, center, M7,
12, "Times";
fontset = leftfooter, inactive, L2, 12, "Times";
fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,
10, "Times";
fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = completions, inactive, nohscroll, noKeepOnOnePage,
preserveAspect, M7, 12, "Times";
fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
currentKernel;
]
:[font = smalltext; inactive; preserveAspect]
Copyright (C) 1997 Rich Neidinger, John Swallow, and Todd Will. Free for
distribution to college and university instructors for personal,
non-commercial use. If these notebooks are used in a course, the authors
request $20 per student.
:[font = title; inactive; preserveAspect]
22. Compilation
:[font = smalltext; inactive; preserveAspect]
Last revision: November 14 1996
:[font = text; inactive; preserveAspect]
Here we explore the notion of compilation, focussing on demonstrating the
advantage in speed and the limitation of using atomic data types to
represent machine-precision numbers.
:[font = section; inactive; Cclosed; preserveAspect; startGroup]
Compilation
:[font = text; inactive; preserveAspect]
Recall that compiled programs have one overarching advantage: the great
reduction in execution time. Recall also that using machine-size integers
and approximations also affords the user a great reduction in execution
time. For these reason, practically every computer application which you
execute is using a compiled, rather than interpreted, program, one which
uses machine-size integers and approximations whenever possible. For
instance, Mathematica itself is such a program. We have not yet been able
to write our own compiled "programs" requiring machine-size numbers because
Mathematica's standard mode is to interpret all commands
statement-by-statement and to evaluate all expressions with as much
accuracy as possible. We can explore compilation and the use of
machine-size numbers, however, by having Mathematica compile particular
functions.
:[font = text; inactive; preserveAspect]
Mathematica permits the compilation of Mathematica code using the "Compile[
]" function. The "Compile[ ]" function takes a sequence of Mathematica
expressions and converts them to a language very similar to machine
language, a language which the computer can execute much faster, thereby
mimicing actual compilation. In order to use the "Compile[ ]" function,
however, we must decide what the data types of the parameters are, and we
are limited to "_Integer" (machine-size integer), "_Real"
(machine-precision approximate real number), and "True|False" (a boolean
value). [Complex numbers are also permitted.] Compiled functions, because
they use the internal language of computers, are not permitted to take
advantage of Mathematica's predefined structured data types such as lists
and patterns. If we insist on using such structures, some intermediary
must transform the structures back to these machine data types, by breaking
a list into its component pieces, breaking huge integers into smaller
machine-size pieces, and so on. In a compiled setting, we would have to
decide ourselves how to represent lists as sequences of machine-size
integers, how to represent a string as a sequence of characters, and so
forth, and such decisions form the basis for a course in data structures
and algorithms.
:[font = text; inactive; preserveAspect]
The syntax for the basic "Compile[ ]" command is "Compile[list,expr]",
where "list" is either a list of single names, which represent parameters
of data type "_Real", or a list of pairs consisting of a name and a data
type selected from the ones above, and where "expr" is some Mathematica
expression. For example, suppose we'd like a function to compute "x^y",
where "x" and "y" are assumed to be names of type Real:
:[font = input; preserveAspect]
myPower = Compile[{x,y},x^y]
:[font = text; inactive; preserveAspect]
We could have specified that "x" be considered of type Real and "y" a
machine-size integer by writing instead
:[font = input; preserveAspect]
myPowInt = Compile[{{x,_Real},{y,_Integer}},x^y]
:[font = text; inactive; preserveAspect]
It is important that we use the "Set" operator "=" and not the "SetDelayed"
operator ":=", for the latter will perform the compilation only after the
function is called, not before, and in what follows we will want to time
the function without counting the time of compilation.
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Comparing Speed with myPower
:[font = text; inactive; preserveAspect]
Here we submit compiled functions and standard Mathematica interpreted
functions to a head-to-head test. The operations we include in the
functions are standard arithmetic functions, which are the real strength of
compiled programs, instead of "Plot[ ]" calls or complicated list-based
functions. Our first attempt is to define a function "myPower" which is a
compiled version of "myPower2", both of which take two arguments, presumed
Real, and raise the first argument to the second. We then compare their
timings when the arguments are "2.0" and "3.0".
:[font = input; preserveAspect]
Clear[myPower,myPower2]
myPower = Compile[{x,y},x^y]
myPower2[x_,y_] := x^y
:[font = input; preserveAspect]
Timing[myPower[2.0,3.0]]
Timing[myPower2[2.0,3.0]]
:[font = text; inactive; preserveAspect]
Execute the above cell a couple of times to insure that you are not getting
exceptional timings; the "Timing[ ]" function can sometimes give answers
which are too small or negative, because the actual time to execute the
function is too small to measure. After executing the functions several
times, however, you may have become convinced that it is difficult to tell
the difference between functions which execute so quickly. In order to get
a better idea, we'll have each of the functions executed 10000 times, using
the Mathematica command "Do[ ]".
:[font = input; preserveAspect]
Timing[Do[myPower[2.0,3.0],{10000}]]
Timing[Do[myPower2[2.0,3.0],{10000}]]
:[font = text; inactive; preserveAspect]
Now the difference should be clear. The compiled version executes more
than twice as quickly as the interpreted version. Now notice that we're
using an interpreted "Do[ ]" command to call the compiled function. How
would the timing speed up if we compiled the "Do[ ]" inside the function?
:[font = input; preserveAspect]
Clear[myPower3]
myPower3 = Compile[{x,y},Do[x^y,{10000}]]
Timing[myPower3[2.0,3.0]]
:[font = text; inactive; preserveAspect; endGroup]
The conclusion: compiling the "Power" operator "^" and the "Do[ ]" command
both speed up the functions by a remarkable amount.
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Data Type Checking in Compiled Functions
:[font = text; inactive; preserveAspect]
What if you give a compiled function values for the parameters which do not
match the required data types? Unlike most languages, Mathematica prevents
us from encountering a serious error in this situation. First, it tries to
convert the values to the desired values, for instance by converting
integers to approximations. Second, if it cannot convert, or reaches an
error in the calculation using the compiled code, Mathematica simply
reverts to evaluating the original expression---in its standard interpreted
mode. One consequence is that is never hurts to compile a function in
Mathematica, since if the data type values don't match, Mathematica simply
executes the function in its normal fashion anyway. Let's look at some
associated timings.
:[font = input; preserveAspect]
Clear[myPower,myPower2]
myPower = Compile[{x,y},Do[x^y,{10000}];x^y]
myPower2[x_,y_] := (Do[x^y,{10000}];x^y)
:[font = text; inactive; preserveAspect]
Note that the two functions now have a return value, namely, the value of "x^y".
:[font = input; preserveAspect]
Timing[myPower[2.0,3.0]]
Timing[myPower2[2.0,3.0]]
:[font = text; inactive; preserveAspect]
Here the timings are as we would expect, and the return values are "8.".
:[font = input; preserveAspect]
Timing[myPower[2,3]]
Timing[myPower2[2,3]]
:[font = text; inactive; preserveAspect]
In the timings above, we see that the timings are still in favor of the
compiled version, indicating that the compiled version was executed, but
we also note that "2" and "3" were converted to Real data (the default for
parameters of compiled functions), since the result from the compiled
version is "8.", not "8".
:[font = text; inactive; preserveAspect]
Finally, we'll give Mathematica some numbers which Mathematica can't handle
in machine-precision: we'll ask for 10^1000, which is outside the range of
machine-precision real numbers, and, for that matter, machine-size
integers, too.
:[font = input; preserveAspect]
Timing[myPower[10,1000]]
Timing[myPower2[10,1000]]
:[font = text; inactive; preserveAspect]
Note that the compiled version actually took longer, because the compiled
code was attempted first, resulting in an error, and the Mathematica
interpreted code was then used.
:[font = text; inactive; preserveAspect]
It is interesting to note that many Mathematica functions actually compile
a function they are given before they use the function over and over again.
One example is "Plot[ ]"; instead of plotting 1000 points, say, and having
to interpret the function 1000 times, Mathematica compiles it first. To
see the difference, we can explicitly instruct "Plot[ ]" not to compile.
Here are two examples.
:[font = input; preserveAspect]
Timing[Plot[Sin[x],{x,0,1},PlotPoints->1000,
Compiled->False]]
:[font = input; preserveAspect]
Timing[Plot[Sin[x],{x,0,1},PlotPoints->1000]]
:[font = text; inactive; preserveAspect; endGroup]
(We use 1000 points in the plots in order to witness a significant
difference in the timings.)
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Taking a Look at Compiled Code
:[font = text; inactive; preserveAspect]
To gain a better idea of just what "machine language" looks like, we'll ask
Mathematica to produce the compiled code for a compiled function. We need
only use the "?" command.
:[font = input; preserveAspect]
Clear[myPower]
myPower = Compile[{x,y},Do[x^y,{10000}];x^y];
?myPower
:[font = text; inactive; preserveAspect; endGroup]
As we see, the internal format of a compiled function is a list which
contains, first, a list of the assumed data types, then a list of
"registers" that will be used in the code, followed by a list of computer
"instructions" given in a certain code, and finally the Mathematica
function that will be executed if the compiled code fails or is
inappropriate for the data. One could decipher the compiled code, using
information in Wolfram Research's technical reports on compiled code, but
our interest here is to understand the idea of machine language without
understanding how to program in machine language.
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Inline Functions
:[font = text; inactive; preserveAspect]
Recall that we learned that inline functions were faster than functions
called under standard function calls. Let's take a look at the
time/length-of-code tradeoff. We confess at the outset that anonymous
functions, which share syntactical similarities with inline functions as
used in other languages, actually take longer in Mathematica. Instead,
we'll implement a true analogue of inline functions, by expanding out the
function calls ourselves.
:[font = text; inactive; preserveAspect]
In practice, remember, inline functions are implemented by eliminating the
standard function call; the code for the inline function is written
directly into the compiled code each time the function is referenced. A
standard function call implementation, on the other hand, requires storing
parameters, jumping to a different section of compiled code where the
function code is stored, and finally copying a return value back to the
original place--but only one copy of the compiled code for the function
needs to be stored.
:[font = text; inactive; preserveAspect]
We implement a compiled version of a standard function as follows.
:[font = input; preserveAspect]
Clear[myPower,myPower2]
myPower = Compile[{x,y},x^y]
myPower2 = Compile[{x,y},Do[myPower[x,y],{100}]]
:[font = text; inactive; preserveAspect]
Notice that "myPower" is compiled and placed in memory somewhere, while
"myPower2" references "myPower" 100 times, thereby executing a function
call 100 times.
:[font = text; inactive; preserveAspect]
We implement an inline version of the function by simply repeating "x^y"
100 times, thereby eliminating the function call at all, instead inserting
whatever compiled code is necessary 100 times.
:[font = input; preserveAspect]
Clear[myPower3]
myPower3 = Compile[{x,y},
x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;
x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;
x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;
x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;
x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;
x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;
x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;
x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;
x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;
x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;x^y;]
:[font = text; inactive; preserveAspect]
Now we do a head-to-head test.
:[font = input; preserveAspect]
Timing[myPower2[2.,3.]]
:[font = input; preserveAspect]
Timing[myPower3[2.,3.]]
:[font = text; inactive; preserveAspect]
To make the timing more accurate, let's execute each 100 times.
:[font = input; preserveAspect]
Timing[Do[myPower2[2.,3.],{100}]]
:[font = input; preserveAspect]
Timing[Do[myPower3[2.,3.],{100}]]
:[font = text; inactive; preserveAspect]
A fairly dramatic difference: function calls take time! Now let's look at
the length of the compiled code. For the standard function call, we have
two functions:
:[font = input; preserveAspect]
?myPower
:[font = input; preserveAspect]
?myPower2
:[font = text; inactive; preserveAspect]
For the inline function implementation, we have only one (long) function:
:[font = input; preserveAspect; endGroup]
?myPower3
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Notes
:[font = text; inactive; preserveAspect]
It is also possible to require that, during compilation of an expressions,
certain quantities have certain data types, using a third argument to the
"Compile[ ]" command; see "?Compile".
:[font = text; inactive; preserveAspect; endGroup; endGroup]
The reason that anonymous functions take just as long, or longer, is that
Mathematica actually uses them to define a function, complete with
necessary function calls, instead of copying the expression directly, as an
inline function implementation would do.
^*)