在我的代码中需要分配几个大数组。
但是,当我尝试使用 IBM xlc_r 时:
xlc_r -g -O -L。 -qarch=pwr7 -qtune=pwr7 -lesslsmp -lm -qsmp -qthreaded -qmaxmem=-1 2.c
int main()
{
int natom = 5000;
while(1)
{
double *z =(double*) malloc(natom*natom*natom* sizeof(double));
if(!z)
{
printf("error memory vector z\n\n");
exit(-1);
}
}
}
我收到了内存 vector z 的 Killed 错误。
这是ulimit -a
:
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 102400
pipe size (512 bytes, -p) 64
stack size (kbytes, -s) unlimited
cpu time (seconds, -t) unlimited
max user processes (-u) 128
virtual memory (kbytes, -v) unlimited
是否需要任何标志来分配更多内存?
最佳答案
不久前我在 POWER775 AIX 机器上遇到了这个问题。编译时需要添加 -q64 标志才能分配超过 2GiB 的内存。请注意,其他人关于使用 int
的评论可能与您的代码相关,这就是我下面的示例使用 size_t
的原因。
我建议您运行一个简单的分配测试来调查这一点。我针对这种情况编写的测试程序如下。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char * argv[])
{
size_t max = (argc>1) ? (size_t)atol(argv[1]) : ((size_t)256*1024)*((size_t)1024*1024);
for (size_t n=1; n<=max; n*=2) {
printf("attempting malloc of %zu bytes \n", n);
fflush(0);
void * ptr = malloc(n);
if (ptr==NULL) {
printf("malloc of %zu bytes failed \n", n);
fflush(0);
return 1;
} else {
printf("malloc of %zu bytes succeeded \n", n);
fflush(0);
memset(ptr,0,n);
printf("memset of %zu bytes succeeded \n", n);
fflush(0);
free(ptr);
}
}
return 0;
}
相关的构建标志如下。
ifeq ($(TARGET),POWER7-AIX)
# Savba i.e. POWER775 AIX
CC = mpcc_r
OPT = -q64 -O3 -qhot -qsmp=omp -qtune=pwr7 -qarch=pwr7 -qstrict
CFLAGS = $(OPT) -qlanglvl=stdc99
endif
关于c++ - 使用 xlc 编译器在 IBM AIX 中分配内存失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35567428/