chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.78k stars 419 forks source link

Chapel bindings for GSL - GNU's Probability and Statistics Library #12743

Open LouisJenkinsCS opened 5 years ago

LouisJenkinsCS commented 5 years ago

One thing that I have sorely missed, especially while working on scientific applications, was a good statistics/probability library. It would be very nice to have some bindings around the GNU Scientific Library (GSL).

https://www.gnu.org/software/gsl/

bradcray commented 5 years ago

I feel like I've heard anecdotally that people have been able to use GSL via extern blocks and/or c2chapel, but I agree that making this into an official library with Chapeltastic interfaces would be better.

ty1027 commented 5 years ago

Athough I have no experience with GSL (and so cannot say anything about it), I think it would be definitely useful to have enhanced/increased support for statistics/probability somehow. For example, I recently wrote a mini routine for generating normal (Gaussian) random numbers based on the Box-Muller method, but I believe it is very poor in terms of performance...

(Looking at the above page, GSL seems to be using other algorithms for generating Gaussians.) https://www.gnu.org/software/gsl/doc/html/randist.html#the-gaussian-distribution The standard library in C++ also has such a generator: http://www.cplusplus.com/reference/random/normal_distribution/

ben-albrecht commented 5 years ago

For example, I recently wrote a mini routine for generating normal (Gaussian) random numbers based on the Box-Muller method, but I believe it is very poor in terms of performance...

FYI - There's an issue for this feature request here: https://github.com/chapel-lang/chapel/issues/6852

An example of using GSL gaussian distributions in Chapel is also included in the comments: link to gist

ty1027 commented 5 years ago

@ben-albrecht Thanks very much for the links, I will check how to connect with such C libraries.

Edit: Nevertheless, I also feel that the most important subset of random number distribution(s) (such as Gaussian) may be better provided in the standard library in future, because it appears so commonly in various simulations...

LouisJenkinsCS commented 5 years ago

It'd be nice to wrap and 'extern' some key things you'd want. With the availability of Mason, you can create your own package that does exactly this and distribute it @ty1027. Even incremental development would be great, and since it's open-source it allows people to contribute and provide their own wrappers.

npadmana commented 5 years ago

Dear all -- I've been slowly converting the examples in the GSL documentation into Chapel codes. In this process, I've also been sometimes adding in helper routines, although in large part, I've just stuck to the C API for simplicity.

Since the GSL list of functions is large, I've been leveraging the extern block functionality of Chapel to keep the GSL module simple. @mppf made a number of improvements to this in the last iteration, making this largely possible (thanks!). The only downside is that this requires compiling with LLVM support (although later usage doesn't require running through LLVM).

I've just pulled things into a public repo here: https://gitlab.com/npadmana/chpl-gsl

I have some more examples floating around (including the RNG distribution cases). I'll push those through.

This could also be made into a Mason package relying on GSL installed through Spack (which is what I use). However, the current testing uses start_test, so that would need to be migrated as well.

LouisJenkinsCS commented 5 years ago

This is nice, but I really would like for an actual wrapper to exist for these kinds of things.

use GSL.RNG;

config const RNGType="mt19937";
config const iseed : uint(64) = 0;
var r = new owned RNG(engine = RNGType, seed = iseed);

writef("generator type: %s\n", r.getEngine());
writef("seed = %i\n", r.getSeed());
writef("first value = %i\n", r.getNext());

I think its fine to incrementally wrap this up, rather than do it all at once. I know GSL is large, but it'd really benefit from actual Chapel bindings.

bradcray commented 5 years ago

My standard question: Was c2chapel unable to handle the GSL header?

npadmana commented 5 years ago

@bradcray -- the problem is that it isn't a single header, it's ~200 header files all including each other. I did make some progress with c2chapel, but it required a fair amount of hand-holding to make something clean. And would have been a pain for me to try and maintain.

This version is quite compact and lets me focus on the niceties...

That isn't to say that c2chapel isn't the best approach for the long-run, but extern blocks gave me what I needed.

npadmana commented 5 years ago

@LouisJenkinsCS -- I agree; it was just slightly lower priority for me. That being said, the particular API you describe would be pretty easy to do.

I just added in random number distributions, although again with the C API only.

LouisJenkinsCS commented 5 years ago

I do sincerely appreciate the heroic effort, especially since you managed to get a very nice proof of concept going so far. If possible, can you make a clone on GitHub so that I can collaborate and make my own PRs?

npadmana commented 5 years ago

@LouisJenkinsCS -- https://github.com/npadmana/chpl-gsl

Feel free to file PRs/issues; all would be much appreciated. I can try to push over a first draft at the RNG code later.

LouisJenkinsCS commented 5 years ago

Thanks!

LouisJenkinsCS commented 5 years ago

@npadmana Made a PR: https://github.com/npadmana/chpl-gsl/pull/2