0

I have no experience with MATLAB before. Now, I am trying to edit a MATLAB program to make it runnable in GNU Octave (both on Windows system).

I fixed some errors such as +:nonconformant arguments (op1 is 1x1, op2 is 0x1) by changing some operators or special characters. For example, I changed

val = textscan(unit53,'%d %s %f %f %f %f \r\n'); 

to

val = textscan(unit53,'%d %s %f %f %f %f "\r""\n"'); 

I successfully edited the program and it runs without an error. However, my edited program takes about 32 hours to finish running. The original MATLAB program only takes about 10 minutes to run.

The slow part of program is a for loop about declaration, filling matrices with the information read from a document, and doing calculation with those matrices.

Does this happen (runs slower) every time when you try to run MATLAB code in Octave?

How to make that MATLAB code run faster?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • Octave is typically slower than MATLAB for almost everything. In your case is taking ~3 times the time it takes in MATLAB which is a bit too much and might be another problem, but it's also possible that you are doing it fine. I don't think there's any general rule to solve this. (may I ask why are you migrating the code?) – myradio Jun 14 '18 at 14:46
  • Also, the title seems a bit ambiguos. I guess you want the Octave code to run faster than is currently running. Or you want to actually beat MATLAB? – myradio Jun 14 '18 at 14:47
  • Thank you very much for your help. I am bad at English writing. That's why I made an ambiguous question. I would like the Octave code to run faster than is currently running. – Killed_by_Bugs Jun 14 '18 at 14:51
  • I just noticed you said it takes 10 minutes in MATLAB and 30 hours in Octave!, not a factor 3 but 180!. Something must be wrong then. Are your results the same? – myradio Jun 14 '18 at 14:54
  • I have not got the result yet. The values of variables before running the slow loop are the same as in original MATLAB codes. I added fprintf('%d loops reached', loop_counter); to the loop. Then I count the duration of ruining a single loop. So, I can evaluate the total running time is about 32 hours. – Killed_by_Bugs Jun 14 '18 at 15:00
  • Well, I think there must be something wrong, I wouldn't even bother to wait for the results. Yet I don't know why is the reason you want to move to Octave if the code works in MATLAB. – myradio Jun 14 '18 at 15:14
  • Yes, I think you are right. There must be something wrong in the codes. About the reason migrating the code, I don't know. My boss gave this job to me. Maybe, the Octave version is for the people who doesn't has MATLAB license? Thank you very much for your help. – Killed_by_Bugs Jun 14 '18 at 15:21

1 Answers1

2

The original MATLAB codes only takes about 10 minutes to run. The slow part of program is a for loop about declaration , filling matrix with the information read from a document,and doing calculation with those matrix.

As a rule of thumb, if you see a for loop with many iterations in an Octave program, that won't be a good program. Matlab used to be the same but now they have a decent JIT that speeds up such sloppy code. If your code is vectorized, then you shouldn't see that much of a difference between Octave and Matlab.

How to do it, depends on your problem. Many functions will actually work fine with arrays it's just that people don't use them that way. Go into your for loop, and take each line outside of the loop, one by one. Depending on your problem, it might not be easy. An alternative, if the loop iterations are independent from each other, consider using the parallel package.

As an example, I once had a program that in Matlab ran in ~20 minutes. In Octave, I killed it after 2 days. The main issue were in two for loops, one of them was iterating over each pixel of a 512*512*2000 image. After I vectorized, Octave ran it in under 2 minutes. I have had to port many Matlab programs, this is very common.

Edit (answer to comment): there are plenty of examples and tutorials out there about vectorization but there's no silver bullet. The solution is often unique and it will be dependent on your code. For the specific case of the difference between continuous elements, you should use diff to get an array of the differences and then work on it. Vectorizing your code will look like an increase in memory usage but it will be much faster.

carandraug
  • 12,938
  • 1
  • 26
  • 38
  • Could you please show me some examples about vectorisation? – Killed_by_Bugs Jun 22 '18 at 10:32
  • In my code, those for loops are quite complicated. Lots of variables are used. Also, the difference between two continuous element is used. (for example: hx(i,j) - hx(i,j-1)). Should I create a new matrix for the difference, then use the matrix in the code for calculation to solve this? Thank you. – Killed_by_Bugs Jun 22 '18 at 10:39
  • @Killed_by_Bugs see edit on answer to address your question. – carandraug Jun 22 '18 at 11:07
  • Thank you for your answer. However, there is a problem if I use diff to get an array of the differences. The diff function returns a array whose column number is 1 less than the original array. So, I will get “error: operator -: nonconformant arguments (op1 is......” this kind of error. I think I might add 1 more zero column to that diff returned array, but I don't know how to do it. Could you please suggest? – Killed_by_Bugs Jun 22 '18 at 14:15
  • @Killed_by_Bugs There is also https://codereview.stackexchange.com/ I would suggest you post there a complete, runnable and well documented code and ask for help running it faster and tips for better style. But it's important that others can grab that piece of code and can start to profile and optimize it – Andy Jun 22 '18 at 15:16