Simulink code generation: function stubs from Function Caller blocks and their return values/arguments - code-generation

In my Simulink model I have several Function Caller blocks like this:
Simple Function Caller block
The function prototype would simply be y = someFunction(). The output argument uses a custom enum type and is given as someEnum(1).
The output signal is defined as one-dimensional throughout.
When generating code from the model, these Function Callers have always yielded a function stub in the expected form of
extern someEnum someFunction(void);.
However, after a lot of changes recently, I've just noticed that code generation now suddenly yields function stubs in the form of
extern void someFunction(someEnum *rty_y);
for some (not all!) Function Caller blocks.
I have compared every parameter about the Function caller blocks and the related output signals that I could find but I can't find any difference between the affected ones and those working as expected in the current version or the same blocks in previous versions. All functions and signals have been renamed, but that's also true for those Function Caller blocks that are not affected.
The Code Generation options are also identical.
I have tried to understand from the help files what might cause the coder to use pointer arguments instead of direct return values for the function stubs but couldn't find anything.
Any hint at what might cause the code generator to use pointers would be greatly appreciated.

Found the problem. Some of the affected blocks had their C/C++ return argument set to "void" in their "Configure C/C++ Function Interface" dialog.
Some of the affected blocks (unfortunately, both of those I had checked before as well) were still set to "y" here and I had to change the setting to "void" and back to "y" before it yielded the desired result.

Related

How to change verbosity of uvm components after certain condition

I am trying to change the UVM verbosity of the simulation after satisfying certain conditions. Verbosity options of different components are passing through the command line as +uvm_set_verbosity. Once the conditions are satisfied, then the simulations should run with the the command line +uvm_set_verbosity option. Till then simulation runs with low verbosity for all components.
Looking through the UVM library code, it appears that there is a function called m_set_cl_msg_args(). This function calls three other functions that appear to consume the command line arguments like: +uvm_set_verbosity, +uvm_set_action, +uvm_set_severity.
So what I did was get the uvm_root instance from the uvm_coreservice singleton, and then use the get_children() function from uvm_component class to recursively get a queue of all of the uvm_components in the simulation. Then call the m_set_cl_msg_args() function on all of the components.
My code looks like:
begin
uvm_root r;
uvm_coreservice_t cs_t;
uvm_component array_uvm[$];
cs_t = uvm_coreservice_t::get();
r = cs_t.get_root();
r.get_children(array_uvm);
foreach(array_uvm[i])
array_uvm[i].m_set_cl_msg_args();
end
Even though this code compiles properly, But this is not changing verbosity. Any idea ?
Moreover I am able to print all the components in array_uvm. So I am guessing
array_uvm[i].m_set_cl_msg_args();
this as a wrong call.
Anyone have any other suggestion to change verbosity during run time.
You should never use functions in the UVM that are not documented in the language reference manual. They can (and do) change in any revision. I'm guessing +uvm_set_verbosity only works at time 0 by default.
There is already a function to do what you want
umm_top.set_report_verbosity_level_hier()
I suggest using +UVM_VERBOSITY=UVM_LOW to start your test, and then define your own switch for activating the conditional setting.
If you want a specific component, use component_h.set_report_verbosity_level() (add the _hier to set all its children)
You can use the UVM's command line processor get_arg_values() method to specify the name of the component(s) you want to set, and then use umm_top.find() to get a handle to the component.

Is it a pure function?

I have following function:
def timestamp(key: String)
: String
= Monoid.combine(key, Instant.now().getEpochSecond.toString)
and wanted to know, if it is pure or not? A pure function for me is, given the same input returns always the same output. But the function above, given always the same string will returns another string with another time, that it is in my opinion not pure.
No, it's not pure by any definition I know of. A good discussion of pure functions is here: https://alvinalexander.com/scala/fp-book/definition-of-pure-function. In Alvin's definition of purity he says:
A pure function has no “back doors,” which means:
...
It cannot depend on any external I/O. It can’t rely on input from files, databases, web services, UIs, etc; it can’t produce output, such as writing to a file, database, or web service, writing to a screen, etc.
Reading the time of the current system uses I/O so it is not pure.
You are right, it is not a pure function as it returns different result for the same arguments. Mathematically speaking it is not a function at all.
Definition of Pure function from Wikipedia
The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices (usually—see below).
Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices (usually—see below).

Variable Declaration with the Presence of Nested Functions

