Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2477 Discussions

Sorting a concurrent_hash_map container by its key

ricarte
Beginner
550 Views
I am new to the TBB library with a moderate programming skillset and I am having problems figuring out how to sort a concurrent hash map by its key without messing up the hash associations. Any help would be appreciated.
0 Kudos
4 Replies
geoffrey-burling
Employee
550 Views
Quoting - ricarte
I am new to the TBB library with a moderate programming skillset and I am having problems figuring out how to sort a concurrent hash map by its key without messing up the hash associations. Any help would be appreciated.

Have a look at this --http://software.intel.com/en-us/forums/showpost.php?p=51548

Geoff
0 Kudos
ricarte
Beginner
550 Views
Quoting - ricarte
I am new to the TBB library with a moderate programming skillset and I am having problems figuring out how to sort a concurrent hash map by its key without messing up the hash associations. Any help would be appreciated.

Won't this just iterate through the map independent of the order of the keys. I'm looking to be able to iterate through the map so that the pair retrieved is in the correct order of their integer keys ( 0 , 1, 2 ...). So I guess what I am looking for is to sort the hash map keys so that theyare ordered(if possible) or be able to itterate the unordered concurrent hash map sequentially relative to its keys.
0 Kudos
ricarte
Beginner
550 Views
Quoting - ricarte

Won't this just iterate through the map independent of the order of the keys. I'm looking to be able to iterate through the map so that the pair retrieved is in the correct order of their integer keys ( 0 , 1, 2 ...). So I guess what I am looking for is to sort the hash map keys so that theyare ordered(if possible) or be able to itterate the unordered concurrent hash map sequentially relative to its keys.

UPDATE: The keys are type double. The key,value pair is inserted in a sequential order from lowest to highest. Since it is a hash map, the end result is an unordered map. I would like to be able to use the concurrent_hash_map in algorithms such as a linear interpolator that requires an ordered sequence of data to iterate through.
0 Kudos
robert_jay_gould
Beginner
550 Views
Quoting - ricarte

UPDATE: The keys are type double. The key,value pair is inserted in a sequential order from lowest to highest. Since it is a hash map, the end result is an unordered map. I would like to be able to use the concurrent_hash_map in algorithms such as a linear interpolator that requires an ordered sequence of data to iterate through.


Ricarte hashmaps by definition don't have to be sorted, so iterating in sequential order isn't easy, because it's not in their nature. Less so with a concurrent hash, concurrency and sorting aren't the best of friends. Also note that hashmaps store their key-value pairs according to a hash function, and not a "lesser than" operator as std::map uses, so my guess is this may not even be possible.

However you could have a function that creates a temporary sorted container as an intermediate step, but it won't be concurrent, and until you finish sorting the entire hashmap, you have to wait, thus its a very bad solution, you'd be better off looking at the problem from a new angle, or perhaps use another container instead of a hash, I think there actually is an experimental sorted_map container somewhere within TBB, that might be the container you're looking for.


0 Kudos
Reply