0

I'm converting a program from Matlab to Visual C++ (Community edition) to see how much faster I can get it to run. My original program runs days at a time...

Although I'm very experienced with Matlab, I only know very basic C++ So i would like to ask about Linear Algebra functions that I need. I saw this thing: http://math.nist.gov/lapack++/ but i'm not sure if this is the best option for me.

My Matlab program looks something like this in principal:

1. Get a 30*30 pixel image from a big pre-loaded list of images.
2. Pad the image with random number of zeros (to be about 60*60).
3. Crop the Padded image randomly.
4. Down-sample the cropped image.
5. Run this line: A = A + 0.1*(sin(A) + W*A + Z*y); 
6. repeat about 7 Million times.

where A is a column vector of length 1000. W is a 1000*1000 Sparse matrix. Z is a 1000*30 Full matrix. y is a 30*1 vector.

So i need good functions for:

  1. Padding matrix
  2. Cropping matrix
  3. down-sampling matrix
  4. Multiplying and Adding matrices and Sparse matrices.

What specific library / functions would you recommend? keep in mind that i know very little C++ so please give URLs if needed.

alonhzn
  • 150
  • 1
  • 7
  • If you are comfortable in `Matlab` and want to keep some (not _all_ obviously) similarity in `C++` you can look at [armadillo](http://arma.sourceforge.net/). Otherwise just compare and take a pick at one [linear algebra](https://en.wikipedia.org/wiki/Comparison_of_linear_algebra_libraries) package that does what you want. Also read this SO question: [What are the most widely used C++ vector/matrix math/linear algebra libraries, and their cost and benefit tradeoffs?](https://stackoverflow.com/questions/1380371/what-are-the-most-widely-used-c-vector-matrix-math-linear-algebra-libraries-a) – Hoki Jul 30 '15 at 11:03
  • 1
    ummm. I think we had someone asking something similar yesterday about optimizing for speed by porting to C++, and I think the general public opinion was that most of Matlab are written in C++ anyways (and partly in Fortran 90? correct me here). So the conclusion was that unless you are really good with C++, don't attempt it. – GameOfThrows Jul 30 '15 at 11:04
  • @GameOfThrows It's often not how good *you* are. More often than not, it's how good the *library* you use is. Personally, I use [Eigen](http://eigen.tuxfamily.org/) and can achieve 10-100x speedups compared to the matlab programs I rewrite. – Avi Ginsburg Jul 30 '15 at 11:07
  • The question referred by _GameOfThrows_ is [this one](https://stackoverflow.com/questions/31692856/why-matlab-is-faster-than-c-in-creating-random-numbers). The consensus in the comments is indeed that if you are a beginner in `C++` you may not achieve any improvement just by switching to `C++`. The choice of a good library is of course important, but the library won't do all the work for you. There may be a loooong learning curve before you can optimize your `c++` code as much as your `Matlab` code (assuming the Matlab code is already optimized of course). – Hoki Jul 30 '15 at 11:15
  • Malab uses lapack inside, so if you try to make faster writing code with lapack, you wont . Consider speeding your Matlab code, and consider profiling it. Check where the time goes and think if you can speedup specifically that line/function. – Ander Biguri Jul 30 '15 at 11:42
  • Thanks guys! I see many opinions and that's great. I think I will try to create a smaller program that has the same basic operations as my full program, just to benchmark and get an estimate of how much i can gain using C++ over Matlab on my specific requirements. – alonhzn Jul 30 '15 at 23:06

0 Answers0