Thanks Sebastien
I would like us to clarify our understanding:
1) For start, I believe that there is a certain (however small) development, maintenance and performance overhead in maintaining strict "Need-to-know" data policy all the way through.
Hence, as an optimal solution (as per my response to Michel's email yesterday and I believe in line of what you wrote), I believe that:
a) we should still use the new GeneralParams data tree interface framework to manage interface to the pointer(s) to the structure(s) (i.e. one or more, see below) LOCAL to the calling Matlab function, and,
b) only the high level functions should "know" the reference to the GeneralParams structure (whose implementation contains pointer(s) to the Dynare structure(s)) and they use it to query only the params needed locally and/or to be passed as arguments to the lower level functions they call either in loop or ad-hoc.
3) Still outstanding issues for clarification are:
a) should we start imposing the "NTK" policy as high as Matlab Dynare calling function and there create a single new structure containing only the params needed by C++ estimate before calling it, or leave to C++ to manage the access on the as need-to-know basis.
b) Again, should we use mexGetVariablePtr() to query structure(s)' pointer(s), or pass the structure(s) as argument(s) - i.e. as pointer(s) in either case?
I now expect it is both more in line with the NTK policy and also quicker to pass them as arguments as we can reduce both, the freedom of access and the number of calls to Matlab respectively.
PS: sorry for misnomer: mexGetVariablePtr(), as its name suggests, passes pointer, not C++ type reference - I only referred to "by reference" in a more generic fashion, just as opposed to "by value" (as mexGetVariable() does) and since, as I understand, Matlab can not pass C++ references to C++ but only pointers.
Regards George
----- Original Message ----- From: "Sébastien Villemot" sebastien.villemot@ens.fr To: "List for Dynare developers" dev@dynare.org Sent: Tuesday, September 15, 2009 1:23 PM Subject: Re: [DynareDev] C++ DsgeLikelihood and Dynare Parameters
Le mardi 15 septembre 2009 à 12:01 +0100, G. Perendia a écrit :
- C++ mexGetVariablePtr() can access either pointers to global structure
(with option global), or pointer to the structure local to the calling Matlab function (with option local) which would be the same as what would be passed to the called C++ function as argument.
If C++ code changes and needs additional parameter, then it only need to call different param from the interface.
This is precisely what I don't like in the general case. In some cases, I think it is better to have the explicit set of parameters in the function prototype (as arguments), so that one can see immediately what are those used by the function.
For example suppose that an internal function of the DLL uses only one option. It makes no sense to pass the pointer to a structure containing all the options used by the calling Matlab function, because it gives the impression that this function depends on the whole options set: this breaks modularity...
But as a general rule I don't claim that your framework is useless. I think that you have to make different decisions depending on the situation: * if all the functions in the calling tree of a given DLL need all the options passed by the calling Matlab function, then it makes sense to use your framework * on the contrary, if the deepest functions in the calling tree need only a small subset of the options passed by the calling Matlab function, then I think you should disaggregate the options and pass as arguments to the deepest functions only those that are needed.
- Other advantage of the mexGetVariablePtr() based interface is that it
allows for easy update of the structures (needed by estimate), including real-time, interactive updates if any future parallelisation requires that real time facility.
This is a good point provided that you add lock/mutex features.
- I will however start working on altering the code as you suggested and
create one structure to pass it from Matlab, i.e. create it before calling C++.
This is not my point, as I hope to have made clear in this message.
Is it ok if I still use mexGetVariablePtr() to pass it by reference or shall I pass it as argument (also by reference) anyway? An alternative is to use mexGetVariable() which copies the structures locally.
Of course mexGetVariablePtr() is better, in order to avoid unnecessary copying.
Also note that, as a general rule, my opinion is that we should use passing-by-reference when possible. Passing-by-pointer and passing-by-value (except for integers and doubles) should be avoided whenever possible.