Undocumented Matlab. A few months ago, I discussed several tips regarding Matlab’s parfor command, which is used by the Parallel Computing Toolbox (PCT) for parallelizing loops.
- Step through the process of creating a simple app using GUIDE.
- MATLAB developer Loren Shure shows you how to turn your ideas into MATLAB code — Loren on the Art of MATLAB in MATLAB Central Blogs.
- Back to top MATLAB math programming software package written by MathWorks.
Today I wish to extend that post with some unexplained oddities when using parfor, compared to a standard for loop. Data serialization quirks. Dimitri Shvorob may not appear at first glance to be a prolific contributor on Matlab Central, but from the little he has posted over the years I regard him to be a Matlab power- user. So when Dimitri reports something, I take it seriously.
Such was the case several months ago, when he contacted me regarding very odd behavior that he saw in his code: the for loop worked well, but the parfor version returned different (incorrect) results. Eventually, Dimitry traced the problem to something originally reported by Dan Austin on his Fluffy Nuke It blog. The core issue is that if we have a class object that is used within a for loop, Matlab can access the object directly in memory.
But with a parfor loop, the object needs to be serialized in order to be sent over to the parallel workers, and deserialized within each worker. If this serialization/deserialization process involves internal class methods, the workers might see a different version of the class object than the one seen in the serial for loop. This could happen, for example, if the serialization/deserialization method croaks on an error, or depends on some dynamic (or random) conditions to create data. In other words, when we use data objects in a parfor loop, the data object is not necessarily sent “as- is”: additional processing may be involved under the hood that modify the data in a way that may be invisible to the user (or the loop code), resulting in different processing results of the parallel (parfor) vs.
To reduce the memory footprint (and hopefully also improve performance), I decided to work with data of type single instead of Matlab’s default double. Furthermore, in order to speed up the calculation I use parfor rather than for in the main calculation. In the end of the run I am running a mini for- loop to see the best results. What I discovered to my surprise is that the results from the parfor and for loop variants is not the same! Continue reading.
Historically, Matlab was supposed to be a numerical plate- form to quickly put together new ideas for computation. So the GUI is a nice bonus, but not its primary focus. To get inspiration on how to push Matlab capabilities, I thought I would look into a domain where the GUI is the primary focus : GAMING. Surprisingly, there are some decent games in Matlab. Don’t get me wrong.
Nothing compared to Call of Duty, but at least a very good source of inspiration to do real- time processing and beautiful usage of Matlab figure capabitilites. Keep in mind that no professional programmer in Gaming has seriously considered doing this. So these games are done by passionate and dedicated folks. Let me show some of these games. If you found one that should be in this list, let me know. Tetris is a THE classic in gaming.
And apparently you can do a pretty decent version of Tetris in Matlab. There are several ones on Matlab file exchange. This is one that I tested and it works great! This program works by using a timer object to update the display.
It captures your keyboard to modify each frame. It uses patch objects to create blocks and move them around.
MATLAB Cookbook -- teaching MATLAB by example. The variable IDX tells you the group to which k-means has assigned each data point. Matlab Plot - stem In this example, we study the ' stem ' instruction to plot Matlab functions. It draws vertical lines (with a little circle at the tip) proportional.
The code is very clear and organized. Stellaria is a shooting game implemented in Matlab. It looks pretty decent if you compare it to a game from the 8. I must confess I was not impressed by the code itself. Lots of global variables.
It is a little bit hard to read but it works. It works with a WHILE loop that goes as fast as it can and updates the frames as the computation progresses.
Some pretty nice effects happening in the figure window, compared to what you usually see in graphs. Risk is a classic board game. Here we have a pretty nice usage of all standard GUI controls to create an environment a little bit more appealing than the usual. I am still looking for Games with a very polished interface.
Let me know if you know one.