C++ MPI_Send, MPI_Recv 向量并修改

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值