2016-02-23 43 views
1

在我的代码需要分配几个大阵,但是当我尝试使用IBM xlc_r:xlc_r -g -O -L. -qarch=pwr7 -qtune=pwr7 -lesslsmp -lm -qsmp -qthreaded -qmaxmem=-1 2.c内存alocation IBM AIX

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); 
     } 
    } 
} 

有时候,我收到杀,有时:错误记忆向量z

这是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 

有没有必要分配更多内存的标志?

+2

你确实意识到你正在分配500千兆字节吗?我很确定你的电脑没有那么多的内存。 –

+3

@MooingDuck我认为一个TB,假设ieee754双(64位)。但另一个问题是'natom * natom * natom'会导致整数溢出,除非'int'也是64位 –

+1

“Killed”错误可能表示操作系统使用延迟分配 –

回答

2

我在一台POWER775 AIX机器上遇到了这个问题。编译时需要添加-q64标志以分配2G以上的内存。请注意,其他人对于使用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