在调用lapacke hesv例程后释放MKL指针时出现分段错误

时间:2018-08-30 03:36:52

标签: c intel-mkl

我正在开发使用C语言的lapacke接口反转矩阵的代码。我用intel mkl库完成了这项工作。但是,当我尝试简单的测试来反转大小为N的矩阵时,当我释放矩阵的指针时会得到不确定的行为。奇怪的是,因为如果N = 3但它在N> = 4时停止工作,例如,如果我不释放该指针,则代码对于N的任何值都可以完美运行。该函数要求的其他指针可以在没有任何释放的情况下释放麻烦。有什么想法吗?

其他信息:我正在使用zhesv函数,该函数可以解决多个线性系统。可以在以下位置找到该API:https://software.intel.com/en-us/node/520994

我要运行的代码:

/*
 * compile with intel mkl installation:
 *
 * gcc -o test exe/lapack_inversion_test.c  -L${MKLROOT}/lib/intel64 -Wl,--no-as-    needed -lmkl_intel_ilp64 -lmkl_gnu_thread -lmkl_core -lgomp -lm -ldl
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <mkl.h>
#include <math.h>

#define PI 3.141592653589793 // to define matrix entries

#define LAPACK_ROW_MAJOR 101
#define LAPACK_COL_MAJOR 102

int main(int argc, char * argv[])
{
    int j, // counter
        i, // counter
        N, // The size of the Matrix
        k;

    double arg;

    sscanf(argv[1], "%d", &N);

    int * ipiv = (int *) malloc(N * sizeof(int));

    MKL_Complex16 x; x.real = 0; x.imag = 0;
    MKL_Complex16 * A = malloc(N * N * sizeof(MKL_Complex16));
    MKL_Complex16 * Acopy = malloc(N * N * sizeof(MKL_Complex16));
    MKL_Complex16 * Id = malloc(N * N * sizeof(MKL_Complex16));

    for (i = 0; i < N; i++)
    {   // Row major storage
        A[i * N + i].real = 1.5 + sin( 3 * PI * i / N );
        A[i * N + i].imag = 0;
        Acopy[i * N + i].real = 1.5 + sin( 3 * PI * i / N );
        Acopy[i * N + i].imag = 0;
        Id[i * N + i].real = 1;
        Id[i * N + i].imag = 0;
        for (j = 0; j < i; j++)
        {
            arg = 10 * PI * ((double) i * j) / (N * N);
            A[i * N + j].real = 2 * sin(arg) + 2;
            A[i * N + j].imag = 3 * cos(arg);
            A[j * N + i].real = 0; // Does not matter the upper tirangular
            A[j * N + i].imag = 0; // when call lapack routine with 'L'

            Acopy[i * N + j].real = 2 * sin(arg) + 2;
            Acopy[i * N + j].imag = 3 * cos(arg);
            Acopy[j * N + i].real = Acopy[i * N + j].real;
            Acopy[j * N + i].imag = - Acopy[i * N + j].imag;

            Id[i * N + j].real = 0; // Identity
            Id[i * N + j].imag = 0;
            Id[j * N + i].real = 0;
            Id[j * N + i].imag = 0;
        }
    }

    i = LAPACKE_zhesv(LAPACK_ROW_MAJOR, 'L', N, N, A, N, ipiv, Id, N);
    printf("\n\nLapacke returned : %d\n", i);

    printf("\n\nIf there was any problem print identity: \n");

    for (i = 0; i < N; i++)
    {
        printf("\n\t|");
        for (j = 0; j < N; j++)
        {
            x.real = 0;
            x.imag = 0;
            for (k = 0; k < N; k++)
            {
                x.real += Id[i*N + k].real * Acopy[k*N + j].real;
                x.real -= Id[i*N + k].imag * Acopy[k*N + j].imag;
                x.imag += Id[i*N + k].real * Acopy[k*N + j].imag;
                x.imag += Id[i*N + k].imag * Acopy[k*N + j].real;
            }
            printf(" (%6.3lf,%6.3lf) |", x.real, x.imag);
        }
    }

    // free(A); if one try to free does not work with N >= 4 !!!!
    free(Id);
    free(ipiv);

    printf("\n\n");
    return 0;
}

如果我们让代码释放(A)(取消注释),它将执行以下操作:

$ ./测试3

Lapacke返回了:0

如果打印身份有任何问题:

| ( 1.000,-0.000) | ( 0.000,-0.000) | (-0.000,-0.000) |
| ( 0.000, 0.000) | ( 1.000, 0.000) | ( 0.000, 0.000) |
| (-0.000,-0.000) | (-0.000, 0.000) | ( 1.000, 0.000) |

$ ./测试4

Lapacke返回了:0

如果打印身份有任何问题:

| ( 1.000,-0.000) | ( 0.000,-0.000) | ( 0.000, 0.000) | (-0.000, 0.000) |
| (-0.000, 0.000) | ( 1.000, 0.000) | ( 0.000, 0.000) | (-0.000, 0.000) |
| (-0.000, 0.000) | (-0.000, 0.000) | ( 1.000,-0.000) | (-0.000,-0.000) |

分段错误(核心已转储)

1 个答案:

答案 0 :(得分:0)

让我们看一下LAPACKE_zhesv的声明:

lapack_int LAPACKE_zhesv(int matrix_layout, char uplo, lapack_int n, lapack_int nrhs, lapack_complex_double* a, lapack_int lda, lapack_int* ipiv, lapack_complex_double* b , lapack_int ldb);

特别是,它需要lapack_int* ipiv,但是您将其声明为int* ipiv。然后,将程序与mkl_intel_ilp64do not define MKL_ILP64链接。您的程序和MKL最终使用不兼容的整数类型:sizeof(int) = 4,而sizeof(lapack_int) = 8

快速解决方案是与mkl_intel_lp64链接。但是您应该重写代码以使用lapack_int而不是int,然后它才能与LP64ILP64一起正常工作。