Chương trình này được viết bằng C Lagrange và MPI. Tôi mới sử dụng MPI và muốn sử dụng tất cả các bộ vi xử lý để thực hiện một số tính toán, bao gồm quy trình 0. Để tìm hiểu khái niệm này, tôi đã viết chương trình đơn giản sau đây. Nhưng chương trình này được treo ở phía dưới sau khi nhận được thông tin từ quá trình 0 và sẽ không gửi kết quả lại cho quá trình 0.Cách Gửi/Nhận trong Bộ KH & ĐT bằng cách sử dụng tất cả bộ xử lý
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int number;
int result;
if (world_rank == 0)
{
number = -2;
int i;
for(i = 0; i < 4; i++)
{
MPI_Send(&number, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
for(i = 0; i < 4; i++)
{ /*Error: can't get result send by other processos bellow*/
MPI_Recv(&number, 1, MPI_INT, i, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process 0 received number %d from i:%d\n", number, i);
}
}
/*I want to do this without using an else statement here, so that I can use process 0 to do some calculations as well*/
MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("*Process %d received number %d from process 0\n",world_rank, number);
result = world_rank + 1;
MPI_Send(&result, 1, MPI_INT, 0, 99, MPI_COMM_WORLD); /* problem happens here when trying to send result back to process 0*/
MPI_Finalize();
}
runing và nhận được kết quả:
:$ mpicc test.c -o test
:$ mpirun -np 4 test
*Process 1 received number -2 from process 0
*Process 2 received number -2 from process 0
*Process 3 received number -2 from process 0
/* hangs here and will not continue */
Nếu bạn có thể , vui lòng chỉ cho tôi một ví dụ hoặc chỉnh sửa mã ở trên nếu có thể.