Questions/concerns regarding external C++ functionality within RStan

Hey all,

We had a couple of questions regarding the external C++ functionality within RStan. Specifically, we want to determine the feasibility of calling an external static library or calling an external C++ header file with multiple functions from RStan. Additionally, we wanted to address a concern we encountered regarding using more than 4 parameters in our external C++ header file.

First, we would like to address the concern of using multiple variables within an external C++ file. When taking in more than 4 parameters using our external C++ header file, we run into the same errors that we encountered when trying to use PyStan. We have not tried ways to mitigate this issue such as packing/unpacking the variables using a tuple or simply limiting the parameters input from the RStan side of things. We simply wanted to note that it was an issue we encountered when attempting to fit the external C++ functionality of RStan to our problem.

Second, we would like to ask for advice regarding the use of an external function that is located within a static library (.a) file. When attempting to use a function within an external static library with the use of a C++ header file, we are unable to produce a functional Stan model. Unfortunately, we are unable to post the output from R when attempting to send data from RStan to an external C++ header file, which included and used a function from a static object. When we use the verbose argument, it shows 187 warnings and 15 errors, with most the warnings containing an excessive amount of machine code.

When attempting to bypass the C++ header file entirely and simply send the data directly to the static object file like so…

loadedStanModel <- stan_model(model_code = stanModel, allow_undefined = TRUE,  verbose = TRUE,
                              includes = paste0('\n#include "', 
                                               file.path(getwd(), 'parabolaObject.a'), '"\n'))

… R produces the same output. As such, our first question: is it possible to use an external static library when using the external C++ functionality of RStan? Moreover, is it possible to use more than one external C++ file or object with RStan?

Third, in attempt to break down the problem of using an external static library with RStan, we simply included an additional function within the already working external C++ header file and tried using that. We simply broke down the one working function into two functions that theoretically accomplish the same task. One function would simply pass parameters initially input into the C++ header file to another function, which would return a result. However, this did not work.

Error in compileCode(f, code, language = language, verbose = verbose) : 
Compilation ERROR, function(s)/method(s) not created! In file included from file5f2481e63e6.cpp:8: 
17 warnings and 11 errors generated. 
make: *** [file5f2481e63e6.o] Error 1

Our second question: is it possible to use multiple functions within a single external C++ file in conjunction with RStan?

Thank you for any assistance you can offer.

I would think so, but you would have to modify ~/.R/Makevars or the PKG_LIBS environmental variable to do the linking. Also, the external static library would have to be templated in order to accept Stan types.

Yes, includes can be of any length.

@bgoodri,

Thank you for responding so quickly, and thank you for providing insight in regard to those questions.

We experimented with linking the files, but attempted to do so by modifying the makeconfig file used by R instead. The makeconfig file was found by using the makeconf_path() function call.

Furthermore, we also assumed we would have to template the static library in a similar fashion to the external C++ header file that we had discussed prior. However, the errors we ran into did not differ if the static library was or was not templated in the appropriate fashion. Regardless, templating the entire static library we are attempting to link would not be plausible for the solution we are attempting to incorporate.

Would you be able to answer the second question in the initial post? We initially assumed the answer to be yes; however, after attempting to use multiple functions, we continuously ran into errors.

Best,
Matt

Yeah, you just need to declare multiple functions in the functions block of your Stan program. But I am not understanding how, even if you get it to link properly, you are going to use it within Stan if the external library doesn’t have templates? It is going to get passed something with a var and not have a signature for that.

I apologize, I worded that sentence incorrectly. The static library does include templates; however, modifying those templates to accommodate Stan doesn’t seem plausible within the timeframe we have left.