如何:并行执行映射和减少操作

此示例演示如何使用和类来计数词 concurrency::concurrent_unordered_map concurrency::parallel_transform concurrency::parallel_reduce 算法和出现在文件中。

映射 操作将函数应用于序列的每个值。 化简操作 合并序列的元素分为一个值。 可以使用标准模板库 (STL) (STL) std::transform std::accumulate 类执行映射和化简操作。 但是,许多改进问题的性能,您可以使用 parallel_transform 算法并行执行映射操作和 parallel_reduce 算法并行执行化简操作。 在某些情况下,可以在一个操作可以使用 concurrent_unordered_map 执行映射和减少。

示例

下例字词数出现在文件中。 此示例使用 std::vector 表示两个文件的内容。 映射操作计算每个单词匹配项在每个矢量的。 操作化简累计跨两个矢量中单词数。

// parallel-map-reduce.cpp 
// compile with: /EHsc
#include <ppl.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <unordered_map>
#include <windows.h>

using namespace concurrency;
using namespace std;

class MapFunc 
{ 
public:
    unordered_map<wstring, size_t> operator()(vector<wstring>& elements) const 
    { 
        unordered_map<wstring, size_t> m;
        for_each(begin(elements), end(elements), [&m](const wstring& elem)
        { 
            m[elem]++;
        });
        return m; 
    }
}; 

struct ReduceFunc : binary_function<unordered_map<wstring, size_t>, 
                    unordered_map<wstring, size_t>, unordered_map<wstring, size_t>>
{
    unordered_map<wstring, size_t> operator() (
        const unordered_map<wstring, size_t>& x, 
        const unordered_map<wstring, size_t>& y) const
    {
        unordered_map<wstring, size_t> ret(x);
        for_each(begin(y), end(y), [&ret](const pair<wstring, size_t>& pr) {
            auto key = pr.first;
            auto val = pr.second;
            ret[key] += val;
        });
        return ret; 
    }
}; 

int wmain()
{ 
    // File 1 
    vector<wstring> v1;
    v1.push_back(L"word1"); //1 
    v1.push_back(L"word1"); //2 
    v1.push_back(L"word2"); 
    v1.push_back(L"word3"); 
    v1.push_back(L"word4"); 

    // File 2 
    vector<wstring> v2; 
    v2.push_back(L"word5"); 
    v2.push_back(L"word6"); 
    v2.push_back(L"word7"); 
    v2.push_back(L"word8"); 
    v2.push_back(L"word1"); //3 

    vector<vector<wstring>> v;
    v.push_back(v1);
    v.push_back(v2);

    vector<unordered_map<wstring, size_t>> map(v.size()); 

    // The Map operation
    parallel_transform(begin(v), end(v), begin(map), MapFunc()); 

    // The Reduce operation 
    unordered_map<wstring, size_t> result = parallel_reduce(
        begin(map), end(map), unordered_map<wstring, size_t>(), ReduceFunc());

    wcout << L"\"word1\" occurs " << result.at(L"word1") << L" times. " << endl;
} 
/* Output:
   "word1" occurs 3 times.
*/

编译代码

若要编译代码,请复制代码并将其粘贴到 Visual Studio项目中或一个名为parallel-map-reduce.cpp的文件中,然后在Visual Studio命令提示符窗口中运行以下命令。

cl.exe /EHsc parallel-map-reduce.cpp

可靠编程

在此示例中,类在 concurrent_unordered_map.h-to 定义执行映射的方法在一个操作可以使用 concurrent_unordered_map 和减少。

// File 1 
vector<wstring> v1;
v1.push_back(L"word1"); //1 
v1.push_back(L"word1"); //2 
v1.push_back(L"word2"); 
v1.push_back(L"word3"); 
v1.push_back(L"word4"); 

// File 2 
vector<wstring> v2; 
v2.push_back(L"word5"); 
v2.push_back(L"word6"); 
v2.push_back(L"word7"); 
v2.push_back(L"word8"); 
v2.push_back(L"word1"); //3 

vector<vector<wstring>> v;
v.push_back(v1);
v.push_back(v2);

concurrent_unordered_map<wstring, size_t> result;
for_each(begin(v), end(v), [&result](const vector<wstring>& words) {
    parallel_for_each(begin(words), end(words), [&result](const wstring& word) {
        InterlockedIncrement(&result[word]);
    });
});

wcout << L"\"word1\" occurs " << result.at(L"word1") << L" times. " << endl;

/* Output:
   "word1" occurs 3 times.
*/

通常,您仅并行化外部或内部循环。 要并行化内层循环,则将相对少文件,每个文件中包含许多字。 要并行化外层循环,则将相对多文件,每个文件中包含很少字。

请参见

参考

parallel_transform 函数

parallel_reduce 函数

concurrent_unordered_map 类

概念

并行算法