C++集体数据交换实现示例讲解 c语言两个数据交换

   2023-02-09 学习力0
核心提示:目录一、说明二、示例和代码一、说明到目前为止介绍的功能共享一对一的关系:即一个进程发送和一个进程接收。链接是通过标签建立的。本节介绍在多个进程中调用相同参数但执行不同操作的函数。对于一个进程,函数可能会发送数据,对于另一个进程,它可能会接收

一、说明

到目前为止介绍的功能共享一对一的关系:即一个进程发送和一个进程接收。链接是通过标签建立的。本节介绍在多个进程中调用相同参数但执行不同操作的函数。对于一个进程,函数可能会发送数据,对于另一个进程,它可能会接收数据。这些功能称为集体操作。

二、示例和代码

示例 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
 
标签: C++ 集体 数据交换
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • Aurelius vs mORMot vs EntityDAC Delphi 的
    Aurelius vs mORMot vs EntityDAC   Delphi 的 ORM框架:http://www.tmssoftware.com/site/aurelius.asp#product-buy-onlinehttps://synopse.info/fossil/wiki/Synopse+OpenSourcehttps://www.devart.com/entitydac/download.htmlkbmMW  http://www.compo
    02-09
  • 【Ruby】Mac gem的一些坑
    前言自上一次升级MacOS系统后出现jekyll无法构建的问题,当时处理半天。谁知道最近又升级了MacOS,荒废博客多时,今天吝啬写了一篇准备发布,构建报错,问题重新。还是记录下,以防下次升级出问题。问题描述安装jekyll静态博客需要在Ruby环境下运行,于是参照
    02-09
  • iOS oc 调用 swift
    如股票oc要调用swift里面的代码 需要包含固定这个头文件项目名称 LiqunSwiftDemo-Swift.h         #ProjectName#-Swift.h固定的写法swift 目的 是取代oc 但是 不会完全取代 只是前端的替换LiqunSwiftDemo-Swift 点进去 可以看到 所有的swift代码 都产生
    02-09
  • objective-c NSTimer 定时器
    -(void)initTimer{//时间间隔NSTimeInterval timeInterval =3.0 ;//定时器repeats 表示是否需要重复,NO为只重复一次NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(mobileAnimation) userInfo:nil
    02-09
  • Objective-C  日记③ 字符串
    Objective-C 日记③ 字符串
    一、创建字符串、类方法   公式创建NSString  +(id) stringWithFormat:(NSString *) format,……;eg:  NSString *height;  height=[NSString stringWithFormat:@"高度是: %d 长度: %d",10,20];得到的字符串:“高度是: 10 长度: 20” 注意:  省
    02-09
  • Objective-C KVC机制
    Objective-C KVC机制http://blog.csdn.net/omegayy/article/details/7381301全部推翻重写一个版本,这是我在公司内做技术分享的文档总结,对结构、条理做了更清晰的调整。 1.    基本概念MODEL主要是英文文档里面经常出现的一些概念,讲解一下,方便英文
    02-09
  • objective-c 加号 减号 - +
    “加号代表static”是错误的说法,可能跟你那样表达的人其实意思是:“前置加号的方法相当于Java 里面的静态方法”。在Oc中,方法分为类方法和实例方法。前置加号(+)的方法为类方法,这类方法是可以直接用类名来调用的,它的作用主要是创建一个实例。有人把
    02-09
  • Objective-C  日记①
    Objective-C 日记①
    1、Xcode的.m扩展名表示文件含有Object-C代码,C以.c文件,C++以.cpp文件2、头文件声明:C使用:#include,O-C使用#import(当然你也可以使用#include) 3、输出方式:  C:printf("",参数);  O-C:NSLog(@"",参数); 4、布尔类型  C:bool 具有true
    02-09
  • ASP.NET MVC 操作AD 获取域服务器当前用户姓
    #region 根据当前登录域账号 获取AD用户姓名和所在OU目录/// summary/// 根据当前登录域账号 获取AD用户姓名和所在OU目录/// /summary/// param name="searchUser"要搜索的当前用户名/param/// param name="paths"out返回该用户所在OU目录/param/// param nam
    02-09
  • swift和OC - 拆分数组 和 拆分字符串
    1. 拆分数组 /// 根据 数组 截取 指定个数返回 多个数组的集合func splitArray( array: [Date], withSubSize subSize: Int) - [[Date]] {//数组将被拆分成指定长度数组的个数let count = array.count% subSize == 0 ? (array.count/ subSize) : (array.count
    02-08
点击排行