Added Olivier's patch for improved bigarray allocation

2009-11-04

author
Markus Mottl <mmottl@janestreet.com>
date
Wed Nov 04 15:17:17 2009 -0500
changeset 13
fdb285e58044
parent 12
a2221180f522
child 14
95534ccdfe5c

Added Olivier's patch for improved bigarray allocation

mlgsl_fun.c file | annotate | diff | revisions
     1.1 --- a/mlgsl_fun.c
     1.2 +++ b/mlgsl_fun.c
     1.3 @@ -209,31 +209,19 @@
     1.4  /* MULTIMIN CALLBACKS */
     1.5  double gsl_multimin_callback(const gsl_vector *x, void *params)
     1.6  {
     1.7 -  int barr_flags = BIGARRAY_FLOAT64 | BIGARRAY_C_LAYOUT | BIGARRAY_EXTERNAL;
     1.8 +  int barr_flags = BIGARRAY_FLOAT64 | BIGARRAY_C_LAYOUT;
     1.9    struct callback_params *p=params;
    1.10    value x_barr;
    1.11 -  int len = x->size;
    1.12 -
    1.13 -/* CR mmottl: Stack allocation may segfault with large lengths, hence
    1.14 -   malloc.  Note that the OCaml callbacks may put the bigarrays into
    1.15 -   references.  This is evil, and these bindings should really get fixed
    1.16 -   to avoid problems of that sort. */
    1.17 -  double *x_arr = malloc(sizeof(double) * len);
    1.18 -#if 0
    1.19 -  LOCALARRAY(double, x_arr, len);
    1.20 -#endif
    1.21 -
    1.22 +  intnat len = x->size;
    1.23 +  double *x_arr;
    1.24    gsl_vector_view x_v;
    1.25    value res;
    1.26  
    1.27 -  x_v = gsl_vector_view_array(x_arr, len);
    1.28 +  x_barr = alloc_bigarray_dims(barr_flags, 1, NULL, len);
    1.29 +  x_v = gsl_vector_view_array(Data_bigarray_val(x_barr), len);
    1.30    gsl_vector_memcpy(&x_v.vector, x);
    1.31 -  x_barr = alloc_bigarray_dims(barr_flags, 1, x_arr, len);
    1.32    res=callback_exn(p->closure, x_barr);
    1.33  
    1.34 -  /* CR mmottl: need to free malloced memory now */
    1.35 -  free(x_arr);
    1.36 -
    1.37    if(Is_exception_result(res)) {
    1.38      return GSL_NAN;
    1.39    }

mercurial