MATLAB Coder and parfor in MATLAB R2014b - matlab

Does the MATLAB Coder in MATLAB R2014b support parfor?
If I check the documentation, it reports:
Treated as a for-loop in a MATLAB Function block.
Does that mean that there is no speed improvement?

The loop runs in a serial manner only in the context of a "MATLAB Function" block
If you check the MATLAB Coder parfor reference page:
http://www.mathworks.com/help/coder/ref/parfor.html
You can see this information:
parfor ... ... creates a loop in a generated MEX function or in C/C++ code that runs in parallel on shared-memory multicore platforms.

Does Matlab Coder in Matlab-r2014b support parfor?
Yes, the list provided in you reference tells you it is supported.
Does that mean that there is no speed reduction?
Yes. You literally quote "it is treated as a for loop".
So in the strict sense of the word parfor is supported, since it will not throw an error. However, it is treated the same way MATLAB would treat it when the parallellisation toolbox is not installed, as a regular for loop. Thus yes, you can compile MATLAB code containing parfor loops, but they will be treated as serial for loops.
Note that the above only holds true for function blocks; as #Edric pointed out:
parfor ... ... creates a loop in a generated MEX function or in C/C++ code that runs in parallel on shared-memory multicore platforms.

Related

Does Octave have a ScaleProblem equivalent for fmincon?

I'm in the process of porting a large set of MATLAB scripts to Octave. Some of the scripts use the MATLAB Optimization toolbox, specifically the fmincon function. In the optim package in Octave, the fmincon function exists but has different parameters. Is there a way to replicate the ScaleProblem parameter in Octave's fmincon?
In my Octave script, I use optimset:
options = optimset('fmincon','Algorithm','sqp','ScaleProblem','obj-and-constr', ...);
Which causes the following warning:
warning: optimset: unrecognized option: ScaleProblem
Is there a workaround for this?
Apart from the trivial advise to write a wrapper that looks for this parameter-value pair, scales the matrices & calls fmincon without the ScaleProblem option, I can only emphasize to use the NLopt-toolbox from the MIT: https://nlopt.readthedocs.io/en/latest/ it got far more optimization algorithms with a neat matlab/octave interface, all open-source, and -- to my experience -- often more accurate and faster indeed =)

For loop model Simulink

I want to model a for loop system in Simulink, how I can model the following MATLAB syntax into Simulink model?
N=3;
for i=0:1:N
sum(i+1)=factorial(i)/factorial(N);
end
I have tried for loop sub systems in Simulink and also Sum block for iteration loop but doesn't help me. factorial function can be calculated with FCN function.
Suggest me the ways to resolve this model with step time.
If you have the code already in matlab use a embedded matlab function to implement it i your simulink model. This is in general quite efficient since it will be compiled (compared to interpreted matlab function blocks)

is there a faster version of fminbnd in matlab?

I am now using fminbnd in Matlab, and I find it relatively slow (I am using it inside a nested loop). The function itself, its interface and the values it returns are great, but when looking into the .m file I see it is not optimized. As a matter of fact, I was hoping for something like that to be written as a mex.
Anyone knows of an alternative to fminbnd that works much faster and does not have as much overhead?
It's written like that because it has to evaluate (feval) your user-defined function(s) on every iteration. Matlab's ODE solvers work in the same way. In current Matlab it's costly for a C/C++ code to call a user-defined Matlab function and read in its return values iteratively.
Make sure you're using the options correctly, that fminbnd is the correct tool (maybe a simpler scheme would be better or, since this in a loop, maybe a multi-dimensional method like fminsearch would be more appropriate), and have optimized your objective function. The next easiest thing would be to try compiling your Matlab code to C or C++ (see codgen). You'll likely need to compile in your objective function, and all of the options, as well in order to avoid the slowdown issues mentioned above. I've not tried this for fminbnd, but I did see mention of it working online. If your objective function itself is complicated, you could try just converting it to a mex function.
fminbnd is based on Brent's method. You can find C, C++, and FORTRAN code for that here. The GSL also has a version: gsl_min_fminimizer_brent.

How does MATLAB vectorized code work "under the hood"?

I understand how using vectorization in a language like MATLAB speeds up the code by removing the overhead of maintaining a loop variable, but how does the vectorization actually take place in the assembly / machine code? I mean there still has to be a loop somewhere, right?
Matlab 'vectorization' concept is completely different than the vector instructions concept, such as SSE. This is a common misunderstanding between two groups of people: matlab programmers and C/asm programmers. Matlab 'vectorization', as the word is commonly used, is only about expressing loops in the form of (vectors of) matrix indices, and sometimes about writing things in terms of basic matrix/vector operations (BLAS), instead of writing the loop itself. Matlab 'vectorized' code is not necessarily expressed as vectorized CPU instructions. Consider the following code:
A = rand(1000);
B = (A(1:2:end,:)+A(2:2:end,:))/2;
This code computes mean values for two adjacent matrix rows. It is a 'vectorized' matlab expression. However, since matlab stores matrices column-wise (columns are contiguous in memory), this operation is not trivially changed into operations on SSE vectors: since we perform the operations row-wise the data you need to load into the vectors is not stored contiguously in the memory.
This code on the other hand
A = rand(1000);
B = (A(:,1:2:end)+A(:,2:2:end))/2;
can take advantage of SSE instructions and streaming instructions, since we operate on two adjacent columns at a time.
So, matlab 'vectorization' is not equivalent to using CPU vector instructions. It is just a word used to signify the lack of a loop implemented in MATLAB. To add to the confusion, sometimes people even use the word to say that some loop has been implemented using a built-in function, such as arrayfun, or bsxfun. Which is even more misleading since those functions might be significantly slower than native matlab loops. As robince said, not all loops are slow in matlab nowadays, though you do need to know when they work, and when they don't.
And in any way you always need a loop, it is just implemented in matlab built-in functions / BLAS instead of the users matlab code.
Yes there is still a loop. But it is able to loop directly in compiled code. Loops in Fortran (on which Matlab was originally based) C or C++ are not inherently slow. That they are slow in Matlab is a property of dynamic runtime (they are also slower in other dynamic languages like Python).
Since Matlab has introduced a Just-In-Time compiler loop performance has actually increased dramatically - so the old guidelines to avoid loops are less important with recent versions than they once were.

How can I call an m file in Simulink and put it to a block in my model?

How can I call an m file in Simulink and put it to a block in my model (without using an S function)? Does anybody have an idea? I'd really appreciate it.
If you're trying to apply a user-defined MATLAB function to Simulink signals, there are a few different ways to do this, depending on your objective. All options are available within the User-Defined Functions section of the Simulink library.
Use the MATLAB function block if you intend to generate code from your model. This block does have restrictions, the entire gamut of built-in MATLAB functions is not available.
Use the Interpreted MATLAB function block if you don't care about code generation, this block can make use of any functions.
Use the Fcn block if your m-file is trivial and contains a simple expression operating on the inputs. In this case you can type the expression directly into the block dialog and reference the input / output signals as shown in the documentation.
MATLAB Fcn block is the best solution to embed M-function file into Simulink model. However, be cautious which version of MATLAB you are using, e.g., with later versions of MATLAB Function Block can be implemented with M-function file with %#codegen and C compiler need to be with your MATLAB package. Good luck