Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • printf from a plugin

    I am just learning to make a Stata plugin, and need to print from inside the plugin, at least for debugging. I find that SF_display only takes a single string argument, so it is work to print the value of a number. printf seems to work - but only if the plugin has a non-zero return. That seems very strange, but I have gone back and forth a few times and "return(90)" shows the printf output in the log file, while with "return(0)" the printf output disappears. My actual calculations are in fortran, and are called from a C wrapper that is almost exactly like the "sum.c" example in the docs.. It works, but there a some odd values returned I would like to debug. Of course I would even more like to use the write statement in fortran, but without a fortran main program it doesn't seem possible to do any I/O. So, any suggestions on how to display some values?

  • #2
    Hi Daniel,

    I can't really help you (haven't written plugins before) but if you do manage to write the plug-in, please post any gotchas or tips you find useful, as they are quite scarce, and one of the things preventing me from going through the plugin route.

    PS: Perhaps looking at this repo helps in some way? https://github.com/kousu/statasvm/bl.../_svm_setenv.c

    Comment


    • #3
      I will post my experience once I have a fully working program. It turns out that calling a fortran program from the C wrapper is very straightforward. I haven't figured out how to call a C function from fortran though.. The documentation I have found assume the C code can be modified, which isn't the case for the SF_* functions provided by Stata.

      The URL you mention does refer to a routine "sterror" that would appear to be just what I am looking for, however the source file "stutil.c" is nowhere to be found, nor the header file "stutil.h". Perhaps someone can direct me to a source for those.

      Comment


      • #4
        I can find the header file here:

        https://github.com/kousu/statasvm/bl...r/src/stutil.h

        If the above code is useful and there are no replies to the thread, maybe try emailing the author (Nick Guenther [email protected] ), as he might have some suggestions.

        Comment


        • #5
          Would it be possible/practical to translate the FORTRAN code into Mata? Bill Gould did an article about doing that a while ago that might at least help with exposing the underlying FORTRAN code. If there's a Java wrapper for the same code base I could potentially help, but am not too sure I could be of much help on the C side of things.

          Comment


          • #6
            Here are some notes on my experience building a Stata plugin from fortran code.

            http://www.nber.org/stata/efficient/fortran-plugin.html

            printf did eventually start to work - not sure why I was having trouble. But I was never able to write from within fortran, which made debugging very difficult. So I am not sure I would do this again. My project is very close to completion, just a few oddities left to debug.

            Comment


            • #7
              Thanks for the writeup! Interesting to see that list of limitations

              Comment


              • #8
                I'm new to this group, and I haven't written plugins in Linux, only Windows, but in Windows you can get just about anything done. I don't think any of these would be limitations, only difficulties.

                1.I couldn't figure out how to write output from FORTRAN.

                Fortran won't know about _stata_*, you could create a wrapper function in C that your FORTRAN calls to use SF_display() or some other output, if you can get the FORTRAN and gcc linker to cooperate bi-directionally. You could use PIPES, or shared memory, etc, I have done both, but not with FORTRAN, but named PIPE should work fine.

                2.No way to access variables by name, only by position in the STATA call.

                You can use macros, I've done this, requires setup in do script, or some other data passing mechanism to relate names to position, as long as you are the one calling the plugin. VarLists passed to plugins won't match up unless you pass _all. there are many creative ways to deal with this, I've even passed and used the variable value labels.

                3.No way to create an e() or r() return variable.
                Your do script can do this, if you define it as R or E class. There are other "tricky" ways to get this done also. I've written and read them, but it's not pretty.

                4.No help parsing options.

                What do you mean?

                5.Errors were often returned with code r(498), the documented text of which concludes "The code 498 is not helpful." Some errors hung the terminal or caused Stata to abend without a message. Fortran runtime messages are suppressed (the program abends with a complaint about a missing library) and there is no obvious way to add debugging statements to the FORTRAN.

                Error 498 is documented, it is a bad variable reference, from STATA - > "if you attempt to exceed either the number of observations in your data or the number of variables you have specified, both routines will return error code 498." Also, you will not see errors from a DLL unless you compile in DEBUG mode and use ASSERTs.

                Comment


                • #9
                  I was wondering if anyone -since this post was written- has come across any resources giving advice or examples regarding Stata plugins? I have not been able to find anything on this topic. Are they always written in C++?

                  Comment


                  • #10
                    Belinda Foster

                    I've written plugins in Java using the Java API, but have no experience with C/C++. There's a few postings on the Stata website related to this, but there isn't a lot of material available out there.

                    Comment


                    • #11
                      I've written plugins in Java using the Java API, but have no experience with C/C++. There's a few postings on the Stata website related to this, but there isn't a lot of material available out there.
                      wbuchanan Actually, there is no other material out there hence my question. Do you have the plugins online on Git? I would be interested in having a look purely for educational purposes.

                      Comment


                      • #12
                        Belinda Foster they are all publicly available. There are a few working plugins and a few works in progress that I've not been able to work on in a bit: https://github.com/wbuchanan.

                        Comment

                        Working...
                        X