C++集体数据交换实现示例讲解
作者:无水先生 发布时间:2023-12-17 11:35:07
一、说明
到目前为止介绍的功能共享一对一的关系:即一个进程发送和一个进程接收。链接是通过标签建立的。本节介绍在多个进程中调用相同参数但执行不同操作的函数。对于一个进程,函数可能会发送数据,对于另一个进程,它可能会接收数据。这些功能称为集体操作。
二、示例和代码
示例 47.9。使用 gather() 从多个进程接收数据
#include <boost/mpi.hpp>
#include <boost/serialization/string.hpp>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <iostream>
int main(int argc, char *argv[])
{
boost::mpi::environment env{argc, argv};
boost::mpi::communicator world;
if (world.rank() == 0)
{
std::vector<std::string> v;
boost::mpi::gather<std::string>(world, "", v, 0);
std::ostream_iterator<std::string> out{std::cout, "\n"};
std::copy(v.begin(), v.end(), out);
}
else if (world.rank() == 1)
{
boost::mpi::gather(world, std::string{"Hello, world!"}, 0);
}
else if (world.rank() == 2)
{
boost::mpi::gather(world, std::string{"Hello, moon!"}, 0);
}
}
Example47.9
示例 47.9 在多个进程中调用函数 boost::mpi::gather()。函数是发送还是接收取决于参数。
等级为 1 和 2 的进程使用 boost::mpi::gather() 发送数据。它们将发送的数据作为参数传递——字符串“Hello, world!”和“你好,月亮!” – 以及数据应传输到的进程的级别。由于 boost::mpi::gather() 不是成员函数,因此还必须传递 communicator world。
等级为 0 的进程调用 boost::mpi::gather() 来接收数据。由于数据必须存储在某个地方,因此传递了一个 std::vector<std::string> 类型的对象。请注意,您必须将此类型与 boost::mpi::gather() 一起使用。不支持其他容器或字符串类型。
排名 0 的进程必须传递与排名 1 和 2 的进程相同的参数。这就是排名 0 的进程也传递 world、要发送的字符串和 0 到 boost::mpi::gather() 的原因。
如果您使用三个进程启动示例 47.9,您好,世界!和你好,月亮!被显示。如果仔细查看输出,您会注意到首先写入了一个空行。第一行是等级为 0 的进程传递给 boost::mpi::gather() 的空字符串。 v 中有三个字符串,它们是从等级为 0、1 和 2 的进程接收的。向量中元素的索引对应于进程的等级。如果多次运行该示例,您将始终得到一个空字符串作为向量中的第一个元素,“Hello, world!”作为第二个元素和“你好,月亮!”作为第三个。
请注意,您不得使用超过三个进程运行示例 47.9。例如,如果您使用 -n 4 启动 mpiexec,则不会显示任何数据。该程序将挂起,必须使用 CTRL+C 中止。
必须对所有进程执行集体操作。如果您的程序调用诸如 boost::mpi::gather() 之类的函数,您必须确保该函数在所有进程中都被调用。否则就违反了 MPI 标准。因为像 boost::mpi::gather() 这样的函数必须被所有进程调用,所以每个进程的调用通常没有不同,如示例 47.9 所示。将前面的示例与执行相同操作的示例 47.10 进行比较。
示例 47.10。使用 gather() 从所有进程收集数据
#include <boost/mpi.hpp>
#include <boost/serialization/string.hpp>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <iostream>
int main(int argc, char *argv[])
{
boost::mpi::environment env{argc, argv};
boost::mpi::communicator world;
std::string s;
if (world.rank() == 1)
s = "Hello, world!";
else if (world.rank() == 2)
s = "Hello, moon!";
std::vector<std::string> v;
boost::mpi::gather(world, s, v, 0);
std::ostream_iterator<std::string> out{std::cout, "\n"};
std::copy(v.begin(), v.end(), out);
}
您为所有流程中的集体操作调用函数。通常函数的定义方式很清楚必须执行哪个操作,即使所有进程都传递相同的参数。
示例 47.10 使用 boost::mpi::gather() 来收集数据。数据在其等级作为最后一个参数传递给 boost::mpi::gather() 的过程中收集。此进程收集它从所有进程接收的数据。存储数据的向量仅供收集数据的进程使用。
boost::mpi::gather() 从所有进程收集数据。这包括收集数据的过程。在示例 47.10 中,这是等级为 0 的进程。该进程在 s 中向自身发送一个空字符串。空字符串存储在 v 中。正如您将在以下示例中看到的,集合操作始终包括所有进程。
您可以使用任意数量的进程运行示例 47.10,因为每个进程都会调用 boost::mpi::gather()。如果您使用三个进程运行该示例,结果将与前面的示例类似。
示例 47.11。在所有进程中使用 scatter() 分散数据
#include <boost/mpi.hpp>
#include <boost/serialization/string.hpp>
#include <vector>
#include <string>
#include <iostream>
int main(int argc, char *argv[])
{
boost::mpi::environment env{argc, argv};
boost::mpi::communicator world;
std::vector<std::string> v{"Hello, world!", "Hello, moon!",
"Hello, sun!"};
std::string s;
boost::mpi::scatter(world, v, s, 0);
std::cout << world.rank() << ": " << s << '\n';
}
Example47.11
示例 47.11 介绍了函数 boost::mpi::scatter()。它与 boost::mpi::gather() 相反。 boost::mpi::gather() 将来自多个进程的数据收集到一个进程中,而 boost::mpi::scatter() 将来自一个进程的数据分散到多个进程中。
示例 47.11 将来自排名为 0 的进程的 v 中的数据分散到所有进程,包括它自己。等级为 0 的进程接收到字符串“Hello, world!”在 s 中,排名为 1 的进程收到“你好,月亮!”在 s 中,等级为 2 的进程收到“Hello, sun!”秒。
示例 47.12。使用 broadcast() 向所有进程发送数据
#include <boost/mpi.hpp>
#include <boost/serialization/string.hpp>
#include <string>
#include <iostream>
int main(int argc, char *argv[])
{
boost::mpi::environment env{argc, argv};
boost::mpi::communicator world;
std::string s;
if (world.rank() == 0)
s = "Hello, world!";
boost::mpi::broadcast(world, s, 0);
std::cout << s << '\n';
}
boost::mpi::broadcast() 将数据从一个进程发送到所有进程。此函数与 boost::mpi::scatter() 之间的区别在于将相同的数据发送到所有进程。在示例 47.12 中,所有进程都收到字符串“Hello, world!”在 s 中写下你好,世界!到标准输出流。
示例 47.13。使用 reduce() 收集和分析数据
#include <boost/mpi.hpp>
#include <boost/serialization/string.hpp>
#include <string>
#include <iostream>
std::string min(const std::string &lhs, const std::string &rhs)
{
return lhs.size() < rhs.size() ? lhs : rhs;
}
int main(int argc, char *argv[])
{
boost::mpi::environment env{argc, argv};
boost::mpi::communicator world;
std::string s;
if (world.rank() == 0)
s = "Hello, world!";
else if (world.rank() == 1)
s = "Hello, moon!";
else if (world.rank() == 2)
s = "Hello, sun!";
std::string result;
boost::mpi::reduce(world, s, result, min, 0);
if (world.rank() == 0)
std::cout << result << '\n';
}
boost::mpi::reduce() 从多个进程收集数据,如 boost::mpi::gather()。但是,数据不存储在向量中。 boost::mpi::reduce() 需要一个函数或函数对象,它将用于分析数据。
如果您使用三个进程运行示例 47.13,则排名为 0 的进程会收到字符串“Hello, sun!”结果。对 boost::mpi::reduce() 的调用收集并分析所有进程传递给它的字符串。它们使用函数 min() 进行分析,该函数作为第四个参数传递给 boost::mpi::reduce()。 min() 比较两个字符串并返回较短的一个。
如果您使用三个以上的进程运行示例 47.13,则会显示一个空字符串,因为排名大于 2 的所有进程都会将一个空字符串传递给 boost::mpi::reduce()。将显示空字符串,因为它比“Hello, sun!”短
示例 47.14。使用 all_reduce() 收集和分析数据
#include <boost/mpi.hpp>
#include <boost/serialization/string.hpp>
#include <string>
#include <iostream>
std::string min(const std::string &lhs, const std::string &rhs)
{
return lhs.size() < rhs.size() ? lhs : rhs;
}
int main(int argc, char *argv[])
{
boost::mpi::environment env{argc, argv};
boost::mpi::communicator world;
std::string s;
if (world.rank() == 0)
s = "Hello, world!";
else if (world.rank() == 1)
s = "Hello, moon!";
else if (world.rank() == 2)
s = "Hello, sun!";
std::string result;
boost::mpi::all_reduce(world, s, result, min);
std::cout << world.rank() << ": " << result << '\n';
}
Example47.14
示例 47.14 使用函数 boost::mpi::all_reduce(),它像 boost::mpi::reduce() 一样收集和分析数据。这两个函数之间的区别在于 boost::mpi::all_reduce() 将分析结果发送到所有进程,而 boost::mpi::reduce() 使结果仅可用于排名作为传递的进程最后一个参数。因此,没有排名传递给 boost::mpi::all_reduce()。如果您使用三个进程运行示例 47.14,每个进程都会写入 Hello, sun!到标准输出流。
来源:https://yamagota.blog.csdn.net/article/details/127941993


