void t11(int argumentcount, char* argumentvector[]) {
MPI_Init(&argumentcount, &argumentvector);
int rank, num_process;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_process);
// Read the local value of the process
// local_value will hold a specific int for process 0, and another for process 1
std::vector<int> send_values{ 1,2,3 }, recv_values(3);
if (rank == 0) {
// Here, enter the code for the first process :
// 2- Receive the value from process 1 (in other_value)
MPI_Recv(recv_values.data(), recv_values.size() * sizeof(int), MPI_BYTE, 1, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// 3- Print the sum of the two values on stdout
std::cout << "rank=" << rank << " recv:";
for (auto& e : recv_values)
std::cout << e <<",";
std::cout << "\n";
recv_values[0] = 3453;
MPI_Send(recv_values.data(), recv_values.size() * sizeof(int), MPI_BYTE, 1, 2, MPI_COMM_WORLD);
}
else if (rank == 1) {
// Here enter the code for the second process :
// 1- Receive the value from process 0 (in other_value)
MPI_Send(send_values.data(), send_values.size() * sizeof(int), MPI_BYTE, 0, 1, MPI_COMM_WORLD);
// 3- Print the product of the two values on stdout
MPI_Recv(recv_values.data(), recv_values.size() * sizeof(int), MPI_BYTE, 0, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::cout << "rank=" << rank << " recv:";
for (auto& e : recv_values)
std::cout << e << ",";
std::cout << "\n";
}
MPI_Finalize();
}
进程1先给进程0发送send values, 进程0接受后修改recv values再发送给进程1,进程1接受修改后的recv values