how to clear variables in code generation ?

24 views (last 30 days)
surya on 15 Dec 2013
Edited: Mike Hosea on 23 Dec 2013
I'm trying to generate C-code from Matlab using "codegen". Codegen does not support "clear var1" call, considering "var1" is a variable I want to clear. How do I clear variables from memory and still be compatible with C-code generation.
Thanks, Surya.

Answers (2)

Mike Hosea
Mike Hosea on 20 Dec 2013
What problem are you trying to solve by clearing variables? Have you verified that it is still a problem with code generation?
Mike Hosea
Mike Hosea on 23 Dec 2013
OK, there are a number of things to look at here. One is that you should do something like
cfg = coder.config('mex');
cfg.IntegrityChecks = false;
cfg.ResponsivenessChecks = false;
and then recompile using
codegen ... -config cfg
Of course you can do all that by clicking in the Coder GUI. Then we should look at the possibility that the MATLAB Code you're comparing to is already compiled to binary and optimized. Examples include FFT and most linear algebra tasks. Generally MATLAB Coder speeds up code you write but may not speed up code that is "built in" to MATLAB.
Additionally, MATLAB may be solving the problem using multiple threads while the mex file is using a single thread. You can start MATLAB with the option -singleCompThread to investigate whether MATLAB's multithreading is the difference. You can add parfor to your own code, but we don't yet support automatic multithreading in library code.
Realistically, you're not going to beat MATLAB functions like FFT, SVD, EIG, etc. because what Coder does is generate generic C code and compile it, whereas what has been done with the MATLAB functions is to write and use customized code for the platform MATLAB is running on. Even in those situations you can tap into what MATLAB is doing by calling functions "extrinsically". You will pay for the overhead of copying data to MATLAB and back, but sometimes it is a win. To call a function extrinsically, you generally need to pre-declare the output type, e.g. if foobar always returns an output the same size and type as its input, you might write
y = zeros(size(x),'like',x);
y = foobar(x);
or, to avoid having to foobar extrinsic throughout the entire file
y = zeros(size(x),'like',x);
y = feval('foobar',x);
If nothing pans out, take a look at the generated code and see if you can find extra array data copies that shouldn't be there. If you can, we'd like to hear about them because that is something we need to try to optimize away. MATLAB is fundamentally a pass-by-value language, so the compiler generates copies to honor that semantic but then tries to eliminate them before it's done. Sometimes one or two copies that could have been eliminated squeak by, and then we (here) need to understand why and try to fix that.

Sign in to comment.

surya on 21 Dec 2013
Thanks for your response! nothing in particular, the code is running fine. but the speed at which the mex is running is multiples higher than the m-file, which is making me look at all possibilities. Thanks, Surya.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!