简述

QtConcurrent::map()、QtConcurrent::mapped()和QtConcurrent::mappedReduced()函数在一个序列中(例如:QList或QVector)的元素上并行地运行计算。QtConcurrent::map()直接修改一个序列,QtConcurrent::mapped()返回一个包含修改内容的新序列,QtConcurrent::mappedReduced()返回一个单独的结果。

这些函数是Qt之Concurrent框架的一部分。

上述每个函数都有一个blocking变量,其返回最终结果而不是一个QFuture。以和异步变量同样的方式来使用它们。

QList<QImage> images = ...;

// 每一个都调用blocks,直到整个操作完成
QList<QImage> future = QtConcurrent::blockingMapped(images, scaled); QtConcurrent::blockingMap(images, scale); QImage collage = QtConcurrent::blockingMappedReduced(images, scaled, addToCollage);

注意:上述的结果类型不是QFuture对象,而是实际结果类型(在这种情况下,QList<QImage>和QImage)。

Concurrent Map

QtConcurrent::mapped()接受一个输入序列和map函数,该map函数被序列中的每个元素调用,返回一个包含map函数返回值的新序列。

map函数必须是下面的形式:

U function(const T &t);

T和U可以是任意类型(它们甚至可以是同一类型),但是T必须匹配存储在序列中的类型,函数返回修改或映射的内容。

下面示例介绍了如何为序列中的所有元素都应用scale函数:

QImage scaled(const QImage &image)
{
return image.scaled(100, 100);
} QList<QImage> images = ...;
QFuture<QImage> thumbnails = QtConcurrent::mapped(images, scaled);

map的结果通过QFuture可用。查看QFuture和QFutureWatcher的文档,了解更多关于程序中如何使用QFutured的内容。

如果想直接修改一个序列,使用QtConcurrent::map()。map函数必须是以下形式:

U function(T &t);

注意: map函数的返回值、返回类型没有被使用。

使用QtConcurrent::map()和使用QtConcurrent::mapped()类似:

void scale(QImage &image)
{
image = image.scaled(100, 100);
} QList<QImage> images = ...;
QFuture<void> future = QtConcurrent::map(images, scale);

由于该序列被直接修改,QtConcurrent::map()不通过QFuture返回任何结果。然而,你仍然可以使用QFuture和QFutureWatcher监控map的状态。

Concurrent Map-Reduce

QtConcurrent::mappedReduced()类似于QtConcurrent::mapped(),但是返回一个新结果序列,通过一个reduce函数,结果被组合成一个值。

reduce函数必须是以下形式:

V function(T &result, const U &intermediate)

T是最终结果的类型,U是map函数的返回类型。注意: reduce函数的返回值、返回类型并没有被使用。

调用QtConcurrent::mappedReduced()如下所示:

void addToCollage(QImage &collage, const QImage &thumbnail)
{
QPainter p(&collage);
static QPoint offset = QPoint(0, 0);
p.drawImage(offset, thumbnail);
offset += ...;
} QList<QImage> images = ...;
QFuture<QImage> collage = QtConcurrent::mappedReduced(images, scaled, addToCollage);

reduce函数将由map函数返回的每个结果调用一次,并且应该合并中间体到结果变量。QtConcurrent::mappedReduced()可以保证保证一次只有一个线程调用reduce,所以没有必要用一个mutex锁定结果变量。QtConcurrent::ReduceOptions枚举提供了一种方法来控制reduction完成的顺序。如果使用了QtConcurrent::UnorderedReduce(默认),顺序是不确定的;而QtConcurrent::OrderedReduce确保reduction按照原始序列的顺序完成。

附加API功能

使用迭代器而不是序列

上述每个函数都有一个变量,需要一个迭代器范围,而不是一个序列。以和序列变量同样的方式来使用它们。

QList<QImage> images = ...;

QFuture<QImage> thumbnails = QtConcurrent::mapped(images.constBegin(), images.constEnd(), scaled);

// map直接仅运行在non-const迭代器上
QFuture<void> future = QtConcurrent::map(images.begin(), images.end(), scale); QFuture<QImage> collage = QtConcurrent::mappedReduced(images.constBegin(), images.constEnd(), scaled, addToCollage);

Blocking变量

上述每个函数能有一个blocking变量,其返回最终结果而不是一个QFuture。以和异步变量同样的方式来使用它们。

QList<QImage> images = ...;

// 每一个都调用blocks,直到整个操作完成
QList<QImage> future = QtConcurrent::blockingMapped(images, scaled); QtConcurrent::blockingMap(images, scale); QImage collage = QtConcurrent::blockingMappedReduced(images, scaled, addToCollage);

注意:上述的结果类型不是QFuture对象,而是实际结果类型(在这种情况下,QList<QImage>和QImage)。

使用成员函数

QtConcurrent::map()、QtConcurrent::mapped()和QtConcurrent::mappedReduced()接受指向成员函数的指针,成员函数类类型必须匹配存储在序列中的类型:

// 挤压所有的字符串到一个QStringList中
QStringList strings = ...;
QFuture<void> squeezedStrings = QtConcurrent::map(strings, &QString::squeeze); // 交换一个列表中的图片所有像素的rgb值
QList<QImage> images = ...;
QFuture<QImage> bgrImages = QtConcurrent::mapped(images, &QImage::rgbSwapped); // 创建一个列表中所有字符串长度的集合
QStringList strings = ...;
QFuture<QSet<int> > wordLengths = QtConcurrent::mappedReduced(strings, &QString::length, &QSet<int>::insert);

注意:当使用QtConcurrent::mappedReduced()时,你可以自由组合正常函数和成员函数的使用:

// 可以使用QtConcurrent::mappedReduced()组合正常函数和成员函数

