0

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;
}
}
supergentle
  • 1,001
  • 1
  • 14
  • 33

1 Answers1

6

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).

  • How big is the stack usually ? – cmidi Feb 03 '15 at 21:31
  • @cmidi: Big enough that you don't have to worry about it, so long as you don't use it to allocate big arrays or write functions that involve an extremely deep amount of recursion. In my opinion, it's almost always a bad idea to get into the mindset "I want to allocate my array on the stack, so I'll size it so it will fit". –  Feb 03 '15 at 21:34
  • 1
    In this case the stack would need to be 8Mb + – Weather Vane Feb 03 '15 at 21:35
  • Thanks @Hurkyl. I am assuming that the process stack size limit should be in /proc/pid/limits right – cmidi Feb 03 '15 at 21:44
  • Then where should I place the array? I think there is no option other than putting array in main function? is there any? – supergentle Feb 03 '15 at 23:28
  • @Supergentle: You use `malloc`, and change `A` to be a pointer to the memory so allocated. (or in C++ `new[]` or even better make `A` a `std::vector` and avoid this whole mess entirely) –  Feb 04 '15 at 10:12
  • @Hurkyl Thank you! It worked perfect!!!! I couldn't find any clue for about 1 week before your answer. I really appreciate that :D – supergentle Feb 05 '15 at 08:08