猜你喜欢
- BLOB (binary large object),二进制大对象,是一个可以存储二进制文件的容器。在计算机中,BLOB常常是数据库中用来存
- 一个项目可能会有不同的环境,例如dev/stating/prod等,不同的环境的配置文件是不同的,如何根据环境快速的切换到对应的配置文件很重
- @ConfigurationProperties加载外部配置@ConfigurationProperties可以将外部配置文件(比如appl
- 前言本文主要给大家介绍了关于Spring4自定义@Value功能的相关内容,使用的Spring版本4.3.10.RELEASE,下面话不多说
- 软件下载可以在官网下载,均为免费软件有问题可以联系我邮箱求助:sexluna@outlook.comsublime text3虽然收费但是支
- 目前网上流行着很多对“时间对话框TimePickerDialog”的讲解文章,但感觉都不是很详细。所以浣熊在这里详细对该方面的知识进行介绍,
- 基础环境SpringBoot、Maven代码实现1.添加依赖<!--二维码生成 --><dependency&
- 无论是用Eclipse还是用Android Studio做android开发,都会接触到jar包,全称应该是:Java Archive,即j
- 流程图: 我们重点关心的是(1)这个过程的输入是什么?(2)这个过程的输出是什么?(3)这个过程使用了什么工具?至于使用什么参数,
- 本文实例讲述了C#获取项目指定目录下文件的方法。分享给大家供大家参考。具体如下:public List<FileInfo> Ge
- 一、Stream流简单示例需求:按照要求集合创建和遍历创建一个结合,存储多个字符串元素把集合中所有以"张"开头的元素存储
- Druid连接池连接池思想在程序初始化时,提前创建好指定数量的数据库连接对象存储在“池子”中(这个池
- 开发中,对于不经常使用英语的同学来说,对类,变量,方法想取一个合适的名字,此时发现自己的词汇早已还给老师 ,怎么办,这个插件能帮到你~一、安
- 本文实例讲述了C#实现Zip压缩目录中所有文件的方法。分享给大家供大家参考。具体实现方法如下:using System;using Syst
- 前言建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一
- 背景:有时候string类型的数据取出来是个很标准的key、value形式,通过Gson的可以直接转成map使用方式:Gson gson =
- 前言我们在 页面切换转场动画,英雄救场更有趣!介绍了 Hero 动画效果,使用 Hero 用于转场能够提供非常不错的体验。既然称之
- 实现效果如图所示:首先公布实现代码:一. 自定义实现import.org.springframework.security.core.use
- //去title requestWindowFeature(Window.FEATURE_NO_TITLE); //隐藏状态栏 getWin
- 原生系统Android8.1上,WiFi上出现感叹号,此时WiFi可正常访问。原因这是Android 5.0引入的网络评估机制:就是当你连上