다음을 통해 공유


방법: 매핑 수행 및 병렬 작업 줄이기

사용 하는 방법을 보여 주는이 예제는 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.
*/

일반적으로 외부 또는 내부 루프를 병렬화.안쪽 루프를 병렬화 하 비교적 적은 파일이 있고 각 파일 여러 단어를 포함 합니다.외부 루프를 병렬화 하 비교적 많은 파일이 있고 각 파일에 몇 개의 단어가 들어 있습니다.

참고 항목

참조

parallel_transform 함수

parallel_reduce 함수

concurrent_unordered_map 클래스

개념

병렬 알고리즘