0

I have something stored in std::map, which maps string to vector. Its keys and values looks like

 key      value
 "a"-----[1,2,3]
 "b"-----[8,100]
"cde"----[7,10]

For each thread, it needs to process one query. The query looks like

["a", "b"]

or

["cde", "a"]

So I need to get the value from the map and then do some other jobs like combine them. So as for the first query, the result will be

[1,2,3,8,100]

The problem is, how can threads access the map and find the value by a key?

At first, I tried to store it in global memory. However, It looks like it can only pass arrays from host to device.

Then I tried to use thrust, but I can only use it to store vector.

Is there any other way I can use? Or maybe I ignored some methods in thrust? Thanks!

**Ps: I do not need to modify the map, I just need to read data from it.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • you can pass arrays in global memory from device to host – Robert Crovella Mar 07 '18 at 15:20
  • @RobertCrovella Isn't he talking about std::map? or am I missing something. – aram Mar 07 '18 at 15:51
  • Yes, OP is talking about `std::map`. But OP also states: "At first, I tried to store it in global memory. However, It looks like it can only pass arrays from host to device." In whatever way OP decided that **arrays** could be passed from host to device, I am claiming that similar arrays can also be passed from device to host. I am not referring to `std::map` specifically here, and with respect to the quotation I have excerpted, I don't believe OP was referring specifically to `std::map` there either, when OP used the non-specific word "arrays". I stand by my statement. – Robert Crovella Mar 07 '18 at 15:59
  • 1
    You cannot use `std::map` in device code. However you should be able to realize an alternate form of `std::map` with an array of keys and an array of values, and create the resultant arrays just as OP is describing. The resultant arrays desired by the OP don't appear to be any part of a `std::map` that I can tell. – Robert Crovella Mar 07 '18 at 16:02
  • A possible GPU map implementation is [here](https://devtalk.nvidia.com/default/topic/523766/cuda-programming-and-performance/std-map-in-device-code/) – Robert Crovella Jul 25 '19 at 23:11
  • Another one is [here](https://github.com/NVIDIA/cuCollections). – Robert Crovella Apr 02 '22 at 13:26

1 Answers1

1

I believe it's unlikely you will benefit from doing any of this on the GPU, unless you have a huge number of queries which are all available to you at once, or at least in batches.

If you do not have that many queries, then just transferring the data (regardless of its exact format/structure) will likely be a waste.

If you do have that many queries, the benefit is still entirely unclear, and depends on a lot of parameters. The fact that you've been trying to use std::map for anything suggests (see below for the reason) that you haven't been seriously concerned with performance so far. If that's indeed the case, just don't make your life difficult by using a GPU.


So what's wrong what std::map? Nothing, but it's extremely slow even on the CPU, and this is even worse on the GPU.

einpoklum
  • 118,144
  • 57
  • 340
  • 684