2012-04-15 7 views
1

TBBの列挙可能なスレッド固有のことを理解するのが苦労しています。 私はTLSTBBスレッドのローカルストレージについて

#include <iostream> 
#include <vector> 

#include "tbb/task_scheduler_init.h" 
#include "tbb/enumerable_thread_specific.h" 
#include "tbb/task.h" 

typedef tbb::enumerable_thread_specific< std::vector<int> > TLS; 

class Child: public tbb::task { 
private: 
    int start; 
    int limit; 
    TLS* local_tls; 
public: 
    Child(int s_, int l_, TLS* t_):start(s_),limit(l_),local_tls(t_){} 
    virtual ~Child(){ 
    local_tls=0; 
    } 
    tbb::task* execute(){ 
    TLS::reference local_vector = local_tls->local(); 
    for(int i=start; i<limit;++i){ 
    local_vector.push_back(i); 
    } 
    } 
    return 0; 
} 
}; 

class Cont: public tbb::task { 
private: 
    TLS global_tls; 
public: 
    Cont(){} 
    virtual ~Cont(){} 
    TLS* GetTls(void) { return &global_tls; } 
    tbb::task* execute(){ 
    TLS::const_iterator it(global_tls.begin()); 
    const TLS::const_iterator end(global_tls.end()); 
    std::cout << "ETS.SIZE: " << global_tls.size() << std::endl; 
    while(it != end) { 
     std::cout << "*ITSIZE: " << (*it).size() << "\n"; 
     for(unsigned int j(0); j < (*it).size(); ++j) { 
     std::cout << (*it)[j] << " " << std::endl; 
     } 
     ++it; 
    } 
    return 0; 
    } 
}; 

class Root: public tbb::task { 
private: 
public: 
    Root(){} 
    virtual ~Root(){} 
    tbb::task* execute(){ 
    tbb::task_list l; 
    Cont& c = *new (allocate_continuation()) Cont(); 
    l.push_back((*new (c.allocate_child()) Child(0,10,c.GetTls()))); 
    l.push_back((*new (c.allocate_child()) Child(11,21,c.GetTls()))); 
    c.set_ref_count(2); 
    c.spawn(l); 
    return 0; 
    } 
}; 

int main(void) { 
    Root& r = *new(tbb::task::allocate_root()) Root(); 
    tbb::task::spawn_root_and_wait(r); 
    return 0; 
} 

をテストするために、この小さなコードを書かれているが、出力は厄介です。

ETS.SIZE: 2 
*ITSIZE: 10 
0 1 2 3 4 5 6 7 8 9 
*ITSIZE: 10 
11 12 13 14 15 16 17 18 19 20 

、時にはです::たまにある

ETS.SIZE: 1 
*ITSIZE: 20 
0 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20 

その変化が起こるのはなぜ? また、TBBフォーラムでは、TLSにすべての期待値が含まれていないことがあることがありますが、その理由は明らかに親タスクと子タスクの関係です。しかし、あまりにもそれを理解していない。

助けが必要ですか?

ありがとうございます。

答えて

2

表示される「厄介な」出力の差異は、enumerable_thread_specificとは関係ありません。 Childのタスクが2つの異なるスレッド(ケース1の場合)または同じスレッド(ケース2の場合)によって実行されているためです。

+0

あなたの答えをありがとう、Alexey。私が話していたTBBフォーラムの記事は、software.intel.com/en-us/forums/... ここには があります。しかし、ここでは問題が起こります。パーティクル衝突のetsでは、 > bオプションを取得すると、スケジュールには>の衝突がすべて含まれず、子のローカルデータのみが含まれます。 この問題はここでも発生する可能性がありますか?それとも、著者が働くいくつかの悪いコードのために起こったのでしょうか? –

+1

私はそのTBBフォーラム投稿の著者は何か誤解していると思います。私は彼が本当に望んでいることを明確にし、探求するためのいくつかのアイデアを提案しようと、彼に答えました。 –

+0

助けてくれてありがとうAlexey! –

関連する問題