// 计算字符串列表的平均长度
extern void computeAverage(int &average, int length);
QStringList strings = ...;
QFuture<int> averageWordLength = QtConcurrent::mappedReduced(strings, &QString::length, computeAverage); // 创建一个列表中所有图片颜色分布的集合
extern int colorDistribution(const QImage &string);
QList<QImage> images = ...;
QFuture<QSet<int> > totalColorDistribution = QtConcurrent::mappedReduced(images, colorDistribution, QSet<int>::insert);

使用函数对象

QtConcurrent::map()、QtConcurrent::mapped()和QtConcurrent::mappedReduced()接受函数对象,可用于添加状态函数调用。result_type typedef必须定义函数调用操作的结果类型:

struct Scaled
{
Scaled(int size)
: m_size(size) { } typedef QImage result_type; QImage operator()(const QImage &image)
{
return image.scaled(m_size, m_size);
} int m_size;
}; QList<QImage> images = ...;
QFuture<QImage> thumbnails = QtConcurrent::mapped(images, Scaled(100));

使用绑定函数参数

如果你想使用一个map函数,它接受多个参数可以使用std::bind()来将其转换到一个接受一个参数的函数上。如果c++ 11支持不可用,boost::bind()或std::tr1::bind()是合适的替代品。

举个例子,我们将使用QImage::scaledToWidth():

QImage QImage::scaledToWidth(int width, Qt::TransformationMode) const;

scaledToWidth接收三个参数(包括“this”指针),不能直接与QtConcurrent::mapped() 一起使用,因为QtConcurrent::mapped()需要接受一个参数的函数。为了使用QImage::scaledToWidth() with QtConcurrent::mapped(),我们必须为width和transformation mode提供一个值:

std::bind(&QImage::scaledToWidth, 100, Qt::SmoothTransformation)

std::bind()的返回值是一个具有以下签名的函数对象(functor):

QImage scaledToWith(const QImage &image)

这符合QtConcurrent::mapped()期望的,完整的示例变为:

QList<QImage> images = ...;
QFuture<QImage> thumbnails = QtConcurrent::mapped(images, std::bind(&QImage::scaledToWidth, 100 Qt::SmoothTransformation));

Qt之Concurrent Map和Map-Reduce的更多相关文章

  1. MapReduce作业的map task和reduce task调度参数

    MapReduce作业可以细分为map task和reduce task,而MRAppMaster又将map task和reduce task分为四种状态: 1.pending:刚启动但尚未向reso ...

  2. Python中的map()函数和reduce()函数的用法

    Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下   Py ...

  3. hive优化之——控制hive任务中的map数和reduce数

    一.    控制hive任务中的map数: 1.    通常情况下,作业会通过input的目录产生一个或者多个map任务.主要的决定因素有: input的文件总个数,input的文件大小,集群设置的文 ...

  4. python的map函数和reduce函数(转)

    map函数 map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例 ...

  5. python一些内建函数(map,zip,filter,reduce,yield等)

    python一些内建函数(map,zip,filter,reduce,yield等) map函数 Python实际上提供了一个内置的工具,map函数.这个函数的主要功能是对一个序列对象中的每一个元素应 ...

  6. Map,Filter和Reduce

    转自:https://www.aliyun.com/jiaocheng/444967.html?spm=5176.100033.1.13.xms8KG 摘要:Map,Filter和Reduce三个函数 ...

  7. Python Map, Filter and Reduce

    所属网站分类: python基础 > 函数 作者:慧雅 原文链接: http://www.pythonheidong.com/blog/article/21/ 来源:python黑洞网 www. ...

  8. [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE

    map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...

  9. Hadoop如何计算map数和reduce数

    阅读本文可以带着下面问题: 1.map和reduce的数量过多会导致什么情况? 2.Reduce可以通过什么设置来增加任务个数? 3.一个task的map数量由谁来决定? 4.一个task的reduc ...

随机推荐

  1. Cheatsheet: 2013 08.14 ~ 08.19

    .NET Lucene.Net ultra fast search for MVC or WebForms site => made easy! C# State Machines HttpCl ...

  2. Create XO Checker Game With Oracle Forms

    Created XO Checker game in Oracle Forms and sharing its FMB (source code) for reference so that you ...

  3. 7.Constants and Fields

    1.Constants is a symbol that has a never-changing value.  its value must be determinable at compile ...

  4. CUBRID学习笔记 38 net调用java的函数过程

    首先要设置java_stored_procedure 为yes  该配置项在cubrid.conf中 书写并编译java代码 public class SpCubrid{      public st ...

  5. ServiceStack.OrmLite 笔记4 删

    删除 db.DeleteAll(); //各种姿势 db.Delete(p => p.Age == 27);// db.Delete(q => q.Where(p => p.Age ...

  6. hdu 1075 (map)

    http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: 10000/5000 MS ...

  7. git :设置 object-c 的忽略文件

    使用 git 命令行来进行版本控制的时候, 需要设置忽略文件. 这里能找到所有语言的忽略文件的内容:https://github.com/github/gitignore OBJECT的忽略文件内容: ...

  8. VS2012创建MVC3项目提示错误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。

    如果在没有安装vs2012 update3升级包的情况下,创建MVC3项目会出现下面的错误信息. 因为VS2012已经全面切换到使用NuGet这个第三方开源工具来管理项目包和引用模块了,使用VS201 ...

  9. Chrome浏览器的密码隐患

    谷歌浏览器的密码填充使得登陆账号很方便 但在你了解了Chrome的密码特性机制后,你该做点什么了 1.如何查看已保存的密码 Chrome 密码管理器的进入方式:右侧扳手图标→设置→显示高级设置→密码和 ...

  10. iOS - UIDevice

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIDevice : NSObject @available(iOS 2.0, *) public class UI ...