void t14(int argumentcount, char* argumentvector[]) {
MPI_Init(&argumentcount, &argumentvector);
int process_id, num_process;
MPI_Comm_rank(MPI_COMM_WORLD, &process_id);
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{ 15,23,35, 67, 87, 90, 435, 125, 127 };
std::vector<std::vector<int>> recv_values_process_i(num_process - 1);
for (int i = 0; i < send_values.size(); ++i) {
if (process_id == 0) {
int ret = MPI_Send(&send_values[i], 1, MPI_INT, i % (num_process - 1) + 1, i, MPI_COMM_WORLD);
std::cout << "MPI_Send ret=" << ret << ", process 0 send to process " << (i % (num_process - 1)) + 1 << " send: " << send_values[i] << std::endl;
}
if (process_id == i % (num_process - 1) + 1) {
int recv_value;
int ret = MPI_Recv(&recv_value, 1, MPI_INT, 0, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::cout << "MPI_Recv ret=" << ret << ", process " << process_id << " receive " << recv_value << " from process 0 : " << std::endl;
recv_values_process_i[process_id - 1].emplace_back(recv_value);
}
}
MPI_Barrier(MPI_COMM_WORLD);
if (process_id > 0) {
for (int j = 0; j < recv_values_process_i[process_id - 1].size(); ++j) {
int id = j * (num_process - 1) + process_id - 1;
std::cout << "process id=" << process_id << " recv_values_process_i " << recv_values_process_i[process_id - 1][j] << ", id in send_values=" << id << std::endl;
}
}
MPI_Finalize();
}
向量的大小大于进程的个数,然后用取余的方法(除了进程0),进程0负责发送向量,其他进程接受,