Someone on /r/matlab asked me a really interesting question a few days ago related to a Flappy Bird clone submitted to the MATLAB FEX. The poster noticed that if you open the main .m file, stop it in the debugger on the first line, and run a whos(), you see a bunch of variables before they are explicitly defined by the function.
The first thing that I noticed in the editor was the syntax highlighting indicating the presence of nested functions. At a glance, it seems like the variables returned by the whos() are only those that will be defined at some point in the scope of the base function.
You can recreate this with a simpler example:
function testcode
asdf = 1;
function testing
ghfj = 2;
end
end
If you set a breakpoint on the first line and run a whos(), you get
Name Size Bytes Class Attributes
ans 0x0 0 (unassigned)
asdf 0x0 0 (unassigned)
I couldn't seem to find anything explaining this behavior in the documentation for nested functions or related topics. I am not a computer scientist and my programming knowledge is limited to MATLAB and a very small sprinkling of Python. Can anybody explain what is going on? Does it have something to do with how MATLAB compiles the code at run time?
The workspace of a function with nested function is protected. When the function is called, Matlab has to analyze the code to determine which variables are in scope at what part of the function. Remember, variables that are declared in the main function and that are used in a nested function are passed by reference, and can be modified within the nested function even if not explicitly declared as input or output.
To avoid messing up any of the nested functions, and possibly to help speed things up, Matlab does not allow assigning any additional variables to the workspace of that function. For example, if you stop the execution of the code at line 1, and then try assigning a value to a new variable klmn, Matlab will throw an error. This can be a bit frustrating for debugging, but you can always assign ans, fortunately.

Simulink: What happens when subsystems are initialized?

I've been having a lot of trouble getting simulink's block callbacks to run, and the documentation is woefully inadequate and disorganized. It seems that I'm misunderstanding multiple points of how Simulink compiles models, but since StackOverflow dislikes multi-part questions, I will post them one at a time.
The situation: I have a library of components, each of which is a virtual subsystem whose parameters are defined through the masks. Block A has Parameter a which is sent to the base workspace using the 'assignin' command.
Next, the block B has a parameter b which is initialized in the Initialization tab of the mask.
Finally, the StartFcn callback of the block B runs a script which needs to reference both a and b to calculate c. In the script, I reference a simply as a because it's in the 'base' workspace, and I reference b using get_param(gcb,'b').
Now, this last command works when the parameter b is a user input (so it's a constant value). But in my case, this b is calculated using other parameters in the Initialization tab. And for some reason, in the script, this parameter is always zero.
I added a display within the block B to see what these values are, and they are clearly non-zero.
Can someone please explain why the script cannot seem to get the real value of the areas out of the block?
You can get the masked workspace variable using getworkspacevariable

Can pure functions read global state?

Please note: by a "pure" function, I don't mean "pure virtual"
I'm referring to this
If a function "reads" some global state, does that automatically render it impure? or does it depend on other factors?
If it automatically renders it impure, please explain why.
If it depends on other factors, please explain what are they.
A "pure" function is a function whose result depends only on its input arguments. If it reads anything else, it is not a pure function.
In certain specialized instances, yes. For example, if you had a global cache of already-computed values that was only read and written by your function, it would still be mathematically pure, in the sense that the output only depended on the inputs, but it wouldn't be pure in the strictest sense. For example:
static int cache[256] = {0};
int compute_something(uint8_t input)
{
if(cache[input] == 0)
cache[input] = (perform expensive computation on input that won't return 0);
return cache[input];
}
In this case, so long as no other function touches the global cache, it's still a mathematically pure function, even though it technically depends on external global state. However, this state is just a performance optimization -- it would still perform the same computation without it, just more slowly.
Pure functions are required to construct pure expressions. Constant expressions are pure by definition.
So, if your global 'state' doesn't change you are fine.
Also see referential transparency:
A more subtle example is that of a function that uses a global variable (or a dynamically scoped variable, or a lexical closure) to help it compute its results. Since this variable is not passed as a parameter but can be altered, the results of subsequent calls to the function can differ even if the parameters are identical. (In pure functional programming, destructive assignment is not allowed; thus a function that uses global (or dynamically scoped) variables is still referentially transparent, since these variables cannot change.)
In Haskell for instance, you can create an endless list of random numbers on the impure side, and pass that list to your pure function. The implementation will generate the next number your pure function is using only when it needs it, but the function is still pure.