In my last post I discussed how to profile your MATLAB scripts and how to improve their performance by taking advantage of simple techniques like pre-allocating arrays instead of growing them inside a processing loop or using its vector processing power instead of loops to perform operations on data arrays. In this post, I’ll discuss optimizations related to memory handling.
Computers get better and better at processing data so the performance of a computer is not always the main concern; rather, as the amount of data requiring processing becomes larger and larger (such as with MIMO applications), memory increasingly becomes a source of potential problems.
At Nutaq, we are in the process of automating the testing of our ADP Software Tools. Part of this automation process is the analysis of the data resulting from the execution of the tests (for example, data recorded using a MI125 FMC with the software tools’ Record/Playback example). When our automated test engine analyses the recorded data, it is pretty common for it to work on chunks of up to 1 GB to verify that the recording and uploading software is working properly and not missing any samples. When performing the analysis on large amounts of data, we found that if we did not pay attention to how we handled the memory usage of our scripts, nights of data analysis could easily be lost because MATLAB would end up in no man’s land.
Here’s three guidelines to improve the memory handling of your scripts.
1) Use in-place processing whenever possible
To avoid allocating memory buffers that are not absolutely needed, try to reuse them as much as possible and perform in-place processing to limit the memory footprint of your script.
Figure 1: Creation of a sine wave using two separate buffers
Figure 2: Creation of a sine wave using in-place processing
2) Clear the arrays you no longer need as soon as possible
In-place processing is a great way to reduce the memory usage of your scripts, but sometimes it is more useful to create new buffers. For example, most people want to maintain script readability by using buffers with meaningful names for each step of the execution. It is possible to do so without growing the amount of memory used by your script by de-allocating a buffer as soon as it becomes obsolete for your processing.
Figure 3: Creation of a sine wave using two separate buffers
Figure 4: De-allocation of a buffer when it becomes obsolete
3) When precision is not critical, use single instead of double data types
By default, MATLAB uses double-precision data when a new variable is created. This data type uses 8 bytes to store each sample. If the precision needed by the algorithm doesn’t require the amount of precision granted by double data types, using single-precision data should be considered. Single-precision data only use 4 bytes of memory per sample, half the amount of memory required by double-precision data. To create a single-precision variable, declare it as single(variableName). Just like when writing C code, always use the smallest possible data format depending on the precision you need to minimize the memory footprint of your algorithm.
Figure 5: Use of double-precision data
Figure 6: Use of single-precision data
The use of single-precision instead of double-precision data cuts the amount of memory used by half but comes at the price of less precision in the calculations. Figure 7 shows the precision discrepancy between the same signal generated using both double-precision and single-precision.
Figure 7: Difference in precision between double and single data types
If you want more information about how to optimize your MATLAB code, visit the following web pages I referred to when I optimized my own scripts:
I hope the information contained in these last two blog posts will come in handy to those working with MATLAB. If you have your own tips, please let everyone know by posting them in the discussion area below!