I experience a crash of R if I call a C function (from R) repeatedly.
I am a newbie in writing C functions and I think that the issue of a R crash I am experiencing is the memory allocation in C. I know that one can free the memory in C, but I cant figure out how exactly and on which parameters. Can anybody help me out here?
I will post a working example here and my issue/question again at the end.
This is the C-Code (credits: the code is based on the code of the mcplr package by Marteen Speeklenbrink ( https://github.com/rforge/mcplr ).
#include <Rmath.h>
void gcmval(int *y, int *ny, double *x, int *nx, double *v, double *p, int *bt, int *et, double *w, double *r, double *q, double *lambda, double *theta, double *dist, double *sim, double *val, double *vpred, double *ypred)
{
double syp = 0.0;
double svp = 0.0;
for(int t=*bt; t < *et; t++) {
for(int j=0; j < *ny; j++) sim[j] = 0.0;
svp = 0.0;
for(int tt=*bt-1; tt < t; tt++) {
dist[tt] = 0.0;
for(int i=0; i < *nx; i++) {
dist[tt] += w[i] * pow( fabs(x[i + tt * *nx] - x[i + t * *nx ]), *r);
}
dist[tt] = pow(dist[tt], *q / *r);
val[tt] = exp(-1 * *lambda *dist[tt]) * v[tt];
for(int j=0; j < *ny; j++) {
if(y[j + tt * *ny] == 1) sim[j] += exp(-1 * *lambda * dist[tt]);
}
svp += val[tt];
}
syp = 0.0;
for(int j=0; j < *ny; j++) {
syp += sim[j];
}
vpred[t] = svp / syp;
ypred[t] = 1.0 / (1.0 + exp(-1 * *theta * (vpred[t] - p[t])));
}
}
R Code Here is a working example that uses the function we just defined in C in R. If I call the call to .C repeatedly, R will crash (on Windows 10). I need to call it repeatedly, and I am not sure how to do the memor allication.
if(.Platform$OS.type == "windows"){
dyn.load("gcmval.dll")
} else if(.Platform$OS.type %in% c("mac","unix")){
dyn.load("gcmval.so")
}
# Some fake input data
dt <- data.table(f1 = c(1,1,0,0,1,0),
f2 = c(1,1,0,0,1,0),
f3 = c(1,1,0,0,0,1),
choices = c(0,0,1,1,0,1),
prices = c(1,1,3,3,2,2),
values = c(.9,.9,3.1,3.1,1.5,2.5))
# Next lines just prepare the variables for the C-function
y <- rbind(t(dt$choices), 1-t(dt$choices))
x <- t(dt[,1:3])
w <- rep(1/3,3)
v <- dt$values
p <- dt$prices
nx <- nrow(x)
ny <- nrow(y)
nt <- ncol(x)
bt <- 1
et <- nt
lambda <- 1
theta <- 1
r <- 1
q <- 1
pv <- vector("double", length = nt)
py <- vector("double", length = nt)
val <- vector("double",length=nt)
dist <- vector("double",length=nt)
sim <- vector("double",length=ny)
val <- vector("double",length=ny)
# Run this code repeatedly, it crashes R
tmp <- .C("gcmval",
y=as.integer(y),
ny=as.integer(ny),
x=as.double(x),
nx=as.integer(nx),
v=as.double(v),
p=as.double(p),
bt=as.integer(bt),
et=as.integer(et),
w=as.double(w),
r=as.double(r),
q=as.double(q),
lambda=as.double(lambda),
theta=as.double(theta),
dist=as.double(dist),
sim=as.double(sim),
val=as.double(val),
vpred=as.double(pv),
ypred=as.double(py)
)
I think R_alloc is my friend, but I have trouble applying this.
I would be super happy if somebody could help me with a general introduction/explanation of how exactly to use the R_alloc function (there are some cryptic examples in the writing r extensions tutorial but I could not parse them), or any general explanation of how the memory allocation works (for somebody who knows R well but is a self-taught programmer).
Thanks for your help in advance /Jana
The problem is that you are defining the vector val
twice:
val <- vector("double",length=nt)
dist <- vector("double",length=nt)
sim <- vector("double",length=ny)
val <- vector("double",length=ny)
In the second definition the length is not correct since val
has to have the same length as dist
, not as sim
. If I remove the second definition, the crash disappears.
I find it interesting that the crash is triggered only after repeated calls. I can trigger it right away by calling tmp
or gc()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.