Profiling MATLAB code on Linux

March 6th, 2009 | Categories: math software, matlab, programming | Tags:

I needed to profile some MATLAB code recently to try and optimise it.  It’s been a while since I have done this sort of thing so I first reminded myself of the details by reading the documentation.  Eventually I came to a sage piece of advice concerning multi-core processors…

Note If your system uses Intel multi-core chips, you may want to restrict the active number of CPUs to 1 for the most accurate and efficient profiling. See Intel Multi-Core Processors — Setting for Most Accurate Profiling for details on how to do this.

‘Sounds fair enough‘ I thought so I clicked on the link and started reading the instructions.  The first couple of steps were

1. Open Windows Task Manager.

2. On the Processes tab, right-click MATLAB.exe and then click Set Affinity.

Outrage!  How dare they assume I am using Windows ;)  A bit of searching through the documentation revealed that, sure enough, The Mathworks had forgotten about us Linux users in this particular instance.  Mac users are left in the cold too for that matter.

So, I fired off a quick email to Mathworks support and asked them how I should proceed with setting the processor affinity as a Linux user.  I got a reply within just a couple of hours.  Here it is in full (edited very slightly by me) for anyone else who needs to do this:

The Linux  taskset command (part of a scheduler utilities package) can be used to set the processor affinity for a certain Process ID (PID). To find MATLAB’s PID, you can execute


from a Linux terminal. Then, to set the processor affinity for MATLAB to one CPU (e.g. CPU #0), execute (also from a Linux terminal):

taskset -pc 0 PIDhere

The ‘-p’ option specifies that taskset should operate on an existing PID instead of creating a new task. The ‘-c’ option allows you to specify a list of processor numbers instead of a bitmask representing the processors you would like to use. For more information on the syntax of taskset, it is best to look at the manpage for this command (i.e. by executing man taskset from a Linux terminal).

The support analyst (who wishes to remain anonymous) who was helping me out then informed me that he had raised a request for enhancement with the Mathworks documentation team to try and get this information covered in future versions of the MATLAB docs.

I’m impressed with this level of service so thank you to everyone involved at the Mathworks for solving this little issue of mine.

  1. Sunil
    October 22nd, 2009 at 09:41
    Reply | Quote | #1

    Hello Mike,

    Thanks for the info.. Its really strange they dont have any proper documentation for Linux usage..