gpt4 book ai didi

c - 为什么在为矩阵分配内存时会出现段错误?

转载 作者:行者123 更新时间:2023-11-30 15:30:15 25 4
gpt4 key购买 nike

我正在开发一个带有矩阵的 MPI 程序。我每个过程都需要 5 个矩阵。当我创建第五个矩阵时,出现段错误。

以下是一些屏幕截图:

当 sPrevParts 矩阵被注释掉时,它就起作用了 enter image description here

这里出现了段错误! :s enter image description here这里又出现了段错误...... enter image description here

这是这部分代码(如果您需要完整代码,请告诉我)。

MATRIX_CREATE函数

    /* M(m*n) as array of rows, call free(p) */
void **matrix_create(size_t m, size_t n, size_t size) {
size_t i;
void **p= (void **) malloc(m*n*size+ m*sizeof(void *));
char *c= (char*) (p+m);
for(i=0; i<m; ++i)
p[i]= (void *) c+i*n*size;
return p;
}

主要内容

    /* Variables for the partial matrixes */
double **aParts, **mParts, **mPrevParts, **sParts, **sPrevParts;
/* Gets the rows of the partial matrixes of each process */
rows = sendcounts[myrank] / n;
/* Allocates memory for the partial A matrix of each process */
aParts = (double**)matrix_create(rows, n, sizeof(double));
/* Allocates memory for the partial M matrix of each process */
mParts = (double**)matrix_create(rows, n, sizeof(double));
/* Allocates memory for the partial S matrix of each process */
sParts = (double**)matrix_create(rows, n, sizeof(double));
/* Allocates memory for the previous partial M matrix of each process */
mPrevParts = (double**)matrix_create(rows, n, sizeof(double));
/* Allocates memory for the previous partial S matrix of each process */
//PrevParts = (double**)matrix_create(rows, n, sizeof(double));

MPI_Barrier(MPI_COMM_WORLD);

/* Scatters the A matrix through all the processes */
MPI_Scatterv(&a[0][0], sendcounts, displs, MPI_DOUBLE, &aParts[0][0], sendcounts[myrank], MPI_DOUBLE, root, MPI_COMM_WORLD);
/* Scatters the M matrix through all the processes */
MPI_Scatterv(&m[0][0], sendcounts, displs, MPI_DOUBLE, &mParts[0][0], sendcounts[myrank], MPI_DOUBLE, root, MPI_COMM_WORLD);
MPI_Scatterv(&m[0][0], sendcounts, displs, MPI_DOUBLE, &mPrevParts[0][0], sendcounts[myrank], MPI_DOUBLE, root, MPI_COMM_WORLD);
/* Scatters the S matrix through all the processes */
MPI_Scatterv(&s[0][0], sendcounts, displs, MPI_DOUBLE, &sParts[0][0], sendcounts[myrank], MPI_DOUBLE, root, MPI_COMM_WORLD);
//MPI_Scatterv(&s[0][0], sendcounts, displs, MPI_DOUBLE, &sPrevParts[0][0], sendcounts[myrank], MPI_DOUBLE, root, MPI_COMM_WORLD);

int i;
for (i = 0; i < npes; ++i) {
MPI_Barrier(MPI_COMM_WORLD);
if (myrank == i) {
printf("%d\n", i);
matrix_print(aParts, rows, n, "aParts");
matrix_print(mParts, rows, n, "mParts");
matrix_print(sParts, rows, n, "sParts");
matrix_print(mPrevParts, rows, n, "mPrevParts");
}
}

注意:这是由所有进程运行的。

难道我的内存已经用完了?我怎样才能解决这个问题?谢谢

最佳答案

void * 泄露了它:

malloc(m*n*size+ m*sizeof(void *));

您实际上从未分配矩阵,而只是分配一个二维指针数组,然后将其视为double数组。

  1. 永远不要这样做。
  2. 您可能正在使用 32 位运行时进行开发,其中您的指针只有 double 大小的一半(或者在其他类型的系统中,指针仅小于 double )。

考虑this example有关使用 MPI 时如何处理矩阵的基本引用.

关于c - 为什么在为矩阵分配内存时会出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25696274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com