我将几个线程收到的处理结果相结合时遇到了一些问题。我不确定,如果我正确使用openmp。下面的代码提取显示了我的代码的openmp部分。将线程结果与openmp结合起来
参数:
线程私有:
它:地图迭代器(时间戳,用户钥)
伊特: map迭代((时间戳,用户钥)/ INT量)
thread_result_map: typedef map < userkey(str),timestamp(str)>
的情况下,谁:
日志: char数组
尺寸: log.size()
标识符匹配正则表达式(时间戳,用户钥)
线程之间共享,时间戳,用户密钥: boost ::正则表达式
combined_result_map:的typedef地图< thread_result_map,命中(INT)>
#pragma omp parallel shared(log, size, identifier, timestamp, userkey) private(it, ite, str_time, str_key, vec_str_result, i, id, str_current, when, who, thread_result_map)
{
#pragma omp for
for (i = 0 ; i < size ; i++){
str_current.push_back(log[i]);
if (log[i] == '\n') {
if (boost::regex_search(str_current, identifier)){
boost::regex_search(str_current, when, timestamp);
str_time = when[0];
boost::regex_search(str_current, who, userkey);
str_key = who[0];
thread_result_map.insert(make_pair(str_time, str_key));
}
str_current = ""; //reset temp string
}
}
#pragma omp critical
{
for (it=thread_result_map.begin(); it!=thread_result_map.end(); it++) {
id = omp_get_thread_num();
cout << thread_result_map[it->first] <<
thread_result_map[it->second];
cout << "tID_" << id << " reducing" << endl;
}
}
}
正如你可以看到每个线程都有自己的字符数组的分区,通过线从阵列分析线,如果当前字符串被认定“标识符“,时间戳和用户密钥被添加到线程的私有结果映射(字符串/字符串)。
现在循环后,我有几个线程的私人结果地图。 combined_result_map是地图内的地图。关键是线程结果的键/值的组合,值是该组合的出现次数。
我只解析时间戳的一部分,所以当在1小时内出现多次相同的用户密钥时,计数器将增加。
结果应该是这个样子:
TIME(MMM/DD/HH/);USERKEY;HITS
May/25/13;SOMEKEY124345;3
所以我没有问题的关键部分结合击量(我已删除)通过指定组合+ =结果。
但是我怎样才能以相同的方式结合我的结果地图?我知道我必须遍历线程映射,但是当我在循环内部放置一个“cout”来测试每个线程时,它只会调用一次。
在我的本地syslog测试运行给我下面的输出当我设置的所有正则表达式“错误”(以确保每个标识线将有一个用户钥,并与同名的时间戳):
模式解析访问串:
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
(计算点击来自线程专用柜台,我在代码中删除以上)
因此,我的每个线程都会执行一个cout并离开循环,尽管所有在一起的应该有418个命中。那么我做错了什么?如何从我的openmp区域内迭代我的结果?