我在创建一个太大的矩阵时遇到了问题,该矩阵具有Slurm簇(内存不足被杀死)。我该如何解决该问题?以下代码是有关分配矩阵的代码部分:
double **matrix;
int rows = 30000;
int cols = 39996;
matrix = (double)malloc(sizeof(double*)*rows);
for (int i = 0; i < rows; i++)
matrix[i] = (double*)malloc(sizeof(double)*cols);
for(int i=0; i<rows; i++)
for(int j=0; j<cols; j++)
matrix[i][j] = 1;
此值(行,列)是一个示例,因为我也可以具有更大的值相反,以下代码是有关释放的代码的一部分:
for (int i = 0; i < 30000; i++)
free(matrix[i]);
free(matrix);
这是我的输出:Slurmstepd:错误:在步骤98584.0 cgroup中检测到1个oom-kill事件。您的某些进程可能已被cgroup内存不足处理程序杀死。srun:错误:lab13p1:任务1:内存不足
double **matrix;
您应该验证malloc
函数的返回值,尤其是对于too big matrix
。
请勿转换malloc
功能。 Do I cast the result of malloc?
matrix = malloc(sizeof(double*)*rows);
if(!matrix) {
// handle the error
}
for (int i = 0; i < rows; i++) {
matrix[i] = malloc(sizeof(double)*cols);
if(!matrix[i]) {
// handle the error
}
}