registering Fortran routines in R packages

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

registering Fortran routines in R packages

Christophe Dutang1
Dear list,

I’m trying to register Fortran routines in randtoolbox (in srt/init.c file), see https://r-forge.r-project.org/scm/viewvc.php/pkg/randtoolbox/src/init.c?view=markup&root=rmetrics.

Reading https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Registering-native-routines and looking at what is done in stats package, I first thought that the following code will do the job:

static const R_FortranMethodDef FortEntries[] = {
 {"halton", (DL_FUNC) &F77_NAME(HALTON),  7},
 {"sobol", (DL_FUNC) &F77_NAME(SOBOL),  11},
 {NULL, NULL, 0}
};

But I got error messages when building : use of undeclared identifier ‘SOBOL_’. I also tried in lower case sobol and halton.

Looking at expm package https://r-forge.r-project.org/scm/viewvc.php/pkg/src/init.c?view=markup&revision=94&root=expm, I try  

static const R_FortranMethodDef FortEntries[] = {
 {"halton", (DL_FUNC) &F77_SUB(HALTON),  7},
 {"sobol", (DL_FUNC) &F77_SUB(SOBOL),  11},
 {NULL, NULL, 0}
};

But the problem remains the same.

Is there a way to have header file for Fortran codes? how to declare routines defined in my Fortran file src/LowDiscrepancy.f?

Any help appreciated

Regards, Christophe
---------------------------------------
Christophe Dutang
LMM, UdM, Le Mans, France
web: http://dutangc.free.fr

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: registering Fortran routines in R packages

Berend Hasselman

> On 9 May 2017, at 13:44, Christophe Dutang <[hidden email]> wrote:
>
> Dear list,
>
> I’m trying to register Fortran routines in randtoolbox (in srt/init.c file), see https://r-forge.r-project.org/scm/viewvc.php/pkg/randtoolbox/src/init.c?view=markup&root=rmetrics.
>
> Reading https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Registering-native-routines and looking at what is done in stats package, I first thought that the following code will do the job:
>
> static const R_FortranMethodDef FortEntries[] = {
> {"halton", (DL_FUNC) &F77_NAME(HALTON),  7},
> {"sobol", (DL_FUNC) &F77_NAME(SOBOL),  11},
> {NULL, NULL, 0}
> };
>
> But I got error messages when building : use of undeclared identifier ‘SOBOL_’. I also tried in lower case sobol and halton.
>
> Looking at expm package https://r-forge.r-project.org/scm/viewvc.php/pkg/src/init.c?view=markup&revision=94&root=expm, I try  
>
> static const R_FortranMethodDef FortEntries[] = {
> {"halton", (DL_FUNC) &F77_SUB(HALTON),  7},
> {"sobol", (DL_FUNC) &F77_SUB(SOBOL),  11},
> {NULL, NULL, 0}
> };
>
> But the problem remains the same.
>
> Is there a way to have header file for Fortran codes? how to declare routines defined in my Fortran file src/LowDiscrepancy.f?
>

lowercase routine names? manual does mention that.

Berend Hasselman


> Any help appreciated
>
> Regards, Christophe
> ---------------------------------------
> Christophe Dutang
> LMM, UdM, Le Mans, France
> web: http://dutangc.free.fr
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: registering Fortran routines in R packages

Christophe Dutang1
Thanks for your email.

I try to change the name in lowercase but it conflicts with a C implementation also named halton. So I rename the C function halton2() and sobol2() while the Fortran function are HALTON() and SOBOL() (I also try lower case in the Fortran code). Unfortunately, it does not help since I get

init.c:97:25: error: use of undeclared identifier 'halton_'; did you mean 'halton2'?
  {"halton", (DL_FUNC) &F77_SUB(halton),  7},

My current solution is to comment FortEntries array and use R_useDynamicSymbols(dll, TRUE) for a dynamic search of Fortran routines.

Regards, Christophe
---------------------------------------
Christophe Dutang
LMM, UdM, Le Mans, France
web: http://dutangc.free.fr <http://dutangc.free.fr/>

