Search code examples
cstack-overflowlapackintel-mkl

Why does it stack overflow?


I'm trying to solve Schrödinger’s equation through diagonalization. And I use C++ on VS2019 and use mkl-lapackage to get the eigenvalue and eigenvector. And the whole file is modificated from intel example of LAPACKE_dsyev. I just modificated for a small piece. But I met that:

0x00C86C79 has an unhandled exception (in schro_comp.exe): 0xC00000FD: Stack Overflow (parameter: 0x00000000, 0x00402000).

I don't know why. It's 500*500 matrix. It's not large, right? I ask someone for help.

My code is:

#include <stdlib.h>
#include <stdio.h>
#include "mkl_lapacke.h"

/* Auxiliary routines prototypes */
extern void print_matrix(char* desc, MKL_INT m, MKL_INT n, double* a, MKL_INT lda);

/* Parameters */
#define N 500//nstep
#define LDA N
#define RMIN -10.0
#define RMAX 10.0
/* Main program */
int main() {
    /* Locals */
    MKL_INT n = N, lda = LDA, info;

    /* Local arrays */
    double h = (RMAX - RMIN) / (double(N) + 1.0);
    double xi;
    double w[N];
    double a[LDA * N];
    for (int i = 0; i < N; i++) {
        xi = RMIN + double(1.0+i) * h;
        a[i*(N+1)] = 2.0 / h / h+xi * xi;
        if (i==0) {
            a[1] = -1.0 / h / h;
        }
        else if (i == N - 1) {
            a[LDA * N-2] =- 1.0 / h / h;
        }
        else {
            a[i *(N + 1)+1] = 2.0 / h / h + xi * xi;
            a[i * (N + 1) - 1] = 2.0 / h / h + xi * xi;
        }
    }
    /* Executable statements */
    printf("LAPACKE_dsyev (row-major, high-level) Example Program Results\n");
    /* Solve eigenproblem */
    info = LAPACKE_dsyev(LAPACK_ROW_MAJOR, 'V', 'U', n, a, lda, w);
    /* Check for convergence */
    if (info > 0) {
        printf("The algorithm failed to compute eigenvalues.\n");
        exit(1);
    }
    /* Print eigenvalues */
    print_matrix("Eigenvalues", 1, n, w, 1);
    /* Print eigenvectors */
    print_matrix("Eigenvectors (stored columnwise)", n, n, a, lda);
    exit(0);
} /* End of LAPACKE_dsyev Example */

/* Auxiliary routine: printing a matrix */
void print_matrix(char* desc, MKL_INT m, MKL_INT n, double* a, MKL_INT lda) {
    MKL_INT i, j;
    printf("\n %s\n", desc);
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) printf(" %6.2f", a[i * lda + j]);
        printf("\n");
    }
}

I have correct some errors, but it still exists....

And the matrix is like:

Image

The matrix's last e_nstep-1 should be e_nstep-2.


Thank all people help me. The problem is solved. The space of stack on VS2019 is 1MB. So.... malloc() and free() can solve this problem.


Solution

  • The problem is solved. Thank all people help me. Just malloc and free solve the problem.(VS2019 stack space is 1MB by default, you can control it,too.)

    
    #include <stdlib.h>
    #include <stdio.h>
    #include "mkl_lapacke.h"
    
    /* Auxiliary routines prototypes */
    extern void print_matrix(char* desc, MKL_INT m, MKL_INT n, double* a, MKL_INT lda);
    
    /* Parameters */
    #define N 500//nstep
    #define LDA N
    #define RMIN -10.0
    #define RMAX 10.0
    /* Main program */
    int main() {
        /* Locals */
        MKL_INT n = N, lda = LDA, info;
    
        /* Local arrays */
        double h = (RMAX - RMIN) / (double(N) + 1.0);
        double xi;
        double *w;
        double *a;
    
        w= (double*)malloc(sizeof(double) * N);
        a = (double*)malloc(sizeof(double) * N*LDA);
    
        for (int i = 0; i < N; i++) {
            xi = RMIN + double(1.0+i) * h;
            a[i*(N+1)] = 2.0 / h / h+xi * xi;
            if (i==0) {
                a[1] = -1.0 / h / h;
            }
            else if (i == N - 1) {
                a[LDA * N-2] =- 1.0 / h / h;
            }
            else {
                a[i *(N + 1)+1] = 2.0 / h / h + xi * xi;
                a[i * (N + 1) - 1] = 2.0 / h / h + xi * xi;
            }
        }
        /* Executable statements */
        printf("LAPACKE_dsyev (row-major, high-level) Example Program Results\n");
        /* Solve eigenproblem */
        info = LAPACKE_dsyev(LAPACK_ROW_MAJOR, 'V', 'U', n, a, lda, w);
        /* Check for convergence */
        if (info > 0) {
            printf("The algorithm failed to compute eigenvalues.\n");
            exit(1);
        }
        /* Print eigenvalues */
        print_matrix("Eigenvalues", 1, n, w, 1);
        /* Print eigenvectors */
        print_matrix("Eigenvectors (stored columnwise)", n, n, a, lda);
        free(a);
        free(w);
        exit(0);
    } /* End of LAPACKE_dsyev Example */
    
    /* Auxiliary routine: printing a matrix */
    void print_matrix(char* desc, MKL_INT m, MKL_INT n, double* a, MKL_INT lda) {
        MKL_INT i, j;
        printf("\n %s\n", desc);
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++) printf(" %6.2f", a[i * lda + j]);
            printf("\n");
        }
    }