tbb :: concurrent_hash_map - нужна помощь для начала работы - PullRequest
3 голосов
/ 16 января 2012

Я пытаюсь начать работу с TBB.

Я хочу реализовать параллельную хэш-карту (concurrent_hash_map)

Она должна иметь ключ с длинным int и возвращать символ *...

Вот код, который у меня есть:

#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);
}

[править: авторское решение удалено как ответ вики сообщества]

1 Ответ

1 голос
/ 05 мая 2014

Работает сейчас:

...
#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;
}
...