我是MPI的新手,目前正在从事一个项目,该项目需要我对本地beowulf群集进行阵列分析。我的代码是用C编写的,并且可以正确编译。它仅在使用单个进程时才能正确运行,但是当我尝试在多个进程中运行时,除根目录(等级0)外,每个进程都倾向于在尝试广播数据时死掉。我的代码看起来像这样

//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.


如果您需要查看代码的其他部分,请告诉我

注意:我已经编辑了代码以与其他用户的建议相对应,但是错误仍然存​​在

最佳答案

因此,您的数组是由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);

10-08 12:49