INTERFACE R/C
Alicia STOTZ
Statistique et G´enome, Universit´e d’Evry (Car`ene Rizzon) Origine, structure et ´evolution de la biodiversit´e, MNHN (J´erˆome Sueur)
31 Mai 2012
Projet
Package SEEWAVE d´ evelopp´ e au sein du MNHN, par J´ erˆ ome Sueur, Thierry Aubin et Caroline Simonis.
→
Environ cent fonctions qui permettent la synth` ese et l’analyse de diff´ erents types de sons.
BUT:
R´ ealiser les calculs en ext´ erieur de l’environnement R R´ ecup´ erer les r´ esultats dans R
→
Temps de calcul am´ elior´ e
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 2 / 32
Sommaire
1 Introduction
Le language C Utilit´ e
Les librairies C
Les diff´ erentes fonctions d’appel
2
Fonction .C
3
Fonction .Call
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 3 / 32
Sommaire
1 Introduction
Le language C Utilit´ e
Les librairies C
Les diff´ erentes fonctions d’appel
2
Fonction .C
3
Fonction .Call
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 4 / 32
Le language C
Qu’est-ce que c’est ???
Le plus c´ el` ebre et le plus utilis´ e Language de bas niveau
Contient beaucoup de types Language imp´ eratif
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 5 / 32
Le language C
Qu’est-ce que c’est ???
Le plus c´ el` ebre et le plus utilis´ e
Language de bas niveau Contient beaucoup de types Language imp´ eratif
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 5 / 32
Le language C
Qu’est-ce que c’est ???
Le plus c´ el` ebre et le plus utilis´ e Language de bas niveau
Contient beaucoup de types Language imp´ eratif
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 5 / 32
Le language C
Qu’est-ce que c’est ???
Le plus c´ el` ebre et le plus utilis´ e Language de bas niveau
Contient beaucoup de types
Language imp´ eratif
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 5 / 32
Le language C
Qu’est-ce que c’est ???
Le plus c´ el` ebre et le plus utilis´ e Language de bas niveau
Contient beaucoup de types Language imp´ eratif
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 5 / 32
Sommaire
1 Introduction
Le language C Utilit´ e
Les librairies C
Les diff´ erentes fonctions d’appel
2
Fonction .C
3
Fonction .Call
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 6 / 32
Pourquoi avoir recours ` a une interface R/C
Utiliser les librairies pr´ esentes dans C
Am´ eliorer le temps de calcul par rapport ` a R sur des donn´ ees importantes (> Mo)
Gestion efficace de la m´ emoire
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 7 / 32
Pourquoi avoir recours ` a une interface R/C
Utiliser les librairies pr´ esentes dans C
Am´ eliorer le temps de calcul par rapport ` a R sur des donn´ ees importantes (> Mo)
Gestion efficace de la m´ emoire
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 7 / 32
Pourquoi avoir recours ` a une interface R/C
Utiliser les librairies pr´ esentes dans C
Am´ eliorer le temps de calcul par rapport ` a R sur des donn´ ees importantes (> Mo)
Gestion efficace de la m´ emoire
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 7 / 32
Pourquoi avoir recours ` a une interface R/C
Utiliser les librairies pr´ esentes dans C
Am´ eliorer le temps de calcul par rapport ` a R sur des donn´ ees importantes (> Mo)
Gestion efficace de la m´ emoire
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 7 / 32
Sommaire
1 Introduction
Le language C Utilit´ e
Les librairies C
Les diff´ erentes fonctions d’appel
2
Fonction .C
3
Fonction .Call
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 8 / 32
Les librairies n´ ecessaires
# i n c l u d e <R . h>
# i n c l u d e <R i n t e r n a l s . h>
# i n c l u d e <Rmath . h>
# i n c l u d e <R d e f i n e s . h>
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 9 / 32
Sommaire
1 Introduction
Le language C Utilit´ e
Les librairies C
Les diff´ erentes fonctions d’appel
2
Fonction .C
3
Fonction .Call
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 10 / 32
Fonction d’appel possibles dans R
Fonction .C
Utilis´ ee pour des arguments ”primitifs” (vector, double, integer)
Fonction .Call
Utilis´ ee pour des arguments plus complexes telles que les listes ou les matrices
Exemple :
> ? . C a l l
. C a l l ( . NAME, . . . , PACKAGE)
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 11 / 32
Sommaire
1
Introduction
2 Fonction .C
3
Fonction .Call
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 12 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .C
Petit exemple: Hello World !!!
# i n c l u d e <R . h>
v o i d h e l l o (i n t ∗n ) {
i n t i ;
f o r ( i = 0 ; i < ∗n ; i ++) {
p r i n t f (" hello World !\ n") ; }
}
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 13 / 32
Les pointeurs
Figure: M´emoire, adresses et pointeurs (site du zero - languageC)
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 14 / 32
Fonction .C
La compilation
R CMD SHLIB nomprogramme.c
> R CMD SHLIB helloC.c
Dans le terminal:
gcc -std=gnu99 -I/usr/local/R-2.15.0/lib64/R/include -DNDEBUG -I/usr/local/include -fpic -g -O2 -c helloC.c -o helloC.o
gcc -std=gnu99 -shared -L/usr/local/lib64 -o helloC.so helloC.o -L/usr/local/R-2.15.0/lib64/R/lib -lR
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 15 / 32
Appel fonction .C
Et dans R...
>dyn.load("helloC.so")
>source("Rcode.R")
Le script R:
h e l l o R <− f u n c t i o n ( n ) {
. C(" hello ", a s . i n t e g e r ( n ) ) }
Sortie R:
> helloR(3)
Hello world !!
Hello world !!
Hello world !!
[[1]]
[1] 3
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 16 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .C
Les diff´erents types d’objets
# i n c l u d e <R . h>
v o i d t y p e ( double ∗d , c h a r ∗∗c , i n t ∗l ) {
d [ 0 ] = 2 5 . 5 6 ;\ \Numerique c [ 1 ] = "z"; \\c h a r a c t e r e
l [ 0 ] = 0 ; \\l o g i q u e }
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 17 / 32
Appel fonction .C
Les diff´erents types d’objets (suite)
> dyn . l o a d (" typeC . so ")
> d <− s e q ( l e n g t h =3 , f r o m = 1 , t o = 2 )
> c <− c ("a", "b", "c")
> l <− c (" TRUE ", " FALSE ")
> o u t <− . C(" type ", d1 = a s . n u m e r i c ( d ) ,
c1 = a s . c h a r a c t e r ( c ) , l 1 = a s . l o g i c a l ( l ) )
> o u t d1
[ 1 ] 2 5 . 5 6 1 . 5 0 2 . 0 0 c1
[ 1 ] "a" "z" "c"
l 1
[ 1 ] FALSE FALSE
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 18 / 32
Sommaire
1
Introduction
2
Fonction .C
3 Fonction .Call
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 19 / 32
.Call version am´ elior´ ee du .C
Avantages
Passer un objet R dans C Cr´ eer un objet R dans C Manipuler un objet R dans C Retourner un objet R de C Appeler une fonction R de C Inconv´ enient
Plus difficile ` a manipuler et ` a utiliser
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 20 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Exemple fonction moyenne
SEXP moyenne ( SEXP Rvec ) {
i n t i , t a i l l e V e c t e u r = 0 ; double ∗v e c = NULL ;
double v a l e u r = 0 ;
Rvec = c o e r c e V e c t o r ( Rvec , REALSXP ) ; v e c = REAL ( Rvec ) ;
t a i l l e V e c t e u r = l e n g t h ( Rvec ) ;
f o r ( i = 0 ; i < t a i l l e V e c t e u r ; i ++) {
v a l e u r = v a l e u r + v e c [ i ] ; }
p r i n t f (" la moyenne est egale a : %f\n", v a l e u r / t a i l l e V e c t e u r ) ;
r e t u r n ( R N i l V a l u e ) ; }
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 21 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Explications...
SEXP
est une structure d´ efinie dans R. Repr´ esente des simples expressions. L’appel d’une fonction avec .Call va retourner un SEXP.
Ici la fonction ne retourne rien:
returnR NilValueElle prend en argument un vecteur de type R´ eel:
Rvec = coerceVector (Rvec, REALSXP)Pour pouvoir le manipuler: cr´ eation d’un pointeur qui cible notre vecteur (
vec = REAL(Rvec);On parcours juste le vecteur et on calcule la moyenne:
f o r ( i = 0 ; i < t a i l l e V e c t e u r ; i ++) {v a l e u r = v a l e u r + v e c [ i ] ; }
p r i n t f (" la moyenne est egale a : %f\n", v a l e u r / t a i l l e V e c t e u r ) ;
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 22 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Explications...
SEXP
est une structure d´ efinie dans R. Repr´ esente des simples expressions. L’appel d’une fonction avec .Call va retourner un SEXP.
Ici la fonction ne retourne rien:
returnR NilValueElle prend en argument un vecteur de type R´ eel:
Rvec = coerceVector (Rvec, REALSXP)Pour pouvoir le manipuler: cr´ eation d’un pointeur qui cible notre vecteur (
vec = REAL(Rvec);On parcours juste le vecteur et on calcule la moyenne:
f o r ( i = 0 ; i < t a i l l e V e c t e u r ; i ++) {v a l e u r = v a l e u r + v e c [ i ] ; }
p r i n t f (" la moyenne est egale a : %f\n", v a l e u r / t a i l l e V e c t e u r ) ;
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 22 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Explications...
SEXP
est une structure d´ efinie dans R. Repr´ esente des simples expressions. L’appel d’une fonction avec .Call va retourner un SEXP.
Ici la fonction ne retourne rien:
returnR NilValueElle prend en argument un vecteur de type R´ eel:
Rvec = coerceVector (Rvec, REALSXP)
Pour pouvoir le manipuler: cr´ eation d’un pointeur qui cible notre vecteur (
vec = REAL(Rvec);On parcours juste le vecteur et on calcule la moyenne:
f o r ( i = 0 ; i < t a i l l e V e c t e u r ; i ++) {v a l e u r = v a l e u r + v e c [ i ] ; }
p r i n t f (" la moyenne est egale a : %f\n", v a l e u r / t a i l l e V e c t e u r ) ;
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 22 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Explications...
SEXP
est une structure d´ efinie dans R. Repr´ esente des simples expressions. L’appel d’une fonction avec .Call va retourner un SEXP.
Ici la fonction ne retourne rien:
returnR NilValueElle prend en argument un vecteur de type R´ eel:
Rvec = coerceVector (Rvec, REALSXP)
Pour pouvoir le manipuler: cr´ eation d’un pointeur qui cible notre vecteur (
vec = REAL(Rvec);On parcours juste le vecteur et on calcule la moyenne:
f o r ( i = 0 ; i < t a i l l e V e c t e u r ; i ++) {v a l e u r = v a l e u r + v e c [ i ] ; }
p r i n t f (" la moyenne est egale a : %f\n", v a l e u r / t a i l l e V e c t e u r ) ;
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 22 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Explications...
SEXP
est une structure d´ efinie dans R. Repr´ esente des simples expressions. L’appel d’une fonction avec .Call va retourner un SEXP.
Ici la fonction ne retourne rien:
returnR NilValueElle prend en argument un vecteur de type R´ eel:
Rvec = coerceVector (Rvec, REALSXP)
Pour pouvoir le manipuler: cr´ eation d’un pointeur qui cible notre vecteur (
vec = REAL(Rvec);On parcours juste le vecteur et on calcule la moyenne:
f o r ( i = 0 ; i < t a i l l e V e c t e u r ; i ++) {
v a l e u r = v a l e u r + v e c [ i ] ; }
p r i n t f (" la moyenne est egale a : %f\n", v a l e u r / t a i l l e V e c t e u r ) ;
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 22 / 32
Appel fonction .Call
La compilation, utilisation
> l i b r a r y (MASS)
> d a t a ( c r a b s )
> crabsRW<− c r a b s [ , 4 ]
> dyn . l o a d (" meanNul . so ")
> . C a l l (" moyenne ", crabsRW )
l a moyenne e s t e g a l e a : 1 2 . 7 3 8 5 0 0 NULL
La valeur de retour est nul, on ne peut pas la manipuler mais...
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 23 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
... Et avec une valeur de retour ?
SEXP Rmoyenne ;
double ∗p o i n t e u r R m o y e n n e = NULL ;
PROTECT ( Rmoyenne = a l l o c V e c t o r ( REALSXP , 1 ) ) ; p o i n t e u r R m o y e n n e = REAL ( Rmoyenne ) ;
∗p o i n t e u r R m o y e n n e = v a l e u r / t a i l l e V e c t e u r ; UNPROTECT ( 1 ) ;
r e t u r n ( Rmoyenne ) ;
Dans R:
> dyn . l o a d (" mean . so ")
> moy <− . C a l l (" moyenne ", crabsRW ) l a moyenne e s t e g a l e a : 1 2 . 7 3 8 5 0 0
> moy
[ 1 ] 1 2 . 7 3 8 5
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 24 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Les matrices
Matrice en entr´ ee
double ∗m a t r i x p t rPROTECT(MATRIX = c o e r c e V e c t o r (MATRIX , REALSXP ) ; m a t r i x p t r = REAL (MATRIX ) ;
Matrice en sortie
SEXP m a t r i c e C ( SEXP MATRIX){ SEXP d i m M a t r i x ;
d i m M a t r i x = g e t A t t r i b (MATRIX , R DimSymbol ) ; nrow = INTEGER ( d i m M a t r i x ) [ 0 ] ;
n c o l = INTEGER ( d i m M a t r i x ) [ 1 ] ;
PROTECT( o u t M a t r i x = a l l o c M a t r i x ( REALSXP , nrow , n c o l ) ) ;
o u t m a t r i x p t r = REAL ( o u t M a t r i x ) ;
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 25 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Les matrices
Matrice en entr´ ee
double ∗m a t r i x p t rPROTECT(MATRIX = c o e r c e V e c t o r (MATRIX , REALSXP ) ; m a t r i x p t r = REAL (MATRIX ) ;
Matrice en sortie
SEXP m a t r i c e C ( SEXP MATRIX){ SEXP d i m M a t r i x ;
d i m M a t r i x = g e t A t t r i b (MATRIX , R DimSymbol ) ; nrow = INTEGER ( d i m M a t r i x ) [ 0 ] ;
n c o l = INTEGER ( d i m M a t r i x ) [ 1 ] ;
PROTECT( o u t M a t r i x = a l l o c M a t r i x ( REALSXP , nrow , n c o l ) ) ;
o u t m a t r i x p t r = REAL ( o u t M a t r i x ) ;
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 25 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Les matrices
Matrice en entr´ ee
double ∗m a t r i x p t rPROTECT(MATRIX = c o e r c e V e c t o r (MATRIX , REALSXP ) ; m a t r i x p t r = REAL (MATRIX ) ;
Matrice en sortie
SEXP m a t r i c e C ( SEXP MATRIX){
SEXP d i m M a t r i x ;
d i m M a t r i x = g e t A t t r i b (MATRIX , R DimSymbol ) ; nrow = INTEGER ( d i m M a t r i x ) [ 0 ] ;
n c o l = INTEGER ( d i m M a t r i x ) [ 1 ] ;
PROTECT( o u t M a t r i x = a l l o c M a t r i x ( REALSXP , nrow , n c o l ) ) ;
o u t m a t r i x p t r = REAL ( o u t M a t r i x ) ;
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 25 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Les matrices (suite)
Remplissage d’une matrice
f o r ( j = 0 ; j < n c o l ; j ++) {
f o r ( i = 0 ; i < nrow ; i ++) {
o u t m a t r i x p t r [ i + nrow ∗ j ] = 20 ∗ m a t r i x p t r [ i + nrow ∗ j ] ;
} }
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 26 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Les matrices (suite)
Remplissage d’une matrice
f o r ( j = 0 ; j < n c o l ; j ++) {
f o r ( i = 0 ; i < nrow ; i ++) {
o u t m a t r i x p t r [ i + nrow ∗ j ] = 20 ∗ m a t r i x p t r [ i + nrow ∗ j ] ;
} }
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 26 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Les listes
SEXP r e t L i s t ; SEXP names ;
PROTECT( r e t L i s t = a l l o c V e c t o r (VECSXP , 2 ) ) ; PROTECT( names = a l l o c V e c t o r (STRSXP , 2 ) ) ;
SET STRING ELT ( names , 0 , mkChar (" vec ") ) ; SET STRING ELT ( names , 1 , mkChar (" matrix ") ) ;
s e t A t t r i b ( r e t L i s t , R NamesSymbol , names ) ;
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 27 / 32
Programme C pouvant ˆ etre appel´ e par la fonction .Call
Les listes (suite)
PROTECT( m a t r i x = a l l o c M a t r i x ( REALSXP , nrow , n c o l ) ) ; m a t r i x p t r = REAL ( m a t r i x ) ;
PROTECT ( v e c = a l l o c V e c t o r ( REALSXP , nrow ) ) ; v e c p t r = REAL ( v e c ) ;
SET VECTOR ELT ( L i s t , 0 , v e c ) ; SET VECTOR ELT ( L i s t , 1 , m a t r i x ) ; r e t u r n r e t L i s t ;
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 28 / 32
Appel fonction .Call
Les listes: exemple
> dyn.load("linemean.so")
> vecteur <- .Call("mean",matrice)
> vecteur$vec [1] 2 3
> vecteur$matrix [,1] [,2]
[1,] 1 3
[2,] 2 4
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 29 / 32
Int´ erˆ et de l’interface R/C
Appel des librairies
Le code C:
# i n c l u d e <s t d i o . h>
# i n c l u d e <s t d l i b . h>
# i n c l u d e <f f t w 3 h . h>
# i n c l u d e <s n d f i l e . h>
La compilation :
R CMD SHLIB fftwR.c -lsndfile -lfftw3
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 30 / 32
Int´ erˆ et de l’interface R/C
Figure: Diff´erence dans le temps d’ex´ecution entre R et C pour le calcul de la STFT
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 31 / 32
R´ ef´ erences
Image pointeur, site du zero - language C
An Introduction to the .C Interface to R (Roger D.Peng et Jan de Leeuw, Aout 2008)
R/Cpp: Interface Classes to simplify Using R Object in C++ (J.J Java, D.P Gaile et K.F Manly, juillet 2007)
Calling C function from R using .C and .Call (S. hojsgaard, janvier 2010)
Calling C code from R, an introduction (S.Blay, octobre 2004) Using .Call in R (B. Caffo)
Merci...
Alicia STOTZ (Universit´e d’Evry) INTERFACE R/C 31 Mai 2012 32 / 32