MPI_Recv() 冻结程序,不从 C 中的 MPI_Send() 接收值

MPI_Recv() freezing program, not receiving value from MPI_Send() in C

我正在尝试在 MPI 中编写超级快速排序的实现,但我遇到了进程卡在 MPI_Recv() 上的问题。

在使用 2 个进程进行测试时,似乎在 if (rank % comm_sz == 0)else 内部,进程 1 从未从进程 0 接收数据透视。进程 0 成功发送其数据透视并通过方法正确。如果输入一些打印调试语句并收到输出:

(arr, 0, 2, 0, 9)
Rank 0 sending pivot 7 to 1
(arr, 1, 2, 0, 9)
Rank 1 pre-recv from 0

之后,来自级别 1 的 post-recv 消息永远不会打印。 Rank 0 打印它的 post-send 消息并继续通过它的数组部分。我对 MPI_Send()MPI_Recv() 的实施是否有问题导致了这种情况?

这是我的快速排序代码:

(作为参考,方法参数中的 comm_sz 指的是查看数组该部分的进程数。)

void hyper_quick(int *array, int rank, int comm_sz, int s, int e) {
    printf("(arr, %d, %d, %d, %d)\n", rank, comm_sz, s, e);
    // Keeps recursing until there is only one element
    if (s < e) {
            int pivot;
            if (comm_sz > 1) {
                    // One process gets a random pivot within its range and sends that to every process looking at that range
                    if (rank % comm_sz == 0) {
                            pivot = rand() % (e - s) + s;
                            for (int i = rank + 1; i < comm_sz; i++) {
                                    int partner = rank + i;
                                    printf("Rank %d sending pivot %d to %d\n", rank, pivot, partner);
                                    MPI_Send(&pivot, 1, MPI_INT, partner, rank, MPI_COMM_WORLD);
                                    printf("Rank %d successfully sent %d to %d\n", rank, pivot, partner);
                            }
                    }
                    else {
                            int partner = rank - (rank % comm_sz);
                            printf("Rank %d pre-recv from %d\n", rank, partner);
                            MPI_Recv(&pivot, 1, MPI_INT, partner, rank, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
                            printf("Rank %d received pivot %d from %d\n", rank, pivot, partner);
                    }
            }
            else {
                    pivot = rand() % (e - s) + s;
            }
            int tmp = array[pivot];
            array[pivot] = array[e];
            array[e] = tmp;
            // Here is where the actual quick sort happens
            int i = s;
            int j = e - 1;
            while (i < j) {
                    while (array[e] >= array[i] && i < j) {
                            i++;
                    }
                    while (array[e] < array[j] && i < j) {
                            j--;
                    }
                    if (i < j) {
                            tmp = array[i];
                            array[i] = array[j];
                            array[j] = tmp;
                    }
            }
            if (array[e] < array[i]) {
                    tmp = array[i];
                    array[i] = array[e];
                    array[e] = tmp;
                    pivot = i;
            }
            else {
                    pivot = e;
            }
            // Split remaining elements between remaining processes
            if (comm_sz > 1) {
                    // Elements greater than pivot
                    if (rank % comm_sz >= comm_sz/2) {
                            hyper_quick(array, rank, comm_sz/2, pivot + 1, e);
                    }
                    // Elements lesser than pivot
                    else {
                            hyper_quick(array, rank, comm_sz/2, s, pivot - 1);
                    }
            }
            // Recurse remaining elements in current process
            else {
                    hyper_quick(array, rank, 1, s, pivot - 1);
                    hyper_quick(array, rank, 1, pivot + 1, e);
            }
    }
Rank 0 sending pivot 7 to 1
MPI_Send(&pivot, 1, MPI_INT, partner, rank, MPI_COMM_WORLD);
                                      ^^^^

所以发件人标签为零。

Rank 1 pre-recv from 0
MPI_Recv(&pivot, 1, MPI_INT, partner, rank, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
                                      ^^^^

并且接收者标签是一个。

如果接收方只请求带有特定标签的消息,它将不会收到带有不同标签的消息。

Sometimes there are cases when A might have to send many different types of messages to B. Instead of B having to go through extra measures to differentiate all these messages, MPI allows senders and receivers to also specify message IDs with the message (known as tags). When process B only requests a message with a certain tag number, messages with different tags will be buffered by the network until B is ready for them. [MPI Tutorial -- Send and Receive]