This question already has an answer here:
Closed 5 years ago.
free char*: invalid next size (fast) [duplicate]
(1个答案)
我是英特尔MKL的新手,下面是代码和错误详细信息。我正在尝试使用“英特尔mklmkl_scsrmultd()例程乘两个稀疏矩阵。我使用两个函数gen_col()生成列向量,gen_row()生成符合英特尔MKL规范的行指针。我使用了gdb,我认为错误在rouine内部。
先生,
a_mat是矩阵a的非零数组。a是8×8矩阵,对角点上有4×4矩阵。因此乘法使用稀疏性。A*A的乘积是8*8矩阵。甲垫和乙垫可以互换。
结果存储在q_mat中,它必须是8*8。
int * gen_col(int M, int N)
{
    int n = 0;
    int m = 0;
    int k = 1;
    int i;
    int *x = (int*)malloc(M*N);
    for (i = 0; i < M*N; i++ ){
        x[i] = m;
        m = m + 1;
        if (m == k*N) {
            m = m - N;
            n = n + 1;
        }
        if (n == N) {
            n = 0;
            k = k + 1;
            m = m + N;
        }
    }
    for (i = 0; i < M*N; i++) {
        x[i]=x[i]+1;
    }
    return x;
}

void * gen_row(int M, int N)
{
    int i;
    int *x = (int*)malloc(M+1);
    for(i = 0; i < M; i++) {
        x[i]= i*N +1;
    }

    x[M]=(M*N + 1);
    return x;
}


#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mkl_types.h"
#include "mkl_spblas.h"
#include "gen_col.h"

int main() {
    int M = 8;
    int N = 4;



    printf("  BK_1  \n");
    float *a_mat, *q_mat, *b_mat, *check_mat;
    printf(" BK_2 \n");

    a_mat = (float*)malloc((M*N)*sizeof(float));
    b_mat = (float*)malloc((M*N)*sizeof(float));
    q_mat = (float*)malloc((M*M)*sizeof(float));


    srand((unsigned int)time(0));
    int i, j;
    for(i=0; i<M; i++) {
        for(j=0; j<N; j++) {
            a_mat[i*N + j] = (float)rand()/RAND_MAX;
            b_mat[i*N + j] = (float)rand()/RAND_MAX;
        }
    }
    for(i=0; i<M; i++) {
        for(j=0; j<M; j++) {
            q_mat[i*M + j] = 0.0;
        }
    }
    printf(" BK_3 \n");
    for(i=0; i<M; i++) {
        for(j=0; j<N; j++) {
            printf("%f ",a_mat[i*N + j]);
        }
        printf(";\n");
    }

    char transa;
    transa = 't';

    MKL_INT *columns = gen_col(M,N);
    printf(" BK_4 \n");
    //int j;
    for (j = 0; j < M*N; j++){
        printf("%d",columns[j]);
    }

    int rowIndex [9]= {1, 5, 9, 13, 17, 21, 25, 29, 33};

    MKL_INT* rowIndex1 = gen_row(M,N);
    printf(" BK_5 \n");
    for (j = 0; j < M+1; j++) {
        printf("%d",rowIndex1[j]);
    }

    MKL_INT m = M;
    mkl_scsrmultd (&transa,&m, &m, &m, a_mat, columns, rowIndex, b_mat, columns, rowIndex, q_mat, &m);
    printf(" BK_6 \n");

}

错误
 *** glibc detected *** ./test.out: free(): invalid next size (fast): 0x00000000015e82c0 ***

最佳答案

我认为您调用具有三个mkl_scsrmultd维度的&m是不正确的:

    mkl_scsrmultd (&transa,&m, &m, &m, a_mat, columns, rowIndex, b_mat, columns, rowIndex, q_mat, &m);

函数的文档说明:https://software.intel.com/sites/products/documentation/hpc/mkl/mklman/GUID-D6F15254-4106-4112-9B11-F19DA5C8DDB1.htm
void mkl_scsrmultd (char *transa, MKL_INT *m, MKL_INT *n, MKL_INT *k, float *a, MKL_INT *ja, MKL_INT *ia, float *b, MKL_INT *jb, MKL_INT *ib, float *c, MKL_INT *ldc);
mkl_scsrmultd例程执行定义为
C := op(A)*B
A,B是CSR格式的稀疏矩阵(3数组变化)
哪里
m
INTEGER. Number of rows of the matrix A.
n
INTEGER. Number of columns of the matrix A.
k
INTEGER. Number of columns of the matrix B.

您已经分配了8x4(MxN)矩阵作为输入和输出(q_mat):
    int M = 8;
    int  N  = 4;

    a_mat = (float*)malloc((M*N)*sizeof(float));
    b_mat = (float*)malloc((M*N)*sizeof(float));
    q_mat = (float*)malloc((M*M)*sizeof(float));

但是说要在8x8(mkl_scsrmultdxM)矩阵上工作。这是不正确的,并且M确实在内存外写入,分配给A、B和C。

09-11 19:21