Map 将线程结果与openmp相结合

Map 将线程结果与openmp相结合,map,openmp,Map,Openmp,我在组合从多个线程接收的处理结果时遇到一些问题。我不确定我是否正确使用openmp。下面的代码摘录显示了我代码的openmp部分 参数: 线程专用: it:映射迭代器(时间戳、用户密钥) ite:映射迭代器((时间戳,用户键)/int数量) 线程结果映射:typedef映射 何时、何人:匹配正则表达式(时间戳、用户密钥) 线程间共享: 日志:字符数组 大小:log.size() 标识符、时间戳、用户密钥:boost::regex模式 组合结果映射:typedef映射 因此,通过指定combine

我在组合从多个线程接收的处理结果时遇到一些问题。我不确定我是否正确使用openmp。下面的代码摘录显示了我代码的openmp部分

参数:

线程专用:

it:映射迭代器(时间戳、用户密钥)

ite:映射迭代器((时间戳,用户键)/int数量)

线程结果映射:typedef映射

何时、何人:匹配正则表达式(时间戳、用户密钥)

线程间共享:

日志:字符数组
大小:log.size() 标识符、时间戳、用户密钥:boost::regex模式
组合结果映射:typedef映射

因此,通过指定combined+=结果,在关键部分(我删除了该部分)组合命中量没有问题

但是我怎样才能以同样的方式组合结果图呢?我知道我必须迭代线程映射,但是当我在循环中放置一个“cout”来测试每个线程时,它只调用一次

当我将所有正则表达式设置为“error”时,在本地syslog上的测试运行会给我以下输出(以确保每个标识的行都有一个userkey和一个同名的时间戳):

用于分析访问字符串的模式:

(计算出的命中数来自线程专用计数器,我在上面的代码中删除了这些计数器)


因此,我的4个线程中的每一个都执行一个cout并离开循环,尽管所有线程总共应该有418次命中。那么我做错了什么?我如何在openmp区域内迭代我的结果?

我自己发现了问题,很抱歉问了一些愚蠢的问题

我多次尝试添加同一个键,这就是为什么映射大小没有增加,每个线程只循环一次

编辑:

如果有人对如何组合线程结果的解决方案感兴趣,我就是这样做的。也许你看到了任何可以改进的地方

我刚刚将本地线程结果映射更改为
对(str,str)
的向量

这是完整的openmp代码部分。也许这对任何人都有用:

#pragma omp parallel shared(log, size, identifier, timestamp, userkey) private(it, ite, str_time, str_key, i, id, str_current, when, who, local_res)
    {
#pragma omp for
        for (i = 0 ; i < size ; i++){

            str_current.push_back(log[i]);

            if (log[i] == '\n') {   // if char is newline character
                if (boost::regex_search(str_current, identifier)){  // if current line is access string
                    boost::regex_search(str_current, when, timestamp);  // get timestamp from string
                    str_time = when[0];
                    boost::regex_search(str_current, who, userkey); // get userkey from string
                    str_key = who[0];
                    local_res.push_back((make_pair(str_time, str_key)));    // append key-value-pair(timestamp/userkey)
                    id = omp_get_thread_num();
                    //cout << "tID_" << id << " - adding pair - my local result map size is now: " << local_res.size() << endl;
                }
                str_current = "";
            }
        }

#pragma omp critical
        {
            id = omp_get_thread_num();
            hits += local_res.size();
            cout << "tID_" << id << " had HITS: " << local_res.size() << endl;
            for (i = 0; i < local_res.size(); i++) {
                acc_key = local_res[i].second;
                acc_time = local_res[i].first;
                if(m_KeyDatesHits.count(acc_key) == 0) { // if there are no items for this key yet, make a new entry
                    m_KeyDatesHits.insert(make_pair(acc_key, str_int_MapType()));
                }
                if (m_KeyDatesHits[acc_key].count(acc_time) == 0) { // "acc_time" is a key value, if it doesn't exist yet, add it and set "1" as value
                    m_KeyDatesHits[acc_key].insert(make_pair(acc_time, 1 ));
                    it = m_KeyDatesHits.begin(); // iterator for userkeys/maps
                    ite = m_KeyDatesHits[acc_key].begin(); // iterator for  times/clicks
                } else m_KeyDatesHits[acc_key][acc_time]++; // if userkey already exist and timestamp already exists, count hits +1 for it

            }
        }
    }
#pragma omp parallel shared(日志、大小、标识符、时间戳、用户密钥)private(it、ite、str_time、str_key、i、id、str_current、who、local_res)
{
#pragma omp for
对于(i=0;iTIME(MMM/DD/HH/);USERKEY;HITS
May/25/13;SOMEKEY124345;3
error   Pattern for parsing Timestamp:
error   Pattern for parsing Userkey:
error

 *** Parsing File /var/log/syslog

errortID_0 reducing errortID_1
reducing errortID_2 reducing
errortID_3 reducing

 *** Ok!   ________________   hits :
418   worktime: 0.0253871s
#pragma omp parallel shared(log, size, identifier, timestamp, userkey) private(it, ite, str_time, str_key, i, id, str_current, when, who, local_res)
    {
#pragma omp for
        for (i = 0 ; i < size ; i++){

            str_current.push_back(log[i]);

            if (log[i] == '\n') {   // if char is newline character
                if (boost::regex_search(str_current, identifier)){  // if current line is access string
                    boost::regex_search(str_current, when, timestamp);  // get timestamp from string
                    str_time = when[0];
                    boost::regex_search(str_current, who, userkey); // get userkey from string
                    str_key = who[0];
                    local_res.push_back((make_pair(str_time, str_key)));    // append key-value-pair(timestamp/userkey)
                    id = omp_get_thread_num();
                    //cout << "tID_" << id << " - adding pair - my local result map size is now: " << local_res.size() << endl;
                }
                str_current = "";
            }
        }

#pragma omp critical
        {
            id = omp_get_thread_num();
            hits += local_res.size();
            cout << "tID_" << id << " had HITS: " << local_res.size() << endl;
            for (i = 0; i < local_res.size(); i++) {
                acc_key = local_res[i].second;
                acc_time = local_res[i].first;
                if(m_KeyDatesHits.count(acc_key) == 0) { // if there are no items for this key yet, make a new entry
                    m_KeyDatesHits.insert(make_pair(acc_key, str_int_MapType()));
                }
                if (m_KeyDatesHits[acc_key].count(acc_time) == 0) { // "acc_time" is a key value, if it doesn't exist yet, add it and set "1" as value
                    m_KeyDatesHits[acc_key].insert(make_pair(acc_time, 1 ));
                    it = m_KeyDatesHits.begin(); // iterator for userkeys/maps
                    ite = m_KeyDatesHits[acc_key].begin(); // iterator for  times/clicks
                } else m_KeyDatesHits[acc_key][acc_time]++; // if userkey already exist and timestamp already exists, count hits +1 for it

            }
        }
    }