Thread Subject: C S Function Rwork vs Global Variables

Subject: C S Function Rwork vs Global Variables

From: Jonathan Currie

Date: 8 Nov, 2009 21:56:02

Message: 1 of 3

I am currently working on a C s function that uses considerable memory (10000+ doubles) for storing intermediate calculations. I am interested in optimizing the memory usage & runtime performance for both simulations and real time workshop code generation.

In my experience, I have three resonable options available:

1) Create one long Rwork vector, and index the addresses to each of the local variables I use at every function call.

2) Declare a global pointer, and use malloc to fill this with the required memory during initialization. Then index my variables as above.

3) Declare multiple global pointers, with multiple calls to malloc for each variable during initialization. This means I do not have to index my variables, but multiple malloc calls is also not suggeted.

If anyone has any suggestions for the best strategy, this would be appreciated!

Subject: C S Function Rwork vs Global Variables

From: Praetorian

Date: 8 Nov, 2009 23:35:54

Message: 2 of 3

On Nov 8, 2:56 pm, "Jonathan Currie" <jonathan.cur...@hotmail.com>
wrote:
> I am currently working on a C s function that uses considerable memory (10000+ doubles) for storing intermediate calculations. I am interested in optimizing the memory usage & runtime performance for both simulations and real time workshop code generation.
>
> In my experience, I have three resonable options available:
>
> 1) Create one long Rwork vector, and index the addresses to each of the local variables I use at every function call.
>
> 2) Declare a global pointer, and use malloc to fill this with the required memory during initialization. Then index my variables as above.
>
> 3) Declare multiple global pointers, with multiple calls to malloc for each variable during initialization. This means I do not have to index my variables, but multiple malloc calls is also not suggeted.
>
> If anyone has any suggestions for the best strategy, this would be appreciated!

I'd recommend the first strategy. If you plan on having more than one
instance of this S-Function in your model, and if these intermediate
values need to be different for each instance, the global variable
strategy will fail since global variables are common to all S-Function
instances.

HTH,
Ashish.

Subject: C S Function Rwork vs Global Variables

From: Ralph Schleicher

Date: 10 Nov, 2009 21:10:30

Message: 3 of 3

Praetorian <ashish.sadanandan@gmail.com> writes:

> I'd recommend the first strategy. If you plan on having more than one
> instance of this S-Function in your model, and if these intermediate
> values need to be different for each instance, the global variable
> strategy will fail since global variables are common to all S-Function
> instances.

Simplicity in the implementation rules, therefore use strategy 3).
Multiple pointers don't use that much more memory than one single
pointer. And 10000 doubles are 80 kiB, that means approx. 13500
instances of your S-function fit into 1 GiB. Think about that.

--
Ralph Schleicher <http://ralph-schleicher.de>

Development * Consulting * Training
Mathematical Modeling and Simulation
Software Tools

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
malloc Jonathan Currie 8 Nov, 2009 16:59:05
global variable Jonathan Currie 8 Nov, 2009 16:59:05
rwork Jonathan Currie 8 Nov, 2009 16:59:05
c s function Jonathan Currie 8 Nov, 2009 16:59:04
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com