東方算程譚

Oriental Code Talk ── επιστημηが与太をこく、弾幕とは無縁のシロモノ。

目次

Blog 利用状況

ニュース

著作とお薦めの品々は

著作とお薦めの品々は
東方熱帯林へ。

あわせて読みたい

わんくま

  1. 東京勉強会#2
    C++/CLI カクテル・レシピ
  2. 東京勉強会#3
    template vs. generics
  3. 大阪勉強会#6
    C++むかしばなし
  4. 東京勉強会#7
    C++むかしばなし
  5. 東京勉強会#8
    STL/CLRによるGeneric Programming
  6. TechEd 2007 @YOKOHAMA
    C++・C++/CLI・C# 適材適所
  7. 東京勉強会#14
    Making of BOF
  8. 東京勉強会#15
    状態遷移
  9. 名古屋勉強会#2
    WinUnit - お気楽お手軽UnitTest

CodeZine

  1. Cで実現する「ぷちオブジェクト指向」
  2. CUnitによるテスト駆動開発
  3. SQLiteで組み込みDB体験(2007年版)
  4. C++/CLIによるCライブラリの.NET化
  5. C# 1.1からC# 3.0まで~言語仕様の進化
  6. BoostでC++0xのライブラリ「TR1」を先取りしよう (1)
  7. BoostでC++0xのライブラリ「TR1」を先取りしよう (2)
  8. BoostでC++0xのライブラリ「TR1」を先取りしよう (3)
  9. BoostでC++0xのライブラリ「TR1」を先取りしよう (4)
  10. BoostでC++0xのライブラリ「TR1」を先取りしよう (5)
  11. C/C++に対応した、もうひとつのUnitTestFramework ─ WinUnit
  12. SQLiteで"おこづかいちょう"
  13. STL/CLRツアーガイド
  14. マージ・ソート : 巨大データのソート法
  15. ヒープソートのアルゴリズム
  16. C++0xの新機能「ラムダ式」を次期Visual Studioでいち早く試す
  17. .NETでマンデルブロ集合を描く
  18. .NETでマンデルブロ集合を描く(後日談)
  19. C++/CLI : とある文字列の相互変換(コンバージョン)
  20. インテルTBBによる選択ソートの高速化
  21. インテルTBB3.0 によるパイプライン処理
  22. Visual C++ 2010に追加されたSTLアルゴリズム
  23. Visual C++ 2010に追加されたSTLコンテナ「forward_list」
  24. shared_ptrによるObserverパターンの実装
  25. .NETでマンデルブロ集合を描く(番外編) ── OpenCLで超並列コンピューティング
  26. StateパターンでCSVを読む
  27. 状態遷移表からStateパターンを自動生成する
  28. 「ソートも、サーチも、あるんだよ」~標準C++ライブラリにみるアルゴリズムの面白さ
  29. インテルTBBの同期メカニズム
  30. なぜsetを使っちゃいけないの?
  31. WPFアプリケーションで腕試し ~C++でもWPFアプリを
  32. C++11 : スレッド・ライブラリひとめぐり
  33. Google製のC++ Unit Test Framework「Google Test」を使ってみる
  34. メールでデータベースを更新するココロミ
  35. Visitorパターンで遊んでみたよ
  36. Collection 2題:「WPFにバインドできる辞書」と「重複を許す検索set」
  37. Visual C++ 2012:stateless-lambdaとSQLiteのぷち拡張
  38. 「Visual C++ Compiler November 2012 CTP」で追加された6つの新機能

@IT

  1. Vista時代のVisual C++の流儀(前編)Vista到来。既存C/C++資産の.NET化を始めよう!
  2. Vista時代のVisual C++の流儀(中編)MFCから.NETへの実践的移行計画
  3. Vista時代のVisual C++の流儀(後編) STL/CLRによるDocument/Viewアーキテクチャ
  4. C++開発者のための単体テスト入門 第1回 C++開発者の皆さん。テスト、ちゃんとしていますか?
  5. C++開発者のための単体テスト入門 第2回 C++アプリケーションの効率的なテスト手法(CppUnit編)
  6. C++開発者のための単体テスト入門 第3回 C++アプリケーションの効率的なテスト手法(NUnit編)

AWARDS


Microsoft MVP
for Visual Developer - Visual C++


Wankuma MVP
for いぢわる C++


Nyantora MVP
for こくまろ中国茶

Xbox

Links

記事カテゴリ

書庫

日記カテゴリ

TBBで遊んでみたよ(7)

単純選択ソートを書いた。STLあると楽ちぃん:

template<typename InputIterator>
void selection_sort(InputIterator first, InputIterator last) {
    while ( first != last ) {
        iter_swap(first, min_element(first,last));
        ++first;
    }
}

ところが遅い、仕方ないけど遅い。
マルチスレッドで速くしてみる。戦略はこんな:

ソートする() {
 N個の要素を 小さい半群 と 大きい半群 に分ける。
 各群をソートする。 ←ここで両群を同時並列にソート
}

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>
#include <cassert>

#include <tbb/tick_count.h> // tick_count
#include <tbb/task.h>       // task

using namespace std;

template<typename InputIterator> inline
bool is_sorted(InputIterator first, InputIterator last) {
    return adjacent_find(first, last, greater<iterator_traits<InputIterator>::value_type>()) == last;
}

// 単純選択ソート
template<typename InputIterator>
void selection_sort(InputIterator first, InputIterator last) {
    while ( first != last ) {
        iter_swap(first, min_element(first,last));
        ++first;
    }
}

