Qt之Concurrent Map和Map-Reduce
简述
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的更多相关文章
- MapReduce作业的map task和reduce task调度参数
MapReduce作业可以细分为map task和reduce task,而MRAppMaster又将map task和reduce task分为四种状态: 1.pending:刚启动但尚未向reso ...
- Python中的map()函数和reduce()函数的用法
Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下 Py ...
- hive优化之——控制hive任务中的map数和reduce数
一. 控制hive任务中的map数: 1. 通常情况下,作业会通过input的目录产生一个或者多个map任务.主要的决定因素有: input的文件总个数,input的文件大小,集群设置的文 ...
- python的map函数和reduce函数(转)
map函数 map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例 ...
- python一些内建函数(map,zip,filter,reduce,yield等)
python一些内建函数(map,zip,filter,reduce,yield等) map函数 Python实际上提供了一个内置的工具,map函数.这个函数的主要功能是对一个序列对象中的每一个元素应 ...
- Map,Filter和Reduce
转自:https://www.aliyun.com/jiaocheng/444967.html?spm=5176.100033.1.13.xms8KG 摘要:Map,Filter和Reduce三个函数 ...
- Python Map, Filter and Reduce
所属网站分类: python基础 > 函数 作者:慧雅 原文链接: http://www.pythonheidong.com/blog/article/21/ 来源:python黑洞网 www. ...
- [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE
map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...
- Hadoop如何计算map数和reduce数
阅读本文可以带着下面问题: 1.map和reduce的数量过多会导致什么情况? 2.Reduce可以通过什么设置来增加任务个数? 3.一个task的map数量由谁来决定? 4.一个task的reduc ...
随机推荐
- qt 2 打开文件选择框
QString sFileName = ""; sFileName = QFileDialog::getOpenFileName(this,tr("Open") ...
- C#中Application.DoEvents()的作用
Visual Studio里的摘要:处理当前在消息队列中的所有 Windows 消息. 交出CPU控制权,让系统可以处理队列中的所有Windows消息,比如在大运算量循环内,加Application. ...
- GitHub详细教程(转载)
1 Git详细教程 1.1 Git简介 1.1.1 Git是何方神圣? 1.1.2 重要的术语 1.1.3 索引 1.2 Git安装 1.3 Git配置 1.3.1 用户信息 1.3.2 高亮显示 1 ...
- 一致性 hash 算法( consistent hashing )
consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在cache 系统中应用越来越广泛: 1 基 ...
- NYOJ 士兵杀敌(三)
描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进行比较,计算出两个人的杀敌数差值,用这种方法一方面能鼓舞杀敌数高的人,另一方面也算是批评杀敌 ...
- Linux配置apache等系列
1.Linux下安装.配置PHP环境 2.ubuntu12.0.4安装apache, php ,mysql 3 CentOs中mysql的安装与配置
- [转发] 理解 oauth 2.0
原文: http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html oauth 的各种编程语言实现: http://oauth.net/2/ 理解OAu ...
- [转载] Codis作者黄东旭细说分布式Redis架构设计和踩过的那些坑们
原文: http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=208733458&idx=1&sn=691bfde670fb ...
- w3c与微软(IE)事件注册区别 -Tom
严格来说,有2中不同的模型:W3C模型和微软模型,除IE之外W3C模型支持所有的现代浏览器,而微软模型只支持IE,使用W3C模型的代码如下: // 格式:target.addEventListener ...
- poj1819Disks
链接 题意:从左到右按顺序给你n个圆的半径,把左右两边想象成两堵墙的话,就是左右两边向里挤压,问哪些圆是对最后的宽度不影响. 刚开始理解错了,..以为怎么放圆使宽度最小.. 这样就可以尽量使每个圆向左 ...