> Le 9 mai 2017 à 14:32, Berend Hasselman <[hidden email]> a écrit :
>
>
>> On 9 May 2017, at 13:44, Christophe Dutang <[hidden email]> wrote:
>>
>> Dear list,
>>
>> I’m trying to register Fortran routines in randtoolbox (in srt/init.c file), see https://r-forge.r-project.org/scm/viewvc.php/pkg/randtoolbox/src/init.c?view=markup&root=rmetrics.
>>
>> Reading https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Registering-native-routines and looking at what is done in stats package, I first thought that the following code will do the job:
>>
>> static const R_FortranMethodDef FortEntries[] = {
>> {"halton", (DL_FUNC) &F77_NAME(HALTON),  7},
>> {"sobol", (DL_FUNC) &F77_NAME(SOBOL),  11},
>> {NULL, NULL, 0}
>> };
>>
>> But I got error messages when building : use of undeclared identifier ‘SOBOL_’. I also tried in lower case sobol and halton.
>>
>> Looking at expm package https://r-forge.r-project.org/scm/viewvc.php/pkg/src/init.c?view=markup&revision=94&root=expm, I try  
>>
>> static const R_FortranMethodDef FortEntries[] = {
>> {"halton", (DL_FUNC) &F77_SUB(HALTON),  7},
>> {"sobol", (DL_FUNC) &F77_SUB(SOBOL),  11},
>> {NULL, NULL, 0}
>> };
>>
>> But the problem remains the same.
>>
>> Is there a way to have header file for Fortran codes? how to declare routines defined in my Fortran file src/LowDiscrepancy.f?
>>
>
> lowercase routine names? manual does mention that.
>
> Berend Hasselman
>
>
>> Any help appreciated
>>
>> Regards, Christophe
>> ---------------------------------------
>> Christophe Dutang
>> LMM, UdM, Le Mans, France
>> web: http://dutangc.free.fr
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: registering Fortran routines in R packages

Berend Hasselman
Christophe,

> On 10 May 2017, at 08:08, Christophe Dutang <[hidden email]> wrote:
>
> Thanks for your email.
>
> I try to change the name in lowercase but it conflicts with a C implementation also named halton. So I rename the C function halton2() and sobol2() while the Fortran function are HALTON() and SOBOL() (I also try lower case in the Fortran code). Unfortunately, it does not help since I get
>
> init.c:97:25: error: use of undeclared identifier 'halton_'; did you mean 'halton2'?
>   {"halton", (DL_FUNC) &F77_SUB(halton),  7},
>
> My current solution is to comment FortEntries array and use R_useDynamicSymbols(dll, TRUE) for a dynamic search of Fortran routines.

Have a look at my package geigen and its init.c.
Could it be that you are missing extern declarations for the Fortran routines?


Berend

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: registering Fortran routines in R packages

Jari Oksanen
Have you tried using tools:::package_native_routine_registration_skeleton()? If you don't like its output, you can easily edit its results and still avoid most pitfalls.

Cheers, Jari Oksanen
________________________________________
From: R-devel <[hidden email]> on behalf of Berend Hasselman <[hidden email]>
Sent: 10 May 2017 09:48
To: Christophe Dutang
Cc: [hidden email]
Subject: Re: [Rd] registering Fortran routines in R packages

Christophe,

> On 10 May 2017, at 08:08, Christophe Dutang <[hidden email]> wrote:
>
> Thanks for your email.
>
> I try to change the name in lowercase but it conflicts with a C implementation also named halton. So I rename the C function halton2() and sobol2() while the Fortran function are HALTON() and SOBOL() (I also try lower case in the Fortran code). Unfortunately, it does not help since I get
>
> init.c:97:25: error: use of undeclared identifier 'halton_'; did you mean 'halton2'?
>   {"halton", (DL_FUNC) &F77_SUB(halton),  7},
>
> My current solution is to comment FortEntries array and use R_useDynamicSymbols(dll, TRUE) for a dynamic search of Fortran routines.

Have a look at my package geigen and its init.c.
Could it be that you are missing extern declarations for the Fortran routines?


Berend

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: registering Fortran routines in R packages

Berend Hasselman
In reply to this post by Berend Hasselman

