Intention is to iterate over an std::unordered_map
in parallel with OpenMP
shown as follows:
std::unordered_map<int, int> ht;
// add key-value pair in the unordered_map
#pragma omp taskloop
for (auto i = ht.begin(); i != ht.end(); ++i) {
std::cout << i->first << "\t" << i->second << std::endl;
}
However, OpenMP
for parallel for doesn't allows !=
for good reasons as explained in this existing stackoverflow post.
One way is to keep an auxiliary data structure like 'std::vector' to keep track of all the keys and do as follows:
std::vector<int> keys_ht;
// add keys in keys_ht
#pragma omp taskloop
for (int i = 0; i < keys_ht.size(); ++i) {
std::cout << keys_ht[i] << "\t" << ht[keys_ht[i]] << std::endl;
}
The above strategy would require extra storage and extra hashing for each element is std::unordered_map
.
Is their any way to avoid creation of this auxiliary data structure?