Menu

libgsl.dll calling convention problem w/ VC++

Help
2004-10-27
2012-07-26
  • Nobody/Anonymous

    Download the gsl package and had it working with the test program below. Everything runs OK until the program exit, at which point, I would get an exception in "i384/chkesp.c", line 42. The message stated "The value of ESP was not properly saved accross a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention"

    Any suggestion is highly appreciated.

    include <stdlib.h>

    include <stdio.h>

    include <gsl/gsl_rng.h>

    include <gsl/gsl_randist.h>

    include <gsl/gsl_vector.h>

    include <gsl/gsl_blas.h>

    include <gsl/gsl_multifit_nlin.h>

    struct data {
    size_t n;
    double * y;
    double * sigma;
    };

    int
    expb_f (const gsl_vector * x, void params,
    gsl_vector * f)
    {
    size_t n = ((struct data
    )params)->n;
    double y = ((struct data )params)->y;
    double sigma = ((struct data ) params)->sigma;

    double A = gsl_vector_get (x, 0);
    double lambda = gsl_vector_get (x, 1);
    double b = gsl_vector_get (x, 2);

    size_t i;

    for (i = 0; i < n; i++)
    {
    / Model Yi = A * exp(-lambda * i) + b /
    double t = i;
    double Yi = A * exp (-lambda * t) + b;
    gsl_vector_set (f, i, (Yi - y[i])/sigma[i]);
    }

    return GSL_SUCCESS;
    }

    int
    expb_df (const gsl_vector * x, void params,
    gsl_matrix * J)
    {
    size_t n = ((struct data
    )params)->n;
    double sigma = ((struct data ) params)->sigma;

    double A = gsl_vector_get (x, 0);
    double lambda = gsl_vector_get (x, 1);

    size_t i;

    for (i = 0; i < n; i++)
    {
    / Jacobian matrix J(i,j) = dfi / dxj, /
    / where fi = (Yi - yi)/sigma[i], /
    / Yi = A * exp(-lambda * i) + b /
    / and the xj are the parameters (A,lambda,b) /
    double t = i;
    double s = sigma[i];
    double e = exp(-lambda * t);
    gsl_matrix_set (J, i, 0, e/s);
    gsl_matrix_set (J, i, 1, -t * A * e/s);
    gsl_matrix_set (J, i, 2, 1/s);

    }
    

    return GSL_SUCCESS;
    }

    int
    expb_fdf (const gsl_vector * x, void *params,
    gsl_vector * f, gsl_matrix * J)
    {
    expb_f (x, params, f);
    expb_df (x, params, J);

    return GSL_SUCCESS;
    }

    void
    print_state (size_t iter, gsl_multifit_fdfsolver * s)
    {
    printf ("iter: %3u x = % 15.8f % 15.8f % 15.8f "
    "|f(x)| = %g\n",
    iter,
    gsl_vector_get (s->x, 0),
    gsl_vector_get (s->x, 1),
    gsl_vector_get (s->x, 2),
    gsl_blas_dnrm2 (s->f));
    }

    define N 40

    int
    main (void)
    {
    const gsl_multifit_fdfsolver_type T;
    gsl_multifit_fdfsolver
    s;

    int status;
    size_t i, iter = 0;

    const size_t n = N;
    const size_t p = 3;

    gsl_matrix *covar = gsl_matrix_alloc (p, p);

    double y[N], sigma[N];

    struct data d = { n, y, sigma};

    gsl_multifit_function_fdf f;

    double x_init[3] = { 1.0, 0.0, 0.0 };

    gsl_vector_view x = gsl_vector_view_array (x_init, p);

    const gsl_rng_type * type;
    gsl_rng * r;

    gsl_rng_env_setup();

    type = gsl_rng_default;
    r = gsl_rng_alloc (type);

    f.f = &expb_f;
    f.df = &expb_df;
    f.fdf = &expb_fdf;
    f.n = n;
    f.p = p;
    f.params = &d;

    / This is the data to be fitted /

    for (i = 0; i < n; i++)
    {
    double t = i;
    y[i] = 1.0 + 5 * exp (-0.1 * t)

                 + gsl_ran_gaussian (r, 0.1);
      sigma[i] = 0.1;
      printf (&quot;data: %d %g %g\n&quot;, i, y[i], sigma[i]);
    };
    

    T = gsl_multifit_fdfsolver_lmsder;
    s = gsl_multifit_fdfsolver_alloc (T, n, p);
    gsl_multifit_fdfsolver_set (s, &f, &x.vector);

    print_state (iter, s);

    do
    {
    iter++;
    status = gsl_multifit_fdfsolver_iterate (s);

      printf (&quot;status = %s\n&quot;, gsl_strerror (status));
    
      print_state (iter, s);
    
      if (status)
        break;
    
      status = gsl_multifit_test_delta (s-&gt;dx, s-&gt;x,
                                        1e-4, 1e-4);
    }
    

    while (status == GSL_CONTINUE && iter < 500);

    gsl_multifit_covar (s->J, 0.0, covar);
    //gsl_matrix_fprintf (stdout, covar, "%g");
    printf("There4444444\n");

    define FIT(i) gsl_vector_get(s->x, i)

    define ERR(i) sqrt(gsl_matrix_get(covar,i,i))

    printf ("A = %.5f +/- %.5f\n", FIT(0), ERR(0));
    printf ("lambda = %.5f +/- %.5f\n", FIT(1), ERR(1));
    printf ("b = %.5f +/- %.5f\n", FIT(2), ERR(2));

    {
    double chi = gsl_blas_dnrm2(s->f);
    printf("chisq/dof = %g\n", pow(chi, 2.0)/ (n - p));
    }

    printf ("status = %s\n", gsl_strerror (status));

    gsl_multifit_fdfsolver_free (s);
    return 0;
    }

     
    • Jerry S.

      Jerry S. - 2006-04-13

      With VC, also make sure that WIN32 is defined when compiling.

       
    • Nobody/Anonymous

      I met exactly the same problem. I tried many different ways but all failed. I don't know why that happened. Is it the but of the gsl and just can't be solved simply? And if any person who knows how to sort it out, could you just let me know? i'm very appreciate any helps provided. Many thanks in advance. My email is: danke_ksj@hotmail.com

       
    • GnuWin

      GnuWin - 2004-10-27

      Can you compile the example programs in the src-package (gsl-histogram and gsl-randist)?

       
      • Nobody/Anonymous

        Interesting... we're getting exactly the same message when debugging code where we are exiting from an operation that had just called gsl_matrix_submatrix() (this seems to be the offending call... comment it out, and all goes along fine). Our code is C++, and we are compiling under Visual C++ v6.0. We have the /GZ compiler switch (which is why we get the message). Is it possible that we have the "calling convention" switch set wrong in the project settings? (it is currently set to __cdecl *).

         
        • Nobody/Anonymous

          Did you solve it? I have the same pb? my amail:
          beguin@et.esiea.fr

           
          • Nobody/Anonymous

            I think I also have the same problem.
            The below is the sample code which copy from gsl_ref.

            The program failed at the gsl_matrix_fwrite statement.

            I use the VC6.0 and the definition GSL_DLL is added.

            Somebody tell me what's wrong? Please...

            int main (void)
            {
            int i, j, k = 0;
            gsl_matrix * m = gsl_matrix_alloc (100, 100);
            gsl_matrix * a = gsl_matrix_alloc (100, 100);
            for (i = 0; i < 100; i++)
            for (j = 0; j < 100; j++)
            gsl_matrix_set (m, i, j, 0.23 + i + j);

            FILE * f = fopen ("test.dat", "wb");
            gsl_matrix_fwrite (f, m);
            fclose (f);

             
            • Nobody/Anonymous

              I used to encounter the same problem in GSL 1.6, but now I solve the problem.
              My platform is GSL 1.8 + Visual C++ 6.0.
              Add libgsl.a libgslcblas.a into the link libraries, and DO NOT define GSL_DLL.

               
            • GnuWin

              GnuWin - 2006-02-09

              You must also include the appropriate header files.

               
    • Nobody/Anonymous

      GnuWin32,

      I'm not sure exactly what you mean by compiling the programs under these two packages. The example program uses multifit package, which in turn uses about 10 other packages directly or indirectly.

      I'm not very familar with Make/NMake stuff. I mainly worked on MS Windows platform. If you can give me a little more instruction, I can give it a try.

      For a last post, I believe you have to use __cdecl. That's the calling convention declared by most function pointer. I tried __stdcall and __fastcall, both failed to compile.

       
      • GnuWin

        GnuWin - 2004-10-28

        I did not say "the programs under these two
        packages," but "the example programs in the src-package," so from the sources of gsl (http://gnuwin32.sourceforge.net/downlinks/gsl-src-zip.php). They are simple programs designed to show the use of gsl, and do not depend on other packages. Note that you must also use the CFLAG -DGSL_DLL.
        I think you don't need Make or Nmake, but that one can also do this in the Visual Studio IDE.

         
        • Nobody/Anonymous

          Hello, fine thanks,

          I don't know exactly what is your problem so don't hesitate to answer me if it is not that.
          I remember I had some problems with gsl to get (or probably put) values of complex matrix, especialy with functions like gsl_matrix_complex_get() or gsl_matrix_submatrix(). I think there is a bug in these functions. To solve it, I created a temporary vector test_test that contains the same number of row as my matrix test_c_X (or the same number of colon if nbcolon < nbrow), then I put the colon of my matrix where is the value I want into test_test like this: gsl_matrix_complex_get_col(test_test, test_c_X, j) (this function works!!!) and finaly take the "i" value of test_test with something like this: GSL_COMPLEX_AT (test_test, i).
          In that way, I can get the (i,j) value of my complex matrix, there is probably a better way but I did'nt found it.

          So I have the equivalence between this:

          gsl_matrix_complex_get(test_c_X, i, j);

          and this:

          gsl_matrix_complex_get_col(test_test, test_c_X, j);
          GSL_COMPLEX_AT (test_test, i);

          I hope I gave you a good solution of your problem, don't hesitate to ask me if not. have a nice day.

          Cordialement,

          Régis BEGUIN

          tel: 06 12 92 34 19
          e-mail:beguin@et.esiea.fr
          ----- Original Message -----
          From: "k sj" <danke_ksj@hotmail.com>
          To: <beguin@et.esiea.fr>
          Sent: Monday, September 25, 2006 1:12 PM
          Subject: ask for help for gsl

          > Hi, how are you?
          > I saw you met a problem of using gsl on the webpage
          > http://sourceforge.net/forum/message.php?msg_id=3470488.
          > now i met exactly the same problem. I want to ask if you solved it? And
          > could tell me some solutions of it? I searched on google but haven't got
          > any solutions.
          > Many thanks for yor time.
          >
          > best regards
          > Ke
          >
          > ___________
          > 免费下载 MSN Explorer: http://explorer.msn.com/lccn/
          >
          >

           
MongoDB Logo MongoDB