Announcement

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

  • mata function available in different ado-files

    Dear all,

    I am trying to execute a mata function in different but related ado-files, but I don't know how to do it efficiently. Right now, I have the same mata functions at the end of each ado-file. Let me explain.

    I have an ado file called `xyz.ado` that executes several other subroutines depending on the needs of the user. For instance, it may execute routine `xyz_load.ado` or `xyz_create.ado` or `xyz_estimate.ado`. Each of these ado-files uses a mata function called `hello()`. My problem is that if I defined `hello()` as shown below, it is not accessible by any of the `xyz_SUBROUTINES.ado` adofiles,

    Code:
    program define xyz
            .
            xyz_load ...
            .
    end
    
    version 15
    mata:
    void hello() {
        printf("hello people\n")
    }
    end

    The restrictions that I face are the following:

    1. Each of the subroutnines `xyz_SUBROUTINES.ado` must remain as independent ado-files. That is, I cannot do the following,

    Code:
    program define xyz
            .
            .
            .
            xyz_load ...
            .
            .
            .
    end
    program define xyz_load
            .
            mata: hello()
            .
    end
    
    mata:
    void hello() {
        printf("hello people\n")
    }
    end
    2. the whole `xyz` package is used for several people in my team, so the mata function should available in all the subrountines each time the user executes `xyz`
    3. all the files of the package `xyz` won't be placed in the same directory path in the computer of each of the members of my team. That is, some people may have it in c:/ado/plus/x, others in c:/ado/personal/x, and others in c:/onedrive/stata/ (the ones with the latter directory path structure have already included such a paths in searching directories of stata using adopath ++)

    So, my questions are the following.

    1. Is it possible to compile a mata library from a .mata file directly from within an ado-file?
    2. When I placed the in `xyz.mata` with all my mata function in c:/ado/plus/x, and then execute `do xyz.mata` directly within my `xyz.ado` it does not read the version in c:/ado/plus/x but the one in my current directory. How do I make Stata to look for .mata file in the searching directories stored in the global macro S_ADO?


    Thank you so much for your help.

    Best,




    Best,
    Pablo Bonilla

  • #2
    Hi Pablo,

    I've thought about this issue a lot, and tried several solutions, from including compiled Mata files, to compiling on-the-go, etc.

    What I think is by far the best approach is to just have a mata file with all the code, and include it at the end of each ado. For instance, this is what I do in the ppmlhdfe package:

    https://github.com/sergiocorreia/ppm...lhdfe.ado#L370

    Code:
    findfile "ppmlhdfe.mata"
    include "`r(fn)'"
    By using "filefile", you will search all the possible folders for the package (see "adopath"), and by including, you actually get faster code than if you were to call a compiled Mata file.

    Comment


    • #3
      This is an excellent solution, Sergio. Thank you so much. I always appreciate your help.

      Best,
      Best,
      Pablo Bonilla

      Comment


      • #4
        Sergio Correia --

        I agree with Pablo Bonilla -- a very good solution to a problem that crops up quite frequently!

        Matthew J. Baker

        Comment


        • #5
          Sergio Correia, this solution is brilliant you just solved an extremely recurrent problem for me. Thank you a lot.

          Best!




          Comment


          • #6
            Originally posted by Sergio Correia View Post
            by including, you actually get faster code than if you were to call a compiled Mata file.
            Sergio Correia I wonder why that is. It seems counter-intuitive.

            Comment


            • #7
              Originally posted by Joseph Coveney View Post

              Sergio Correia I wonder why that is. It seems counter-intuitive.
              Here is my guess: When you call a Mata function, Stata first needs to find that function. Stata will probably first try to find the function amongst the functions that are private to an ado-file. It will then look through the built-in libraries and functions and last look through libraries and functions along the ado-path. This search process to find the respective function probably takes longer than the time to compile the source code when the ado-file is loaded. The differences in execution time should be small once the function is identified and loaded into memory.

              Best
              Daniel

              Comment


              • #8
                OK, I see. I understood that Sergio's code called the actual compiled Mata file, and not just invoked the name of the Mata function, which would require that Stata go and find it somewhere floating in the ether.

                Comment

                Working...
                X