MPI_Gather 收集双重向量std::vector<std::vector<double>>

#include <mpi.h>
#include<vector>
#include<iostream>

void t13(int argumentcount, char* argumentvector[]) {
	int procRank, procSize;
	int namelen;

	char processor_name[MPI_MAX_PROCESSOR_NAME];
	MPI_Init(&argumentcount, &argumentvector);
	MPI_Comm_rank(MPI_COMM_WORLD, &procRank);
	MPI_Comm_size(MPI_COMM_WORLD, &procSize);
	MPI_Get_processor_name(processor_name, &namelen);
	MPI_Status status;

	int nSamples = 100, numRP = 3;
	int nPart = nSamples;
	if (procSize >= 1)
		nPart = nSamples / procSize + 1;
	std::cout << "nPart=" << nPart << std::endl;

	std::vector<std::vector<double>> report_params_values_all_procs(numRP);
	for (size_t c = 0; c < report_params_values_all_procs.size(); ++c) {
		report_params_values_all_procs[c].resize(nPart*procSize);
	}
	

	std::vector<std::vector<double>> report_params_values_one_proc(numRP);
	for (size_t c = 0; c < report_params_values_one_proc.size(); ++c) {
		report_params_values_one_proc[c].resize(nPart);
	}

	for (int s = 0; s < nPart; ++s) {
		for (size_t c = 0; c < numRP; ++c) {		
			report_params_values_one_proc[c][s] = procRank + s + c*0.01;
		}
	}

	for (size_t c = 0; c < numRP; ++c)
		MPI_Gather(report_params_values_one_proc[c].data(),                 
                   report_params_values_one_proc[c].size()*sizeof(double), 
                   MPI_BYTE,
		           report_params_values_all_procs[c].data(),                                                                 
                   report_params_values_one_proc[c].size()*sizeof(double),
                   MPI_BYTE, 0, MPI_COMM_WORLD);

	if (procRank == 0) {
		for (int s = 0; s < nPart*procSize; ++s) {
			std::cout << "id=" << s << ", ";
			for (size_t c = 0; c < numRP; ++c) {
				std::cout << report_params_values_all_procs[c][s] << ", ";
			}
			std::cout << std::endl;
		}
	}
}

MPI_Gather收集双重向量std::vector<std::vector<double>>,在这里是在内层数据收集的,把各个进程的内层向量收集到进程0

编译后在命令行执行mpiexec.exe -n 4 ./t13.exe ,这个指令开了4个进程

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MPI中发送和接收std::vector的步骤如下: 1. 发送方将std::vector中的数据拷贝到一个数组中。 2. 发送方使用MPI_Send函数将数组发送给接收方。在MPI_Send函数中,需要指定要发送的数据、数据的大小、接收方的rank和通信器。 3. 接收方使用MPI_Recv函数接收数据。在MPI_Recv函数中,需要指定接收缓冲区、接收数据的大小、发送方的rank和通信器。 4. 接收方将接收到的数据存储到一个数组中。 5. 接收方将数组中的数据拷贝到std::vector中。 以下是一个示例代码: ```c++ #include <mpi.h> #include <vector> int main(int argc, char** argv) { MPI_Init(NULL, NULL); int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); std::vector<int> data; if (rank == 0) { // 如果是发送方,将数据存储到std::vector中 data = {1, 2, 3, 4, 5}; // 将数据拷贝到一个数组中 int* array = data.data(); int size = data.size(); // 发送数据给接收方 MPI_Send(array, size, MPI_INT, 1, 0, MPI_COMM_WORLD); } else if (rank == 1) { // 如果是接收方,创建一个数组用于接收数据 int size = data.size(); int* array = new int[size]; // 接收数据 MPI_Recv(array, size, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); // 将数组中的数据存储到std::vector中 data.assign(array, array + size); // 打印接收到的数据 for (int i = 0; i < size; i++) { printf("%d ", data[i]); } printf("\n"); // 释放数组内存 delete[] array; } MPI_Finalize(); return 0; } ``` 在这个示例代码中,发送方将std::vector中的数据拷贝到一个数组中,并使用MPI_Send函数将数组发送给接收方。接收方使用MPI_Recv函数接收数据,并将接收到的数据存储到一个数组中,然后再将数组中的数据拷贝到std::vector中。注意,发送方和接收方需要使用相同的数据类型和数据大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值