That's quite normal. You have this block of declarations in your code:
lapack_int ipiv[N];
lapack_complex_double a[N*N];
lapack_complex_double b[N*N];
which, I guess, is the dominant memory allocation. I suppose that a Lapack complex double is represented by a couple of double
and a Lapack int as int
, but even if this is not the case, the final result won't change that much.
Let's take some standard values (in Bytes) for sizeof (double)
, 8, and sizeof (int)
, 4.
Ok, we are ready to calculate how much memory you need
Memory occupation = 2*(8*2)*N² + 4*N = 32N² + 4N Bytes
What does this mean?
| N | Memory occupation |
|--------|-------------------|
| 50 | 80kb |
| 100 | 320kb |
| 150 | 721kb |
| 500 | 8MB |
| 1000 | 32MB |
| 1500 | 72MB |
Since those are local variables, they are allocated on the stack and... wow! 72MB of stack! That's quite a lot, consider that standard stack size is 1MB on Windows with MSVC.
You could allocate them dynamically with malloc
:
lapack_int *ipiv = malloc(N * sizeof (lapack_int));
lapack_complex_double *a = malloc(N * N * sizeof (lapack_complex_double);
lapack_complex_double *b = malloc(N * N * sizeof (lapack_complex_double);
but remember to release resources before leaving the function, or you will leak (a lot of) memory. Do:
free(ipiv);
free(a);
free(b);
just before return
.