Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Global Variables and Multiple instances of S Function

470 views
Skip to first unread message

Zia

unread,
Jul 14, 2004, 8:23:36 AM7/14/04
to
Dear Readers,
I created a S function which had some global variables say:
...
int m;
static void mdlInitializeSizes(SimStruct *S){
.....

I compiled this S function to form dll file for a simulink block.
Now when I used two such instances of this block in parallel, I found
that the variable 'm' is considered to be the same in the two blocks
i.e suppose 'm' is changed to some value in one of the blocks then
that change is also tranparent to the other block too or 'm' is
changed for the other block too. Someone Please suggest some method
to make these blocks independent with Global variables still in use.
greatful for any help,
Zia

T Neeves

unread,
Jul 14, 2004, 8:32:06 AM7/14/04
to
Zia;

Using work vectors should help out.

From 'Writing S-Functions':

"If your S-function needs persistent memory storage, use S-function
work vectors instead of static or global variables. If you use static
or global variables, they are used by multiple instances of your
S-function. This occurs when you have multiple S-Function blocks in a
Simulink model and the same S-function C MEX-file has been specified.
The ability to keep track of multiple instances of an S-function is
called reentrancy.

You can create an S-function that is reentrant by using work vectors.
These are persistent storage locations that Simulink manages for an
S-function. Integer, floating-point (real), pointer, and general data
types are supported. The number of elements in each vector can be
specified dynamically as a function of the number of inputs to the
S-function."

Hope this helps.

Ted

Ziaul Hasan

unread,
Jul 14, 2004, 8:44:17 AM7/14/04
to
Hi Ted
Thanks for the reply. I used global variables to use two or more work
vectors.
How could I handle more than one work vectors in Simulink?
How are work vectors are implemented in Simulink?
Could you suggest some other way like defining external etc..? I have
a problem now else i have to compile it with different names.
Zia

Zia

unread,
Jul 14, 2004, 9:23:34 AM7/14/04
to

T Neeves

unread,
Jul 14, 2004, 10:12:05 AM7/14/04
to
Ziaul;

I typically use work vectors as 'storage bins', so to speak, in order
to use them persistantly. The key value of using work vectors you
are concerned with (I think) is reentrancy.

Work vectors act very similarly to global or static variables, in the
sense that they are accessable everywhere within a specific instance
of your S-function.

Reentrancy allows you to use multiple instances of your S-function
without things getting 'muddled up' with each other.

Conversly, using global or static variables DOES NOT provide
reentrancy. This means the global or static variable 'X' in one
S-function instance is no different (in memory) than the variable 'X'
in another instance of the SAME S-function. Using work vectors
avoids this 'confusion'.

I suggest <http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/sfg/sfun.html>

s a starting point.

Ted

0 new messages