> On 10 May 2017, at 08:48, Berend Hasselman <[hidden email]> wrote:
>
> Christophe,
>
>> On 10 May 2017, at 08:08, Christophe Dutang <[hidden email]> wrote:
>>
>> Thanks for your email.
>>
>> I try to change the name in lowercase but it conflicts with a C implementation also named halton. So I rename the C function halton2() and sobol2() while the Fortran function are HALTON() and SOBOL() (I also try lower case in the Fortran code). Unfortunately, it does not help since I get
>>
>> init.c:97:25: error: use of undeclared identifier 'halton_'; did you mean 'halton2'?
>>  {"halton", (DL_FUNC) &F77_SUB(halton),  7},
>>
>> My current solution is to comment FortEntries array and use R_useDynamicSymbols(dll, TRUE) for a dynamic search of Fortran routines.
>
> Have a look at my package geigen and its init.c.
> Could it be that you are missing extern declarations for the Fortran routines?

I ran a development version of R some time ago with this

tools:::package_native_routine_registration_skeleton("geigen",con="./geigen_report.txt")

to generate a skeleton init.c. I used that without modification.

Berend

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: registering Fortran routines in R packages

Christophe Dutang1
In reply to this post by Jari Oksanen
Thanks Jari and Berend.

I was not aware of that function. I do use it : indeed it may extern declarations for Fortran routines. see https://r-forge.r-project.org/scm/viewvc.php/pkg/randtoolbox/src/init.c?view=markup&root=rmetrics <https://r-forge.r-project.org/scm/viewvc.php/pkg/randtoolbox/src/init.c?view=markup&root=rmetrics>

However I get a new error when building

Error: package or namespace load failed for ‘randtoolbox’ in dyn.load(file, DLLpath = DLLpath, ...):
 impossible de charger l'objet partagé '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/randtoolbox/libs/randtoolbox.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.4/Resources/library/randtoolbox/libs/randtoolbox.so, 6): Symbol not found: _halton_f_
  Referenced from: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/randtoolbox/libs/randtoolbox.so
  Expected in: flat namespace
 in /Library/Frameworks/R.framework/Versions/3.4/Resources/library/randtoolbox/libs/randtoolbox.so
Erreur : le chargement a échoué
Exécution arrêtée
ERROR: loading failed

Note that I change the name of Fortran routines to halton_f and sobol_f to avoid conflict with C version also renamed halton_c and sobol_c.

In the NAMESPACE, I put at the top useDynLib(randtoolbox, .registration = TRUE).

Berend, I do look at your package… I still don’t figure out why it works for you!

Regards, Christophe

---------------------------------------
Christophe Dutang
LMM, UdM, Le Mans, France
web: http://dutangc.free.fr <http://dutangc.free.fr/>

> Le 10 mai 2017 à 08:56, Jari Oksanen <[hidden email]> a écrit :
>
> Have you tried using tools:::package_native_routine_registration_skeleton()? If you don't like its output, you can easily edit its results and still avoid most pitfalls.
>
> Cheers, Jari Oksanen
> ________________________________________
> From: R-devel <[hidden email]> on behalf of Berend Hasselman <[hidden email]>
> Sent: 10 May 2017 09:48
> To: Christophe Dutang
> Cc: [hidden email]
> Subject: Re: [Rd] registering Fortran routines in R packages
>
> Christophe,
>
>> On 10 May 2017, at 08:08, Christophe Dutang <[hidden email]> wrote:
>>
>> Thanks for your email.
>>
>> I try to change the name in lowercase but it conflicts with a C implementation also named halton. So I rename the C function halton2() and sobol2() while the Fortran function are HALTON() and SOBOL() (I also try lower case in the Fortran code). Unfortunately, it does not help since I get
>>
>> init.c:97:25: error: use of undeclared identifier 'halton_'; did you mean 'halton2'?
>>  {"halton", (DL_FUNC) &F77_SUB(halton),  7},
>>
>> My current solution is to comment FortEntries array and use R_useDynamicSymbols(dll, TRUE) for a dynamic search of Fortran routines.
>
> Have a look at my package geigen and its init.c.
> Could it be that you are missing extern declarations for the Fortran routines?
>
>
> Berend
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel