Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Avoiding use of Mata external variables in large programs
From
"Lacy,Michael" <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
Re: st: Avoiding use of Mata external variables in large programs
Date
Thu, 9 May 2013 19:21:50 +0000
[email protected] (Rafal Raciborski, StataCorp) wrote:
Wed, 08 May 2013 15:49:25 -0500
>Michael Lacy <[email protected]> is seeking advice about avoiding the
>use of Mata external variables in Mata/Stata adofile programs:
>
>Below I sketch an outline Michael can follow to avoid using externals.
>This method assumes Michael knows in advance how many temporary names Mata
>will use.
... snip, snip
>
>--------------------------- begin mymain.ado ---------------------------
>prog mymain
> version 12
> tempname m1 m2 // need two temporary names for Mata
> capture noi mysub `m1' `m2' `0'
> local rc =_rc
> mycleanup `m1' `m2'
> exit `rc'
>end
>
>prog mysub
> gettoken a1 0 : 0 // note `a1' and `m1' refer to the same tempname
> gettoken a2 0 : 0 // note `a2' and `m2' refer to the same tempname
> syntax ...
>
> <some Stata stuff>
>
> mata: `a1' = mymata1(...)
> mata: `a2' = mymata1(...)
>
> <some Stata stuff>
>
> mata: mymata2(`a1',`a2'...)
>
> <some Stata stuff>
>end
>
>prog mycleanup
> syntax [anything]
> foreach m of local 0 {
> capture mata: mata drop `m'
> }
>end
>
>exit
>--------------------------- end mymain.ado ---------------------------
>
>It is possible that -mysub- will error out or the user will press the break
>key before -mysub- finishes. If this happens, the `m1' and `m2' Mata objects
>will not automatically be dropped from Mata memory and, considering they can
>point to large matrices, we need to clean up. This is why we capture the call
>to -mysub- and call -mycleanup- to drop these Mata objects from memory.
Thanks to Rafal and Sergiy for continuingto respond to my question.
My understanding, then, is that Rafal is endorsing the idea of creating the
Mata externals with -tempname-. (`m1' and `a1', for example, are still
visible to -mymata2- only as external variables, right-- so I'd describe them
as externals.)
And, although this is not stated directly, I'd like to check my understanding
that the purpose of creating the m1 and m2 in one program but using them in
another is so that, if -mysub- fails, there will still be a program (-mymain-)
that knows their names so that they can be cleaned up. On that view, the
sole purpose of the -mymain- to -mysub- chain is to ensure this ability to
clean up in case of error?
Regards,
Mike Lacy
Dept. of Sociology
Colorado State University
Fort Collins CO 80523-1784
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/