Skip to content

The following code (using rgamma) hangs

3 messages · Dirk Eddelbuettel, Faheem Mitha

#
Hi,

I'm seeing something that may be a bug in R's standalone math library, 
which is packaged by Debian as r-mathlib. I reported it to the Debian BTS 
as http://bugs.debian.org/657573

I'm using Debian squeeze, and the code was tested with r-mathlib 2.11.1-6 
(default on stable) and 2.14.1-1 (from testing/unstable).

I summarize this report below. The following code with the R math library 
hangs. Note that set_seed is defined as taking two unsigned int arguments, 
so 0's are valid arguments. I'm guessing that since 0 is as low as an 
unsigned integer can go, it represents some kind of edge case.

################################################
#define MATHLIB_STANDALONE
#include <Rmath.h>

int main(void)
{
   set_seed(0, 0);
   rgamma(1, 1);
}
################################################

If I add the definitions of `get_seed` and `set_seed` from 
`src/nmath/standalone/sunif.c` to my code, then the hang disappears.

################################################
#define MATHLIB_STANDALONE
#include <Rmath.h>

/* A version of Marsaglia-MultiCarry */

static unsigned int I1=1234, I2=5678;

void set_seed(unsigned int i1, unsigned int i2)
{
     I1 = i1; I2 = i2;
}

void get_seed(unsigned int *i1, unsigned int *i2)
{
     *i1 = I1; *i2 = I2;
}

int main(void)
{
   set_seed(0, 0);
   rgamma(1, 1);
}
################################################

I assume sunif.c defines the `get_seed` and `set_seed` for the R 
standalone random number generation facilities.

However, I wonder why

a) redefining them in my source file makes the hang go away

and

b) why doesn't redefining `get_seed` and `set_seed` (even with the same 
definition) give a linker error, since the function has been defined in 
the library already?

Dirk also pointed out (in the bug report) that you get the following

##########################################################
int main(void)
{
     set_seed(0, 0);
     cout << "one normal " << norm_rand() << endl;
}
##########################################################

edd at max:/tmp$ g++ -o faheem faheem.cpp -lRmath; ./faheem
one normal -inf

One would expect norm_rand to return finite values, even in edge cases.

If you want me to report this as a bug, let me know. Thanks.

                                                        Regards, Faheem
#
On 28 January 2012 at 02:33, Faheem Mitha wrote:
| 
| Hi,
| 
| I'm seeing something that may be a bug in R's standalone math library, 
| which is packaged by Debian as r-mathlib. I reported it to the Debian BTS 
| as http://bugs.debian.org/657573
| 
| I'm using Debian squeeze, and the code was tested with r-mathlib 2.11.1-6 
| (default on stable) and 2.14.1-1 (from testing/unstable).
| 
| I summarize this report below. The following code with the R math library 
| hangs. Note that set_seed is defined as taking two unsigned int arguments, 
| so 0's are valid arguments. I'm guessing that since 0 is as low as an 
| unsigned integer can go, it represents some kind of edge case.
| 
| ################################################
| #define MATHLIB_STANDALONE
| #include <Rmath.h>
| 
| int main(void)
| {
|    set_seed(0, 0);
|    rgamma(1, 1);
| }
| ################################################
| 
| If I add the definitions of `get_seed` and `set_seed` from 
| `src/nmath/standalone/sunif.c` to my code, then the hang disappears.
| 
| ################################################
| #define MATHLIB_STANDALONE
| #include <Rmath.h>
| 
| /* A version of Marsaglia-MultiCarry */
| 
| static unsigned int I1=1234, I2=5678;
| 
| void set_seed(unsigned int i1, unsigned int i2)
| {
|      I1 = i1; I2 = i2;
| }
| 
| void get_seed(unsigned int *i1, unsigned int *i2)
| {
|      *i1 = I1; *i2 = I2;
| }
| 
| int main(void)
| {
|    set_seed(0, 0);
|    rgamma(1, 1);
| }
| ################################################
| 
| I assume sunif.c defines the `get_seed` and `set_seed` for the R 
| standalone random number generation facilities.
| 
| However, I wonder why
| 
| a) redefining them in my source file makes the hang go away
| 
| and
| 
| b) why doesn't redefining `get_seed` and `set_seed` (even with the same 
| definition) give a linker error, since the function has been defined in 
| the library already?
| 
| Dirk also pointed out (in the bug report) that you get the following
| 
| ##########################################################
| int main(void)
| {
|      set_seed(0, 0);
|      cout << "one normal " << norm_rand() << endl;
| }
| ##########################################################
| 
| edd at max:/tmp$ g++ -o faheem faheem.cpp -lRmath; ./faheem
| one normal -inf

Well I actually sent you a complete program of which you showed only an
incomplete part.  A better quote would have shown all:

  #define MATHLIB_STANDALONE
  #include <Rmath.h>
  #include <iostream>
  using std::cout;
  using std::endl;

  int main(void) {
      set_seed(0, 0);
      cout << "one normal " << norm_rand() << endl;
  }


That does indeed return -Inf on my Ubuntu server.  It works with other seed
values as does the rgamma which hangs only for value 0 and 0.

Dirk
 
| One would expect norm_rand to return finite values, even in edge cases.
| 
| If you want me to report this as a bug, let me know. Thanks.
| 
|                                                         Regards, Faheem
| 
| ______________________________________________
| R-devel at r-project.org mailing list
| https://stat.ethz.ch/mailman/listinfo/r-devel
#
On Fri, 27 Jan 2012, Dirk Eddelbuettel wrote:

            
Yes, apologies for not including the complete code.

Btw, adding the definitions of `get_seed` and `set_seed` from
| `src/nmath/standalone/sunif.c` fixes the problem here as well.

faheem at orwell[default branch:rev 12]:~/corrmodel/bug$ ./edd one normal 
-1.26974

where previously it was giving -inf.
                                                        Regards, Faheem