如何使用英特尔 TBB 并发无序列图

how to use intel tbb concurrent unordered map

本文关键字:无序 并发 TBB 何使用 英特尔      更新时间:2023-10-16

以下是我用来开始学习如何使用Intel TBB中的concurrent容器的代码。

代码只是尝试填充tbb::concurrent_unordered_map

我已经使用以下命令在 Ubuntu 上安装了Intel TBBsudo apt-get install libtbb-dev

但是,当我将代码编译为: g++ -std=c++11 -fopenmp -ltbb concur_hash.cpp

#include <iostream>
#include <vector>
#include <omp.h>
#include "tbb/concurrent_unordered_map.h"
int main() {
    tbb::concurrent_unordered_map<int, int> ht;
    std::vector<std::pair<int, int>> vec;
    vec.push_back({1,1});
    vec.push_back({1,9});
    vec.push_back({2,1});
    vec.push_back({2,2});
    vec.push_back({2,7});
    vec.push_back({3,0});
    vec.push_back({3,0});
    vec.push_back({3,0});
    vec.push_back({3,0});
    vec.push_back({3,10});
    const int vec_size = vec.size();

    #pragma omp parallel for
    for (int i = 0; i < vec_size; ++i) {
        auto it = ht.find(vec[i].first); 
        if (it != ht.end()) {
            it->second += vec[i].second;
        }   
        else { ht.insert(vec[i]); }
    }
    for (auto kv : ht) {
        std::cout << "k: " << kv.first << "t v: " << kv.second << std::endl;
    }
    return 1;
}

它抛出以下错误,我无法理解。

`/tmp/cckL17EZ.o: In function `tbb::tbb_allocator<tbb::interface5::internal::flist_iterator<tbb::interface5::internal::split_ordered_list<std::pair<int const, int>, tbb::tbb_allocator<std::pair<int const, int> > >, std::pair<int const, int> > >::deallocate(tbb::interface5::internal::flist_iterator<tbb::interface5::internal::split_ordered_list<std::pair<int const, int>, tbb::tbb_allocator<std::pair<int const, int> > >, std::pair<int const, int> >*, unsigned long)':
concur_hash.cpp:(.text._ZN3tbb13tbb_allocatorINS_10interface58internal14flist_iteratorINS2_18split_ordered_listISt4pairIKiiENS0_IS7_EEEES7_EEE10deallocateEPSA_m[_ZN3tbb13tbb_allocatorINS_10interface58internal14flist_iteratorINS2_18split_ordered_listISt4pairIKiiENS0_IS7_EEEES7_EEE10deallocateEPSA_m]+0x1c): undefined reference to `tbb::internal::deallocate_via_handler_v3(void*)'
/tmp/cckL17EZ.o: In function `tbb::tbb_allocator<tbb::interface5::internal::split_ordered_list<std::pair<int const, int>, tbb::tbb_allocator<std::pair<int const, int> > >::node>::deallocate(tbb::interface5::internal::split_ordered_list<std::pair<int const, int>, tbb::tbb_allocator<std::pair<int const, int> > >::node*, unsigned long)':
concur_hash.cpp:(.text._ZN3tbb13tbb_allocatorINS_10interface58internal18split_ordered_listISt4pairIKiiENS0_IS6_EEE4nodeEE10deallocateEPS9_m[_ZN3tbb13tbb_allocatorINS_10interface58internal18split_ordered_listISt4pairIKiiENS0_IS6_EEE4nodeEE10deallocateEPS9_m]+0x1c): undefined reference to `tbb::internal::deallocate_via_handler_v3(void*)'
/tmp/cckL17EZ.o: In function `tbb::tbb_allocator<tbb::interface5::internal::split_ordered_list<std::pair<int const, int>, tbb::tbb_allocator<std::pair<int const, int> > >::node>::allocate(unsigned long, void const*)':
concur_hash.cpp:(.text._ZN3tbb13tbb_allocatorINS_10interface58internal18split_ordered_listISt4pairIKiiENS0_IS6_EEE4nodeEE8allocateEmPKv[_ZN3tbb13tbb_allocatorINS_10interface58internal18split_ordered_listISt4pairIKiiENS0_IS6_EEE4nodeEE8allocateEmPKv]+0x29): undefined reference to `tbb::internal::allocate_via_handler_v3(unsigned long)'
/tmp/cckL17EZ.o: In function `tbb::tbb_allocator<tbb::interface5::internal::flist_iterator<tbb::interface5::internal::split_ordered_list<std::pair<int const, int>, tbb::tbb_allocator<std::pair<int const, int> > >, std::pair<int const, int> > >::allocate(unsigned long, void const*)':
concur_hash.cpp:(.text._ZN3tbb13tbb_allocatorINS_10interface58internal14flist_iteratorINS2_18split_ordered_listISt4pairIKiiENS0_IS7_EEEES7_EEE8allocateEmPKv[_ZN3tbb13tbb_allocatorINS_10interface58internal14flist_iteratorINS2_18split_ordered_listISt4pairIKiiENS0_IS7_EEEES7_EEE8allocateEmPKv]+0x20): undefined reference to `tbb::internal::allocate_via_handler_v3(unsigned long)'
collect2: error: ld returned 1 exit status`

尝试g++ -std=c++11 -fopenmp concur_hash.cpp -ltbb