Call Fortran function in a TMB R package

69 views
Skip to first unread message

Kinh Nguyen

unread,
Sep 4, 2023, 5:31:58 AM9/4/23
to TMB Users
Hi all, 
I need to evaluate bivariate normal cdf with the fortran code wrote by Alan Genz. Declaring the function from Fortran in C++ model as
extern double F77_NAME(mvbvn)(double *lower, double *upper, int *infin, double *correl);
This compiled OK and can be called with `mvbvn_`.

But putting the model in an R package, there is an error
Error in dyn.load(dll_copy_file) :
  unable to load shared object
symbol not found in flat namespace '__Z6mvbvn_PdS_PiS_'
I think because normally we need to register the fortran function in the R package, such as 
static const R_FortranMethodDef FortranEntries[] = {
    {"mvbvn",  (DL_FUNC) &F77_NAME(mvbvn),  4},
    {NULL, NULL, 0}
};
void R_init_mypackage(DllInfo *dll)
{
    R_registerRoutines(dll, NULL, NULL, FortranEntries, NULL);
    R_useDynamicSymbols(dll, FALSE);
}

But TMB already defined the `TMB_LIB_INIT`, so redefine it in the model code is not allowed.

Do you have suggestion on how to get it work?

Thanks,
Kinh

Kasper Kristensen

unread,
Sep 4, 2023, 6:14:52 AM9/4/23
to TMB Users
I haven't mixed TMB/cpp and Fortran in a package before, but here's a suggestion that might work (inspired by the glmmTMB setup https://github.com/glmmTMB/glmmTMB/blob/master/glmmTMB/src/init.h ):

1. Use 'TMB_CALLDEFS' macro to generate a '.Call' methods table. Use your existing R_init_* function to register both tables:

static const R_FortranMethodDef FortranEntries[] = {
    {"mvbvn",  (DL_FUNC) &F77_NAME(mvbvn),  4},
    {NULL, NULL, 0}
};
const static R_CallMethodDef R_CallDef[] = {
    TMB_CALLDEFS,
    {NULL, NULL, 0}
  };
void R_init_mypackage(DllInfo *dll)
{
    R_registerRoutines(dll, NULL,
R_CallDef, FortranEntries, NULL);
    R_useDynamicSymbols(dll, FALSE);
}

2. I'm guessing you are using TMB::compile to build your package? Don't do that - please use instructions on
but skip the step that tells you to define 'TMB_LIB_INIT'. Use glmmTMB as example if in doubt.

Kinh Nguyen

unread,
Sep 4, 2023, 6:40:23 AM9/4/23
to Kasper Kristensen, TMB Users

Thanks Kasper! it no longer complains about the linking now.
and yes, I did use TMB::compile, I removed it now from the makefile.

Best,
Kinh

--
To post to this group, send email to us...@tmb-project.org. Before posting, please check the wiki and issuetracker at https://github.com/kaskr/adcomp/. Please try to create a simple repeatable example to go with your question (e.g issues 154, 134, 51). Use the issuetracker to report bugs.
---
You received this message because you are subscribed to a topic in the Google Groups "TMB Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tmb-users/8EWqEUW5KkI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tmb-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tmb-users/7c99aaff-9cae-433e-9e27-a188e4653580n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages