From murdoch.duncan at gmail.com  Fri Mar 16 13:06:28 2012
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Fri, 16 Mar 2012 08:06:28 -0400
Subject: [Rd] Segfault while calling fexact in C
In-Reply-To: <CAPpMX7Ro2aspvKUP2yOWXUzJ9XedLKs8kmeB8A_-Zhb057deUg@mail.gmail.com>
References: <CAPpMX7Ro2aspvKUP2yOWXUzJ9XedLKs8kmeB8A_-Zhb057deUg@mail.gmail.com>
Message-ID: <4F632CC4.3040601@gmail.com>

On 12-03-16 5:27 AM, Nima Mohammadi wrote:
> Hi folks,
> I'm trying to call an R function (fisher.test) in my program for like a
> billion times! Though my program is in Python and I feel that using rpy2 to
> interface R to python doesn't give me satisfactory performance. So I looked
> into R code and found out that fisher.test is actually a wrapper around
> another function called fexact which is implemented in C. Using Cython I
> managed to directly call the function and get the desired results.
> But the weird problem is that after calling the function for like 4000
> times (with same parameters) a segmentation fault occurs! Eliminating the
> possibility that my interfacing code has any problem, I wrote a piece of
> code in C to call the fexact multiple times:

I don't think fexact is mentioned in Rmath.h, so it's not a function 
designed to be called from a standalone program.

Duncan Murdoch

>
> int main(){
>      int nr = 3, nc = 2;
>      int x[6] = {3, 1, 5, 1, 3, 6};
>      double expect = -1, percnt = 100, emin = 0, prt = 1;
>      double p;
>      int workspace = 200000, mult = 30;
>      int i;
>      for (i=0; i<100000; i++){
>          fexact(&nr,&nc, x,&nr,&expect,
>              &percnt,&emin,&prt,&p,&workspace,&mult);
>          printf("%d %f\n", i, p);
>      }
>      return 0;
> }
>
> I statically linked the program to libRmath, which I don't suppose has
> anything to do with problem. I also dynamically linked to libRmath.so which
> yielded the same problem.
>
> nima at nima-laptop:~/BioProject/fet$ gcc -I/usr/share/R/include -lm fexact.c
> /usr/lib/libRmath.a
>
> nima at nima-laptop:~/BioProject/fet$ ./a.out
> 0 0.408431
> 1 0.408431
> 2 0.408431
> 3 0.408431
> 4 0.408431
> ....
> ....
> 4003 0.408431
> 4004 0.408431
> 4005 0.408431
> Segmentation fault
>
> Any help would be highly appreciated :)
>
> *-- **Nima Mohammadi*
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Segfault while calling fexact in C
Duncan Murdoch