我將幾個線程收到的處理結果相結合時遇到了一些問題。我不確定,如果我正確使用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區域內迭代我的結果?