tbb::concurrent_hash_map - 需要入门帮助

发布于 2024-12-27 01:48:57 字数 2769 浏览 1 评论 0原文

我正在尝试开始使用 TBB。

我想实现一个并发哈希映射(concurrent_hash_map),

它应该用一个 long int 键控并返回一个 char*...

这是我到目前为止的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <gmp.h>
#include <sys/time.h>
#include <omp.h>
#include <iostream>
#include <string.h>

#include "tbb/concurrent_hash_map.h"

using namespace tbb;
using namespace std;

typedef concurrent_hash_map<long int,char*> table;

int main(int argc,char* argv[]){
   /*what to do here?*/
   /*How do I check if key present/add/remove entries to the hash, table?*/
   return 0;
}

请记住,我对 C++ 还很陌生...我已经为此奋斗了一个多小时。我已阅读以下链接:

http://www.devx。 com/cplus/Article/33334/1763/page/2

https: //stackoverflow.com/questions/7656329/how-to-lock-the-whole-concurrent-hash-map-not-warping-a-portion-of-code-with-mut

有人可以吗请指出我正确的方向?

我从文档中得到了这个示例:

#include "tbb/concurrent_hash_map.h"
#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
#include <string>

using namespace tbb;
using namespace std;
// Structure that defines hashing and comparison operations for user's type.
struct MyHashCompare {
    static size_t hash( const string& x ) {
        size_t h = 0;
        for( const char* s = x.c_str(); *s; ++s )
            h = (h*17)^*s;
        return h;
    }
    //! True if strings are equal
    static bool equal( const string& x, const string& y ) {
        return x==y;
    }
};
// A concurrent hash table that maps strings to ints.
typedef concurrent_hash_map<string,int,MyHashCompare> StringTable;
// Function object for counting occurrences of strings.
struct Tally {
    StringTable& table;
    Tally( StringTable& table_ ) : table(table_) {}
    void operator()( const blocked_range<string*> range ) const {
        for( string* p=range.begin(); p!=range.end(); ++p ) {
            StringTable::accessor a;
            table.insert( a, *p );
           a->second += 1;
        }
    }
};
const size_t N = 1000000;
string Data[N];
void CountOccurrences() {
    // Construct empty table.
    StringTable table;
    // Put occurrences into the table
    parallel_for( blocked_range<string*>( Data, Data+N, 1000 ),
    Tally(table) );
    // Display the occurrences
    for( StringTable::iterator i=table.begin(); i!=table.end(); ++i )
        printf("%s %d\n",i->first.c_str(),i->second);
}

[编辑:作者的解决方案已作为社区维基答案移出]

I'm trying to get started with TBB.

I want to implement a concurrent hash map (concurrent_hash_map)

It should be keyed with a long int and return a char*...

Here's the code I have sofar:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <gmp.h>
#include <sys/time.h>
#include <omp.h>
#include <iostream>
#include <string.h>

#include "tbb/concurrent_hash_map.h"

using namespace tbb;
using namespace std;

typedef concurrent_hash_map<long int,char*> table;

int main(int argc,char* argv[]){
   /*what to do here?*/
   /*How do I check if key present/add/remove entries to the hash, table?*/
   return 0;
}

Please bear in mind that I'm quite new to C++... I've been struggling on this for over an hour. I've read the following links:

http://www.devx.com/cplus/Article/33334/1763/page/2

https://stackoverflow.com/questions/7656329/how-to-lock-the-whole-concurrent-hash-map-not-warping-a-portion-of-code-with-mut

Could someone please point me in the right direction?

I got this example from the documentation:

#include "tbb/concurrent_hash_map.h"
#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
#include <string>

using namespace tbb;
using namespace std;
// Structure that defines hashing and comparison operations for user's type.
struct MyHashCompare {
    static size_t hash( const string& x ) {
        size_t h = 0;
        for( const char* s = x.c_str(); *s; ++s )
            h = (h*17)^*s;
        return h;
    }
    //! True if strings are equal
    static bool equal( const string& x, const string& y ) {
        return x==y;
    }
};
// A concurrent hash table that maps strings to ints.
typedef concurrent_hash_map<string,int,MyHashCompare> StringTable;
// Function object for counting occurrences of strings.
struct Tally {
    StringTable& table;
    Tally( StringTable& table_ ) : table(table_) {}
    void operator()( const blocked_range<string*> range ) const {
        for( string* p=range.begin(); p!=range.end(); ++p ) {
            StringTable::accessor a;
            table.insert( a, *p );
           a->second += 1;
        }
    }
};
const size_t N = 1000000;
string Data[N];
void CountOccurrences() {
    // Construct empty table.
    StringTable table;
    // Put occurrences into the table
    parallel_for( blocked_range<string*>( Data, Data+N, 1000 ),
    Tally(table) );
    // Display the occurrences
    for( StringTable::iterator i=table.begin(); i!=table.end(); ++i )
        printf("%s %d\n",i->first.c_str(),i->second);
}

[edit: author's solution is moved out as community wiki answer]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深陷 2025-01-03 01:48:57

现在可以使用了:

...
#include "tbb/concurrent_hash_map.h"

using namespace tbb;
using namespace std;

typedef concurrent_hash_map<long int,char*> data_hash;

int main(){
        data_hash dh;
        data_hash::accessor a;

        long int k=10;
        dh.insert(a,k);
        a->second="hello";
        for(data_hash::iterator j=dh.begin();j!=dh.end(); ++j){
                printf("%lu %s\n",j->first,j->second);
        }
        if(dh.find(a,9)){
                printf("true\n");
        }else{
                printf("false\n");      
        }
        a.release();
        return 0;
}

Got it working now:

...
#include "tbb/concurrent_hash_map.h"

using namespace tbb;
using namespace std;

typedef concurrent_hash_map<long int,char*> data_hash;

int main(){
        data_hash dh;
        data_hash::accessor a;

        long int k=10;
        dh.insert(a,k);
        a->second="hello";
        for(data_hash::iterator j=dh.begin();j!=dh.end(); ++j){
                printf("%lu %s\n",j->first,j->second);
        }
        if(dh.find(a,9)){
                printf("true\n");
        }else{
                printf("false\n");      
        }
        a.release();
        return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文