4

parallel_for_each次の形式です。

Concurrency::parallel_for_each(start_iterator, end_iterator, function_object);

しかしparallel_for、同様の形式でもあります:

Concurrency::parallel_for(start_value, end_value, function_object);

Concurrency::parallel_forマルチコアのプログラミングで使用されるとのConcurrency::parallel_for_eachアルゴリズムの違いは何ですか?

4

1 に答える 1

7

あなたが話しているライブラリはわかりませんが、これはイテレータを使用しているようです。

Concurrency::parallel_for_each(start_iterator, end_iterator, function_object);

そして、おそらくこれと同じ効果があります(必ずしも同じ順序である必要はありませんが):

for(sometype i = start_iterator; i != end_iterator; ++i) {
    function_object(*i);
}

例えば:

void do_stuff(int x) { /* ... */ }
vector<int> things;
// presumably calls do_stuff() for each thing in things
Concurrency::parallel_for_each(things.begin(), things.end(), do_stuff);

もう1つは値を取るため、これと同様の効果がある可能性があります(ただし、順序は保証されていません)。

for(sometype i = start_value; i != end_value; ++i) {
    function_object(i);
}

これを実行してみてください:

void print_value(int value) {
    cout << value << endl;
}

int main() {
    // My guess is that this will print 0 ... 9 (not necessarily in order)
    Concurrency::parallel_for(0, 10, print_value);
    return 0;
}

編集:これらの動作の確認は、並列アルゴリズムのリファレンスにあります。

于 2011-12-14T03:45:04.620 に答える