MATRIXx

cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic allocation and autocode

Is there any way to use autocode to generate code that uses dynamic allocation for the global variables (e.g., subsystem input outputs, %var, ....)?
The reason is that, we would like to combine codes generated for several hybrid systems (with continuos, discrete, and triggered blocks), or several instances of the same model, into one system using hand written wrapper functions. Therefore, each individual system needs to have its own scheduler and private copy of the global variables.
Thanks,
0 Kudos
Message 1 of 4
(7,436 Views)
Ziggy,
You would not necessary need these to be dynamically allocated. The scope of variables and define statements are limited to the file where they are declared. Only what is declared in the header file is available to other parts of the program. Functions on the other hand are globally scoped, but if you add static in front of the function prototype then it is scoped to the file. You can modify the function prototype for subsystems by editing the tpl segment gen_tasks_decl in c_core.tpl.

You could have AutoCode generate code for each model. You would export some functions in a header file (for example Scheduler and main) that give you access to that model. These functions would have to have unique names.

The attached files provide a very simple exa
mple of how the scope can be limited to a file.

Carl L
National Instruments
0 Kudos
Message 2 of 4
(7,435 Views)
This solution works fine for combining multiple systems autocoded in different C files. However, it will fail in cases which multiple instances of a single model (sharing the same C-file) is needed.

As an example consider an "engine" model which is developed and autocoded. Now, one requires an aircraft simulation code to call the "engine" for the left and right engines (each with a different set of global variables).

The only way that this is possible (using a single source code) is to get rid of all global and static variables and replace them by local variables that are specific to each instance of the model. However, this is a very difficult job considering the lack of visibility and accessibility to modify the subsystem code.

The code can be
organized such that all the global variables are located in a structure and be passed to each subsystem. In this way the
definition of the subsystem functions should include an extra variable (pointer to the global variable structure), but, the user cannot instruct
this to autocode since subsystem code is generated by the tpl atomic function "define_subsystem".

Any suggestion to generate a reusable and reentrant code?


Thanks,
0 Kudos
Message 3 of 4
(7,435 Views)
Another approach to creating these reuseable models is to convert the model into components. This requires defining which variables within the model are to be "exported" or parameterized for each instance. Then each instance of a model (right, left engine) could have unique parameter sets but still use the same model. Depending on what limitations you might have for generated code, this technique ends up generating separate variable names for each instance by creating a separate Xmath segment for each instance.
0 Kudos
Message 4 of 4
(7,435 Views)