如何:并行执行映射和减少操作
本示例演示如何使用 concurrency::parallel_transform 和 concurrency::parallel_reduce 算法和 concurrency::concurrent_unordered_map 类文件中的单词的次数。
A 映射操作适用于序列中的每个值的函数。 A 减少操作将合并为一个值序列的元素。 您可以使用标准模板库 (STL) std::transformstd::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 项目中,或将它粘贴到名为的文件并行-映射-reduce.cpp ,然后在 Visual Studio 命令提示符窗口中运行以下命令。
cl.exe /EHsc parallel-map-reduce.cpp
可靠编程
在此示例中,您可以使用concurrent_unordered_map类 — — 这在 concurrent_unordered_map.h—to 中定义执行映射和减少一次操作中。
// 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.
*/
通常情况下,您并行化仅外部或内部循环。 如果您具有相对较少的文件,每个文件包含多个单词,并行化内部循环。 如果您拥有的文件相对较多,并且每个文件包含几个字,并行化外部循环。