Search code examples
carraysodegslnonlinear-functions

Dealing with big sized array in c programming


I am working on nonlinear differential equation. What I was doing is to average up the positions over 100 different values of initial conditions.

I used odeiv in gsl. For each initial values, the time range is 4*10^7. However, the program kills, once ever I set 10 different initial conditions and the time range 10^6. This is kind of limit.

My computer has 8 cores and 16GB memory. I don't think this is that much big.

I'll put a part of the coding. Anybody help me on this? Thank you.

long long int i, j, k;
double const y_i = 0, dydt_i = 0;
double n = 10;
long long int tmax = 1000000;
double A[tmax];

for (j=0; j < n; j++)
{
    double y_j = y_i + 0.001*j;
    double dydt_j = dydt_i;
    t = 0.0;
    double y[2] = {y_j, dydt_j};
    gsl_odeiv2_system sys = {func, jac, 2, &params};
    gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk8pd, 1e-6, 1e-6,     0.0);

  for (i=0; i< tmax; i++)
  {
    double ti = (double) i;
    int status = gsl_odeiv2_driver_apply (d, &t, ti, y);

    if (status != GSL_SUCCESS)
    {
      printf("error, return value%d\n", status);
      break;
    }

    A[i] = A[i] +y[0];

  }
  gsl_odeiv2_driver_free (d);
}

  for (k=0; k < tmax; k++)
  {
  A[k] = A[k]/n;
  printf("%lld %e\n", k, A[k]);
  }
return 0;
}
}

Solution

  • Local variables are allocated on the stack; the stack is not particularly huge, which means it's a bad place to allocate very large arrays. You need to make A a pointer and allocate it dynamically (or better, make it std::vector<double> if C++ is an option).