简述

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. V-rep学习笔记:机器人逆运动学数值解法(The Jacobian Transpose Method)

    机器人运动学逆解的问题经常出现在动画仿真和工业机器人的轨迹规划中:We want to know how the upper joints of the hierarchy would rotate ...

  2. VEP安装指南

    #下载依赖包 sudo apt-get install -y curl rsync tar make perl perl-base tabix #设置perl环境变量 export PERL_PATH ...

  3. Codeforces Round #376 (Div. 2) C. Socks bfs

    C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  4. Spring的DI(Ioc) - 注入bean 和 基本数据类型

    注入bean有两种方式: 注入其他bean: 方式一 <bean id="orderDao" class="cn.itcast.service.OrderDaoBe ...

  5. iOS - NetRequest 网络数据请求

    1.网络请求 1.1 网络通讯三要素 1.IP 地址(主机名): 网络中设备的唯一标示.不易记忆,可以用主机名(域名). 1) IP V4: 0~255.0~255.0~255.0~255 ,共有 2 ...

  6. [转载] 构建微服务:使用API Gateway

    原文: http://mp.weixin.qq.com/s?__biz=MzA5OTAyNzQ2OA==&mid=206889381&idx=1&sn=478ccb35294c ...

  7. Python学习(16)File(文件)方法

    Python File(文件) 方法 file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读 ...

  8. UIButton的状态

    normal(普通状态) 默认情况(Default) 对应的枚举常量:UIControlStateNormal highlighted(高亮状态)按钮被按下去的时候(手指还未松开)对应的枚举常量:UI ...

  9. hdu 4965 Fast Matrix Calculation

    题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...

  10. POJ 2385 Apple Catching

    比起之前一直在刷的背包题,这道题可以算是最纯粹的dp了,写下简单题解. 题意是说cows在1树和2树下来回移动取苹果,有移动次数限制,问最后能拿到的最多苹果数,含有最优子结构性质,大致的状态转移也不难 ...