MPICH中全局变量的分段故障1.6

时间:2013-06-07 22:29:04

标签: c++ segmentation-fault mpi mpich

考虑以下简单程序:

#include <mpi.h>                                                                                                                                                                                                                                 
#include <iostream>                                                                                                                                                                                                                              
#include <stdlib.h>                                                                                                                                                                                                                              
#include <stdio.h>                                                                                                                                                                                                                               
#include <string>                                                                                                                                                                                                                                
#include <vector>                                                                                                                                                                                                                                

using std::cout;                                                                                                                                                                                                                                 
using std::string;                                                                                                                                                                                                                               
using std::vector;                                                                                                                                                                                                                               

vector<float> test;                                                                                                                                                                                                                              
#ifdef GLOBAL                                                                                                                                                                                                                                    
string hostname;                                                                                                                                                                                                                                 
#endif                                                                                                                                                                                                                                           

int main(int argc, char** argv) {                                                                                                                                                                                                                
  int rank;  // The node id of this processor.                                                                                                                                                                                                   
  int size;  // The total number of nodes.                                                                                                                                                                                                       
#ifndef GLOBAL                                                                                                                                                                                                                                   
  string hostname;                                                                                                                                                                                                                               
#endif                                                                                                                                                                                                                                           
  MPI_Init(&argc, &argv);                                                                                                                                                                                                                        
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);                                                                                                                                                                                                          
  MPI_Comm_size(MPI_COMM_WORLD, &size);                                                                                                                                                                                                          

  cout << "Joining the job as processor: " << rank << std::endl;                                                                                                                                                                                 

  {                                                                                                                                                                                                                                              
    char buf[2048] = "HELLO";                                                                                                                                                                                                                    
    hostname.assign(buf, 2048);                                                                                                                                                                                                                  
  }                                                                                                                                                                                                                                              
  test.push_back(1.0f);                                                                                                                                                                                                                          

  cout << "Hostname: " << hostname << "::" << test[0] << std::endl;                                                                                                                                                                              

  MPI_Finalize();                                                                                                                                                                                                                                
  return 0;                                                                                                                                                                                                                                      
} 

如果我用:

编译/运行它
mpicxx -c test.cc && mpicxx -lstdc++ test.o -o test && ./test

没有分段错误,但如果我运行它:

mpicxx -DGLOBAL -c test.cc && mpicxx -lstdc++ test.o -o test && ./test

然后在hostname.assign()行有一个分段错误。另外,如果我删除了这个赋值,一旦main方法返回,字符串析构函数就会出现分段错误,所以assign方法不是真正的罪魁祸首。

请注意,唯一的区别是“全局”变量主机名被声明的位置。

我正在使用MPICH2 1.6进行编译,因为我在超级计算机上运行它,所以我没有选择更改它。

如果我删除MPI_Init等,则错误消失,导致我相信MPI和此全局变量发生了意外情况。

我在网上发现了一些其他的例子,但是他们都通过安装新版本的MPICH来解决他们的问题,这对我来说也是不可能的。

此外,我想知道为什么这种情况发生的不仅仅是一种解决方法。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

好的,经过大量调试后我发现MVAPICH2-1.6库定义了一个名为hostname的变量:

mpid/ch3/channels/mrail/src/rdma/ch3_shmem_coll.c

以下是该行(此版本文件中的55):

char hostname[SHMEM_COLL_HOSTNAME_LEN];

编译器没有在这里抱怨名称冲突,但这几乎肯定是罪魁祸首,因为在我的程序中更改变量名称会删除错误。我想这在MVAPICH2的更高版本中会有所改变,但如果没有,我会提交bug。

相关问题