当释放MKL指针后,称为lapacke hesv例程的分段错误

问题描述 投票:1回答:1

我正在开发一个代码,使用lapacke接口为C语言反转矩阵。我使用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)(取消注释),它会:

$ ./test 3

拉帕克回归: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) |

$ ./test 4

拉帕克回归: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) |

分段故障(核心转储)

c intel-mkl
1个回答
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正确工作。

© www.soinside.com 2019 - 2024. All rights reserved.