Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using GSL's random number generation function leads to JVM crash #636

Closed
GongYiLiao opened this issue Oct 20, 2018 · 2 comments
Closed

Using GSL's random number generation function leads to JVM crash #636

GongYiLiao opened this issue Oct 20, 2018 · 2 comments

Comments

@GongYiLiao
Copy link

GongYiLiao commented Oct 20, 2018

I tried the following Scala code :

    import org.bytedeco.javacpp._
    import org.bytedeco.javacpp.gsl._
       
    object JavaCppGSLRnormTest {
       def rZigguratGSL(n: Int): Array[Double] = {
          var r0 = new gsl_rng()
          Array.fill[Double](n)(gsl_ran_gaussian_ziggurat(r0, 1.0))
       }
    }

the call the random number generating function via JavaCppGSLRnormTest.rZigguratGSL(30) caused JVM (OpenJDK 8, 10, 11 and GraalVM 1.0rc7 have been tested) crashed:

# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f42782971bf, pid=19603, tid=19671
#
# JRE version: OpenJDK Runtime Environment (11.0+24) (build 11+24-Ubuntu-118.04)
# Java VM: OpenJDK 64-Bit Server VM (11+24-Ubuntu-118.04, mixed mode, sharing, tiered, compressed oops, parallel gc, linux-amd64)
# Problematic frame:
# C  [libgsl.so.23+0x13f1bf]  gsl_ran_gaussian_ziggurat+0x1f
#
# Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport %p %s %c %d %P" (or dumping to /home/gong-yi/Programming/JVM/Dairy/2018/Oct/20/ziggurat_test/core.19603)
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

I am not sure if my approach to use gsl RNG functions is incorrect, or it's a limitation of javacpp-gsl.

@saudet
Copy link
Member

saudet commented Oct 20, 2018

It looks like we need to allocate the object with gsl_rng_alloc():
https://www.gnu.org/software/gsl/manual/html_node/Random-Number-Distribution-Examples.html
This code works fine on my end:

        gsl_rng_env_setup();
        gsl_rng_type T = gsl_rng_default();
        gsl_rng r = gsl_rng_alloc(T);
        double d = gsl_ran_gaussian_ziggurat(r, 1.0);

Let's update the sample code with that...

@GongYiLiao
Copy link
Author

GongYiLiao commented Oct 21, 2018

Thanks for the hint! The following code works now:

import org.bytedeco.javacpp._
import org.bytedeco.javacpp.gsl._

object JavaCppGSLRnormTest { 

  val rng = gsl_rng_alloc(gsl_rng_default())

  def rZigguratGSL(n: Int): Array[Double] = {
    Array.fill[Double](n)(gsl_ran_gaussian_ziggurat(rng, 1.0))
  }
}

Test:

scala> JavaCppGSLRnormTest.rZigguratGSL(3000000).sum / 3000000.0
res0: Double = 2.945159729144122E-4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants