On Large Simulations in Matlab

There comes a time when you need to run a huge simulation (taking maybe days, but lets hope overnight) to finish. This post will talk about 3 main points, just briefly though. Will write in detail about one of them (MEX) sometime later.

Firstly, you should always know how much of the simulation is complete. Most of the simulations tend to have a general pattern like there is some initialization code, some section of iterated code (in a for / while loop) and then some ending (generally plots, or error computations). The middle section is which takes the most amount of time. Matlab gives something called a waitbar which basically is an cute visualization of how much of the process is complete. Its extremely easy to use in the basic form. Just initialize with the bar being empty,
wbarh = waitbar(0,'text_comes_here','Name','window_name');
and then fill up as
waitbar(itercnt/total_iterations, wbarh, 'updated_text_if_any');
Note that wbarh is the handle to this window that pops up. Matlab allows you to use sprintf(' '); and to put changing variables in the text to be inserted. Although at this point, its important to note that this WILL slow down the entire simulation by some time. So it may be wise not to update the bar at every iteration, but at every 10th or 100th or so. The not so beautiful way to do this would be just to print the iteration count to the command window! Again, every 100th or so.

In general, such simulations involve calling functions in the iterated middle section. Its a nice idea to have an estimate of which function is called most, and which function is eating all your time. The tool to do this is called the Profiler. It can be called up from the Matlab Editor -> Tools -> Open Profiler. Start profiling for a short number of iterations (since this is like a test run) and once it finishes, you will understand the rest. Its pretty self-explanatory! 🙂 There is a command-line way to do this, but the previous way is much more easier.

Now you have caught the culprit function using the previous method, which is probably called billions of times, and thus takes up most of your computation time. Solution – Convert it to a MEX. This is a convenient extremely fast binary file compatible with Matlab, generated by writing C like code and using the “mex.h” header file. More on MEX files in other posts to come. The speed-up is specially significant if your culprit function had for / while loops and you will appreciate it. If your code still takes multiple days to complete, you have only one option – to somehow obtain more processing power, and combine them using MATLAB’s Distributed Computing Service / Engine. 🙂


3 thoughts on “On Large Simulations in Matlab

    1. Makarand Tapaswi Post author

      A few more ideas
      1. Generate a random number and display when that number is less than 0.1. So 10% of the time. Advantage – The numbers you display wont all be divisible by 100. They will “look” real! Disadvantage – generating the random number
      2. fprintf('\b\b\b\b\b\b\b%03d/%03d') Advantage-same line refresh, no graphics needed. Disadvantage – having to write many many \b and counting that manually.

      Thanks for the info!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s