Writing PNGs from C++

hexagonsTo continue the series on using C++ to replace MATLAB, here are some details about using the pngwriter library. Included below is a function that I use in various places to write a 2D array to a png file. This can be left in a header somewhere and used in a similar way to MATLAB’s imagesc() function.

I’d be happy to post more details if anyone is interested, and I’ll try to keep up with posts about the process I followed to port from MATLAB to C++.

Continue reading

Advertisements

MATLAB → C++ part II

Matlab

I have had some success with converting my MATLAB code to C++. There are some caveats, from array indexing (minor), to missing functions (less trivial to fix). In case people are looking for solutions to these problems, I’ve posted some sample code.

To motivate the conversion a little bit more, my code spends 99% of its time computing FFTs. I initially found that FFT benchmarks in MATLAB were comparable to C++, which can be explained by the fact that MATLAB calls FFTW libraries which are well optimized. However, I wasn’t using a well-optimized array implementation so the real improvement came when I found John Bowman’s fftw++ headers and Array class. In the end, re-writing my code with these data structures results in a 2-4x speedup over MATLAB. The additional advantage is that a portion of my simulation is embarrassingly parallel so C++ makes it easy to exploit that.

Continue reading

MATLAB → C++

Looking through some google results after searching for tips on porting code from matlab to c/c++ I came across the following response on one forum:

Its not a good idea to use .m to C conversion (using mcc) for any commercial application. Codes produce in this way are far from optimized and are slow to run. Most of the time its used for research in Universities where a grad student is too lazy to do it and uses matlab instead. Infact when your code become complex conversion success is limited.

Wow, they sure tell it like it is. To give myself a little credit, it wasn’t laziness, it was a need for quick and easy visualization. Now, between good old gnuplot and the pngwriter c++ libraries, I have that all sorted out.

Continue reading