簡體   English   中英

tbb 中的並行任務

[英]parallel task in tbb

我有以下 function 我想作為並行任務運行:

void WuManberFinder::find() 

所以我寫了以下內容:

void WuManberFinder::Parallel_find()
{

 tbb::task_group g;

 for(auto i = 0; i != tbb::tbb_thread::hardware_concurrency(); ++i) 
 {
  g.run(find);
 }

   g.wait();

 }

但它給了我以下錯誤:

wumanber.cc:139:17: error: no matching function for call to ‘tbb::task_group::run(<unresolved overloaded function type>)’
wumanber.cc:139:17: note: candidates are:
/usr/include/tbb/task_group.h:209:10: note: void tbb::task_group::run(const F&) [with F = void (thru::matching::WuManberFinder::*)()]
/usr/include/tbb/task_group.h:209:10: note:   no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void (thru::matching::WuManberFinder::* const&)()’
/usr/include/tbb/task_group.h:151:10: note: template<class F> void tbb::internal::task_group_base::run(tbb::task_handle<F>&)

這是串行代碼(尚未完成):

 void WuManberFinder::find() {    
    while(m_pos <= m_text2.size()) {        
        string bgram = m_text2.substr(m_pos - m_B, m_B);
        shift_type::iterator i = m_shift.find(bgram);           
    if (i == m_shift.end())
        {// shared variable lock
        m_pos += m_lmin - m_B + 1;
        }// unlock
    else {

        if (i->second == 0) {

            vector<size_t>& list = m_hash[bgram];
            // Verify all patterns in list against the text.

            //shared variable lock
            ++m_pos;
            //unlock         

            for (size_t j = 0; j < list.size(); ++j) {
                string const& str = m_patterns[list[j]];
                m_find_pos = m_pos - str.size() - 1;
                size_t k = 0;

                for (; k < str.size(); ++k)
                    if (str[k] != m_text2[m_find_pos + k])
                        break;

                if (k == str.size()) {
                    m_find_pattern_index = list[j];
                 //  cout << "***" << m_patterns[m_find_pattern_index] <<endl;
                   // return true
                   // shared variable lock
                   m_intrusions++;
                   // unlock
                }
            }
        }
        else
           { // access m_pos shared critical section
            m_pos += i->second;
           } // access m_pos shared critical section
    }
    }

   // return false;  nothing found
     }

我知道這可能看起來很奇怪,但主要思想是我希望這種類型的多個任務遍歷一個字符串。 共享變量是pos 你能給我一個提示嗎?

我不認為你在做什么是個好主意,雖然我無法在不知道 find 應該做什么的情況下更好地解釋。 要使該代碼正常工作,您需要執行以下操作:

g.run(std::bind(&WuManberFinder::find, this));

如果您想使用 TBB 來並行化搜索,我建議您將您的方法基於使用 TBB 的parallel_reduce實現的並行查找示例代碼( TBB 教程的第 3.3.1 節)。 由於在您發布的代碼中提到了鎖和關鍵部分,您確實未能利用使其值得使用的高級 TBB 功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM