简述

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. 【leetcode❤python】 438. Find All Anagrams in a String

    class Solution(object):    def findAnagrams(self, s, p):        """        :type s: s ...

  2. Android 基础

    1. 安卓的平台构建 例如我的手机 内核版本就是ubuntu  为手机硬件提供各种驱动. 架构的简单理解: Application(应用程序层) 我们一般说的应用层的开发就是在这个层次上进行的,当然包 ...

  3. jquery之event与originalEvent的关系、event事件对象用法浅析

    在jquery中,最终传入事件处理程序的 event 其实已经被 jQuery 做过标准化处理, 其原有的事件对象则被保存于 event 对象的 originalEvent 属性之中, 每个 even ...

  4. FreeSWITCH的TLS加密

    听着很高大上(实际也很实用)的加密机制,在FreeSWITCH里配置支持竟然这么简单! Greate FreeSWITCH and Greate Programmer! ① cd /usr/local ...

  5. 超强vim配置

    在网上找vim的配置,自己配置的特别丑 安装起来也超级方便. #!/bin/bash echo "安装将花费一定时间,请耐心等待直到安装完成^_^" if which apt-ge ...

  6. Ubuntu下手动安装VMware Tools步骤

    To mount the CD image and extract the contents: Power on the virtual machine. Log in to the virtual ...

  7. HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋)

    HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 3 ...

  8. HDU 1698 Just a Hook(线段树成段更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. iOS - Swift Array 数组

    前言 public struct Array<Element> : CollectionType, MutableCollectionType, _DestructorSafeContai ...

  10. count-the-repetitions

    https://leetcode.com/problems/count-the-repetitions/ 下面是我的方法,结果对的,超时了... package com.company; class ...