Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 422 clean standalone functions #459

Merged
merged 8 commits into from Jul 14, 2018

Conversation

sakrejda
Copy link
Contributor

Summary:

Remove most code-grepping from rstan::expose_stan_functions, hide generated code in namespace and call it via wrapper. There are warnings I'm not sure how to get rid of at the moment about get_rng and get_stream, we should fix those before merging.

Intended Effect:

This version of expose_stan_functions works with stan-dev/stan branch
"maintenance/issue-2401-remove-unnecessary-standalone-code-try-2". For
the moment the rng/stream must be passed explicitely, but next step is
to make default global objects. To check it out make a foo.stan like
so:

      functions {
        real f_rng(real mu) {
          real x = normal_rng(mu, 1);
          return x;
        }
      }
Then in R run:
     library(rstan)
     rstan::expose_stan_functions('foo.stan', cacheDir='.')
     o <- get_stream()
     rng <- get_rng(3)
     f_rng(3.3, rng, o)
     f_rng(3.3, rng, o)
     f_rng(3.3, rng, o)
     f_rng(3.3, rng, o)
     f_rng(3.3, rng, o)
Each call with the same RNG should return a new draw.  Re-initializing
the RNG with the same seed should restart the sequence.  Functions that
print will print via Rcpp::rcout.  The whole thing also works without explicitly 
loading the library:
rstan::expose_stan_functions('foo.stan')
f_rng(80.3, rstan::RNG, rstan::OUT)

How to Verify:

Use with the `` stan-dev/stan branch. Make sure the code above runs as expected. Also test a .stan function that prints output via print.

Side Effects:

None?

Documentation:

I don't know where we document expose_stan_functions but we should add documentation about the get_stream and get_rng functions there. Should also doc rstan::RNG and rstan::OUT. I haven't figured out how to inject those as the default arguments to the R wrappers for the exposed Stan functions yet.

Copyright and Licensing

Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company):

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:

…mpile, that should go away; and 2) the RNG must get copied somewhere since it doesn't advance properly (repeat calls with same RNG get same value)... at worst one could create new RNG with random seed every time, at best I could make sure no copies occur.
This version of expose_stan_functions works with stan-dev/stan branch
"maintenance/issue-2401-remove-unnecessary-standalone-code-try-2".  For
the moment the rng/stream must be passed explicitely, but next step is
to make default global objects.  To check it out make a foo.stan like
so:

  functions {
    real f_rng(real mu) {
      real x = normal_rng(mu, 1);
      return x;
    }
  }

Then in R run:

 library(rstan)
 rstan::expose_stan_functions('foo.stan', cacheDir='.')
 o <- get_stream()
 rng <- get_rng(3)
 f_rng(3.3, rng, o)
 f_rng(3.3, rng, o)
 f_rng(3.3, rng, o)
 f_rng(3.3, rng, o)
 f_rng(3.3, rng, o)

Each call with the same RNG should return a new draw.  Re-initializing
the RNG with the same seed should restart the sequence.  Functions that
print will print via Rcpp::rcout.
@sakrejda sakrejda requested a review from bgoodri October 17, 2017 14:12
@stan-buildbot
Copy link

Can one of the admins verify this patch?

@sakrejda
Copy link
Contributor Author

@bgoodri just figured out how to get rid of the warnings about rstan::RNG and rstan::OUT, pushed that.

@sakrejda
Copy link
Contributor Author

Here's the install_SteanHeaders.R for it:


path_rstan <- tempfile(pattern = "git2r-")
git2r::clone("http://github.com/stan-dev/rstan", path_rstan, branch = "feature/issue-422-standalone-functions-k")
git2r::clone("http://github.com/stan-dev/stan", 
             file.path(path_rstan, "StanHeaders", "inst", "include", "upstream"), 
             branch = "maintenance/issue-2401-remove-unnecessary-standalone-code-try-2") # may want to change this branch
git2r::clone("http://github.com/stan-dev/math", 
             file.path(path_rstan, "StanHeaders", "inst", "include", "mathlib"), 
             branch = "develop") # may want to change this branch
devtools::install(file.path(path_rstan, "StanHeaders"), args = "--preclean")

@ariddell
Copy link

Is this going in 2.18?

@bgoodri
Copy link
Contributor

bgoodri commented Feb 18, 2018 via email

@sakrejda
Copy link
Contributor Author

No idea if this already made it in or not but if you need help finalizing it ping me. @bgoodri

@bgoodri bgoodri merged commit 4399d92 into develop Jul 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants