Sep 3, 2012

Faster Mathematica I: script and text based interface

I am not a Mathematica developer, expert or advanced user. Yet I do have been using Mathematica frequently for several years. And I do know there are often slower and faster ways of using Mathematica. In these posts, I'll present my experience of Mathematica, which may or may not be the best/smart ways. I welcome the dear readers posting their experience/comments.

I have several Mathematica available, i386, x86_64 desktop and network licenced math on x86_64 supercomputer. By default, my Mathematica is running on Ubuntu/Linux i386Mathematica for students version 8.0. Throughout these posts, I'm gonna use math for short of Mathematica.

1. Use MathKernel

The FrontEnd (in most cases, the notebook) can be very slow when there are lots stuff in it. Output data, graphs and syntax highlight may take huge amount of memory, which slows down math. In other times, the user uses math on a remote machine. The bandwidth may limit the use of FrontEnd. In either case, the user may want to interact with math kernel directly. math/MathKernel can be viewed as a text based interface of math, or an interactive version of script.

To start MathKernel, in any terminal enter math or MathKernel. To quit from math kernel, one has to use math function Exit[ ] or Quit[ ]. It also works without square bracket like Exit or Quit. Warning, enter mathematica will open a FrontEnd.

use MathKernel
2. Use math script

When working on heavy tasks, or with a queue job submission system, non-interactive scripts are often very useful. Conventionally, Mathematica packages and other Mathematica scripts end with the extension ".m", although this is not a strict requirement.

To run a math script foo.m,
math -script foo.m
MathKernel -script foo.m
math -noprompt -run "<<foo.m"

Math scripts can also be wrapped as normal executive scripts.

This is done by invoking MathematicaScript interpreter at the very beginning. The rest of the file is Mathematica commands. The interpreter line starts with two characters “#!”, following with the absolute path to the MathematicaScript interpreter. A typical script file looks like:

There is a known bug (not fixed at least in 8.0) that the MathematicaScript interpreter malfunctions in 32-bit machines.

The issue can be partially solved by using math or MathKernel as interpreter. But math or MathKernel does NOT take command line parameters (See example below).


No comments:

Post a Comment