我在使用 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
函数的返回值,尤其是在矩阵太大
的情况下。不要强制转换
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
}
}
关于c - 内存不足杀死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62048131/