1

我的主进程有一个字符串数组,它发送给从属进程。从属进程对数组进行快速排序并将其发回。问题在于收到它。

在 MPI_Recv 之后,仅填充数组的前 3 个索引,其余元素为空。当我在发送之前打印数组时,元素就在那里。但是收到后,除了那3个,其他都是空的。

从机发送消息代码。它接收一个字符串数组,对其进行快速排序并将其发送回有序

// receiving number of elements
    MPI_Recv(&size, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);

// allocating the array
    array = (char**) malloc(sizeof(*array) * size);
    array[0] = (char*) malloc(sizeof(*array[0]) * size * buf);
    for(i=1; i<size; i++)
        array[i] = &(array[0][i*buf]);

    // receiving the array
    MPI_Recv(&array[0][0], size*buf, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status);

    // sorting the array
    quicksort(array, 0, size-1);

    // sending the ordered array back
    //DEBUG printf("%d: Sending for %d -> %d...\n", rank, 0, size);
    MPI_Send(&size, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
    //DEBUG printf("%d: Sending for %d -> %d...\n", rank, 0, size*buf);
    //printArray(array, size);
    printf("%d: before sending\n", rank);
    for(i=0; i<size; i++)
        printf("%d: %s", rank, array[i]);

    MPI_Send(&array[0][0], size*buf, MPI_CHAR, 0, tag, MPI_COMM_WORLD);

发送数组并应该按顺序接收它的主控

// sending the messages
    int j = 0;
    for(i=1; i<procs; i++){
        MPI_Send(&division[i], 1, MPI_INT, i, tag, MPI_COMM_WORLD);
        MPI_Send(&array[j][0], division[i]*buf, MPI_CHAR, i, tag, MPI_COMM_WORLD);
        j += division[i];
    }
    printf("%d: All Sent\n", rank);

    // receiving the ordered arrays
    printf("%d: Waiting for answers...\n", rank);

    // receiving number of elements
    MPI_Recv(&sizeA, 1, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status);

    // alocating an array
    arrayA = (char**) malloc(sizeof(*arrayA) * sizeA);
    arrayA[0] = (char*) malloc(sizeof(*arrayA[0]) * sizeA * buf);
    for(i=1; i<sizeA; i++)
        arrayA[i] = &(arrayA[0][i*buf]);

    // receiving the array
    MPI_Recv(&arrayA[0][0], sizeA*buf, MPI_CHAR, status.MPI_SOURCE, tag, MPI_COMM_WORLD, &status);

    for(i=0; i<sizeA; i++)
        printf("%i: %s", i, arrayA[i]);

编辑问题似乎出在快速排序上。如果我评论快速排序行,我会收到整个数组。尽管它在顺序版本上运行良好。这是代码。

void quicksort(char **array, int left, int right){
// array empty
if(right - left < 1)
    return;

// end of quicksort, just two elements left
if(right - left == 1){
    if(strcomp(array[left], array[right]) > 0)
        swap(array, left, right);
    return;
}

int i = left + 1;                   // left + 1: used to avoid the pivot, on the first position
int j = right;                      // right
int pivot = (left + right) / 2;     // pivot position
char *key = array[pivot];           // string with the array[pivot] element

// moving the pivot to the beginning
swap(array, left, pivot);

// quicksorting
while(i < j){

    // if an element is on the left of the pivot and, is <= than the pivot
    // keep the element there and increase i
    while(i <= right && strcomp(array[i], key) < 0){
        i++;
    }

    // if an element is on the right of the pivot, and is > than the pivot
    // keep the element there and decrease j
    while(j >= left && strcomp(array[j], key) > 0){
        j--;
    }

    // array[i] > key, and should be on the right
    // array[j] <= key, and should be on the left
    if(i < j){
        swap(array, i, j);
    }
}

// moving the pivot back to the middle
swap(array, left, j);

// left recursion
if(left < j-1)
    quicksort(array, left, j-1);

// right recursion
if(j+1 < right)
    quicksort(array, j+1, right);
}
4

0 回答 0