class sort_task : public tbb::task {
public:
    typedef vector<string>::iterator iterator;
    typedef vector<string>::difference_type difference_type;
protected:
    iterator first;
    iterator last;
    static difference_type cutoff;
public:
    sort_task(iterator f, iterator l) : first(f), last(l) {}
    task* execute() {
        difference_type size = distance(first,last);
        // 要素数が cutoff 未満なら素直にselection_sort
        if ( size < cutoff ) {
            selection_sort(first, last);
        } // さもなくば
        else {
            iterator mid = first;
            advance(mid, size/2);
            // [first,mid) : 小さい要素群 と [mid,last) : 大きい要素群 に振り分けて
            nth_element(first, mid, last);
            // それぞれをソートするtaskを作り
            task* left_task = new( allocate_child() ) sort_task(first, mid);
            task* right_task = new( allocate_child() ) sort_task(mid, last);
            // 双方を起動して完了を待つ
            set_ref_count(3);
            spawn(*left_task);
            spawn_and_wait_for_all(*right_task);
        }
        return 0;
    }
};

sort_task::difference_type sort_task::cutoff = 30;

int main() {

    vector<string> source;
    {
        string value = "ABCDEFGH";
        do {
            source.push_back(value);
        } while ( next_permutation(value.begin(), value.end()) );
        assert( is_sorted(source.begin(), source.end()) );
        random_shuffle(source.begin(), source.end());
    }

    {
        cout << "quick sort ... " << flush;
        vector<string> input = source;
        tbb::tick_count t = tbb::tick_count::now();
        sort(input.begin(), input.end());
        cout << (tbb::tick_count::now() - t).seconds() << " [sec]\n";
        assert( is_sorted(input.begin(), input.end()) );
    }

    {
        cout << "selection_sort ... " << flush;
        vector<string> input = source;
        tbb::tick_count t = tbb::tick_count::now();
        selection_sort( input.begin(), input.end());
        cout << (tbb::tick_count::now() - t).seconds() << " [sec]\n";
        assert( is_sorted(input.begin(), input.end()) );
    }

    {
        cout << "sort_task ... " << flush;
        vector<string> input = source;
        tbb::task* tsk = new(tbb::task::allocate_root()) sort_task(input.begin(), input.end());
        tbb::tick_count t = tbb::tick_count::now();
        tbb::task::spawn_root_and_wait(*tsk);
        cout << (tbb::tick_count::now() - t).seconds() << " [sec]\n";
        assert( is_sorted(input.begin(), input.end()) );
    }

}

おもしろいですねー、スレッドの数なんか気にせずに task こさえて task_scheduler に投げ込むだけです。 task_schedular がスレッドに投げ込んでくれるですね。

で、実行結果(dual-core):
quick sort ... 0.0690502 [sec]
selection_sort ... 33.9746 [sec] オソー
sort_task ... 0.0601392 [sec]

やった。 母さん、俺やったよ。 std::sortに勝ったよ!

投稿日時 : 2010年3月2日 22:30

コメントを追加

# re: TBBで遊んでみたよ(7) 2010/03/02 23:13 επιστημη

って、これクイックソートぢゃーん
てゆーツッコミは無しの方向で。
# 書いてたらこーなっちゃったんだからしゃぁないやん

# re: TBBで遊んでみたよ(7) 2010/03/03 8:14 もりお

C++ はよくわからないのですが、
> やった。 母さん、俺やったよ。 std::sortに勝ったよ!
面白かったです。

# re: TBBで遊んでみたよ(7) 2010/03/03 13:27 επιστημη

> 面白かったです。
ちいさくガッツポーズ

勝ったっつってもこれっぽちいじゃねー
CPUふたっつ使ってその程度? HAHAHA!
みたいな orz

# re: TBBで遊んでみたよ(7) 2010/03/03 14:18 K5

改めてstd::sortの優秀さを示しました…ともいえませぬ?

# re: TBBで遊んでみたよ(7) 2010/03/03 15:40 επιστημη

> std::sortの優秀さを示しました

C++「ところで 俺のsortを見てくれ こいつをどう思う?」
επι「すごく・・・速いです・・・」

# No matter if some one searches for his necessary thing, therefore he/she wants to be available that in detail, so that thing is maintained over here. 2019/05/14 15:45 No matter if some one searches for his necessary t

No matter if some one searches for his necessary thing, therefore he/she wants to be available that in detail,
so that thing is maintained over here.

# Hi, i think that i saw you visited my weblog so i came to “return the favor”.I'm attempting to find things to enhance my website!I suppose its ok to use a few of your ideas!! 2019/06/14 21:26 Hi, i think that i saw you visited my weblog so i

Hi, i think that i saw you visited my weblog so i came
to “return the favor”.I'm attempting to find things to enhance my website!I suppose its ok to
use a few of your ideas!!

# Hi, yup this post is really good and I have learned lot of things from it on the topic of blogging. thanks. 2019/08/15 9:17 Hi, yup this post is really good and I have learne

Hi, yup this post is really good and I have learned lot of things from it on the topic of blogging.
thanks.

# Hi, yup this post is really good and I have learned lot of things from it on the topic of blogging. thanks. 2019/08/15 9:18 Hi, yup this post is really good and I have learne

Hi, yup this post is really good and I have learned lot of things from it on the topic of blogging.
thanks.

# Hi, yup this post is really good and I have learned lot of things from it on the topic of blogging. thanks. 2019/08/15 9:19 Hi, yup this post is really good and I have learne

Hi, yup this post is really good and I have learned lot of things from it on the topic of blogging.
thanks.

# Hi, yup this post is really good and I have learned lot of things from it on the topic of blogging. thanks. 2019/08/15 9:20 Hi, yup this post is really good and I have learne

Hi, yup this post is really good and I have learned lot of things from it on the topic of blogging.
thanks.

タイトル
名前
URL
コメント