Hatena::ブログ(Diary)

Faith and Brave - C++で遊ぼう このページをアンテナに追加 RSSフィード Twitter

2008-10-28

[] Parallel Pattern Library

VC++10(VS2010)では、 Parallel Pattern Library (PPL) というのが提供されます。

これは並列処理を行うためのライブラリです。


インクルードするのは <ppl.h> で、名前空間は Concurrency です。


まず、 std::for_each の並列処理版である parallel_for_each

template <typename InputIterator, typename Function>
Function parallel_for_each(InputIterator first, InputIterator last, Function func);

// ※本当は _Input_iterator _First, ... のようになってますが、読みやすいように勝手に直してます。
#include <iostream>
#include <vector>
#include <ppl.h>

using namespace std;
using namespace Concurrency;

int main()
{
    vector<int> v;

    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }

    parallel_for_each(v.begin(), v.end(), [](int i) { cout << i << endl; });
}
5
6
7
8
9
0
1
2
3
4

順番を気にせず、高速に全要素を処理を行いたい場合は役立つでしょう。



次に、 for文に相当する並列処理関数 parallel_for

template <typename IndexType, typename Function>
Function parallel_for(IndexType first, IndexType last, IndexType step, Function func);
#include <iostream>
#include <vector>
#include <ppl.h>

using namespace std;
using namespace Concurrency;

int main()
{
    vector<int> v;

    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }

    parallel_for(0, 10, 1, [](int i) { cout << i << endl; });
        // for (int i = 0; i < 10; i += 1) と同じ
}
8
9
5
6
7
3
4
0
1
2

あとは、複数の関数を並列的に実行する parallel_invoke

template <typename... Functions>
void parallel_invoke(Functions&... funcs);

// ※VC++10 CTP時点では可変引数テンプレートではなく Function1 〜 Function10 のオーバーロードを作成しています
#include <iostream>
#include <array>
#include <functional>
#include <ppl.h>

using namespace std;
using namespace std::tr1;
using namespace Concurrency;

int main()
{
    array<function<void()>, 5> f = {
        []{ cout << 1 << endl; },
        []{ cout << 2 << endl; },
        []{ cout << 3 << endl; },
        []{ cout << 4 << endl; },
        []{ cout << 5 << endl; }
    };

    parallel_invoke(f[0], f[1], f[2], f[3], f[4]);
}
5
4
3
2
1

他にもいろいろあるみたいなので、わかったらまた書きます



wraith13wraith13 2008/10/28 18:22 この手のモノはC++標準で用意して欲しいものですね。

faith_and_bravefaith_and_brave 2008/10/28 18:48 ですねぇ。
スレッドが導入されるので、きっとみんな自作するんだろうなー。

hito_hpphito_hpp 2008/10/28 22:04 いやしかし、現行の規格だけで、この手のスレッドプールを効率よく実装するのは至難の業ですよ。

faith_and_bravefaith_and_brave 2008/10/28 23:46 そういえば、id:Seasonsさんから聞いたのですが、IntelのTBB?にもparallel_forがあるんだとか。
と、見てみたら名前が同じだけでした。

ntnekntnek 2008/10/29 01:02 ひょっとしてTBBと互換性があったりします?

ntnekntnek 2008/10/29 01:02 って、失礼。見落としてました。

faith_and_bravefaith_and_brave 2008/10/29 02:14 ntnekさんの生存確認(`・ω・´)

ntnekntnek 2008/10/29 21:48 生きてるけど追いかける時間がないでつ……

kazekaze 2010/07/10 21:34 MFCアプリの中で、下記のコードを実行すると、アプリ終了時に、メモリリークになってしまいました。
parallel_for(0, 10, 1, [](int i) { TRACE...; });

原因を教えてください。

faith_and_bravefaith_and_brave 2010/07/10 21:51 情報が不足しています。
「アプリ終了時にメモリリーク」というのも曖昧です。
再現プロジェクトを添えてMicrosoftに解析依頼してください。

kazekaze 2010/07/11 22:25 ご回答ありがとうございました。
parallel_for処理で、MFCアプリのディバッグ終了時に、多量のメモリリークの警告が出されています。

itouh2itouh2 2010/07/12 16:04 http://vsts2010.tistory.com/263
これが解決策になるかもしれません

kazekaze 2010/07/15 14:21 ありがとうございました。

スパム対策のためのダミーです。もし見えても何も入力しないでください
ゲスト


画像認証