2017-09-23 29 views
0

我是MPI的新手,目前正在开发一个项目,需要我在本地beowulf集群上进行阵列分析。我的代码是用C编写的,它编译正确。它仅在使用单个进程时正确运行,但当我尝试使用多个进程运行它时,除了根(排名0)之外的每个进程都倾向于在尝试传播数据时死在点附近。我的代码看起来是这样的当使用多个进程时MPI Bcast附近的Seg故障

//1. Initialize global variables 
//2. Initialize MPI, get number of processes, get rank 
//3. All processes create two dimensional arrays 
    array1 = (char **) malloc(sizeArray1 * sizeof(char *)); 
    array1[0] = (char *) malloc(sizeArray1 * lineLength * sizeof(char)); 
    for(i = 1; i < sizeArray1; i++) 
    { 
      array1[i] = array1[i - 1] + lineLength; 
    } 
    //4. Only server will populate it's arrays, then broadcast to all processes 
    if(rank == 0) 
    { 
      f = fopen("path..../testFile1.txt", "r"); 
      if(NULL == f) { 
        perror("FAILED: "); 
        return -1; 
      } 
      numWords = 0; 
      while(err != EOF && numWords < sizeArray2) 
      { 
        err = fscanf(f, "%[^\n]\n", array2[numWords]); 
        numWords ++; 
      } 
      fclose(f); 

    } 

//5. Broadcast each line from both arrays to all processes 
MPI_Bcast(array1, sizeArrray1 * lineLength, MPI_CHAR, 0, MPI_COMM_WORLD); 
//6. do further work on arrays 

根节点将完成所有这一切都完全正常,而其他节点通常会尝试播放一遍,打印线,然后死去。那我得到确切的错误是

Signal: Segmentation fault (11) 
Signal code: Address not mapped (1) 
Failing at address: 0x37 
malloc.c:2392: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. 

如果你需要看我的代码的任何其他部分让我知道

注:我已经编辑了我的代码与来自其他用户的建议一致,但错误仍然存​​在

+3

理想情况下,你将建立一个[极小,完整,可验证的示例](https://stackoverflow.com/help/mcve)。至少,请显示你分配'array1'和'array2'的代码。这些数组应该分配在所有**任务上,并且只填充在根任务(这里是任务“0”) –

+0

我将用该部分更新代码,我只是不想包含太多因为总文件很长。就像你说的那样,我在所有任务上分配数组,但只在一个任务上填充。 – MrRempton

+0

机会是(一旦你有*最小*但可行的例子)你可能自己找到答案 – YePhIcK

回答

0

所以你的阵列是由char而不是int。 所以你应该MPI_Bcast()MPI_CHAR而不是MPI_INT。 例如

MPI_Bcast(&(array1[i][0]), lineLength, MPI_CHAR, 0, MPI_COMM_WORLD); 

作为一个风格问题,你也可以写为

MPI_Bcast(array1[i], lineLength, MPI_CHAR, 0, MPI_COMM_WORLD); 

此外,你可能想在一个块分配array1,这样你就可以用一个电话MPI_Bcast()它(这通常是更有效)

分配会是什么样

array1 = (char **)malloc(sizeArray1 * sizeof(char *); 
array1[0] = (char *)malloc(sizeArray1 * lineLength * sizeof(char)); 
for (int i=1; i<sizeArray1; i++) array1[i] = array1[i-1] + lineLength; 

然后

MPI_Bcast(array1, sizeArray1 * lineLength, MPI_CHAR, 0, MPI_COMM_WORLD); 
+0

非常感谢!我不能相信我没有马上看到它,它一定是缺乏睡眠或什么。还要感谢关于连续数组的想法!编辑:当我改变了类型,我仍然有一个seg错误,但在这种情况下,它能够通过更多的数组。它仍然有帮助,并给了我一个体面的想法。 – MrRempton

+0

如果这回答了你的问题,你可以考虑upvoting和/或接受答案?如果您遇到新问题,请发出新问题。 –