-2

In CUDA, OpenCL, OpenGL computer shaders, and webgl, is it possible to create an one-pass algorithm which, given a big array/buffer, will return true if a specific value/byte is present in the array?

MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
  • [thrust::find](http://thrust.github.io/doc/group__searching.html#ga99c7a59cef5b9f4cdbc70f37b2e221be) can do this, pretty much (it returns a location or "end" if not found, but that is easy to convert to true/false). I also believe it would be trivial to write a cuda or openCL kernel that does this. Your question seems to require an answer that addresses 4 different domains. – Robert Crovella May 30 '14 at 17:38
  • @RobertCrovella would it be preferred to ask the same question 4 times? – MaiaVictor May 30 '14 at 17:38
  • 2
    It strikes me as a rather broad question. Perhaps someone will know the answer in all 4 domains. – Robert Crovella May 30 '14 at 17:41
  • Are you really wanting a yes/no answer about the possibility that an algorithm exists in four disparate environments (two parallel computation, and two 3D graphics)? Does that make even the slightest sense? – talonmies May 30 '14 at 18:06
  • Yes, it does. Technically, OpenGL and webGL can be used for GPGPU such as this. I believe that he's looking for uniform solution that could be applied with all these technologies. – Dragan Okanovic May 30 '14 at 18:18
  • @Closers, if my question is "unclear" for you, please make a comment explaining what you do not understand so I can work for making it better. – MaiaVictor May 30 '14 at 19:50
  • @talonmies as he said, it makes complete sense. The 4 technologies presented can be used for the same kind of parallel computation and yes, I do want that answer. – MaiaVictor May 30 '14 at 19:52

3 Answers3

2

Yes, for at least 3 of your 4 domains.

For CUDA, OpenCL, and OpenGL computer shaders you could write a kernel that compares a single element from your buffer to your desired value and if they are the same sets the value in an output buffer to 1 (initialize the buffer to 0). After you run your kernel you can check the value in the buffer. If it is 1 then your desired value was found, if still 0 then it wasn't. You don't even need atomic access to the result buffer because it doesn't matter if there are races to set it; anyone can win.

I don't know WebGL so I can't answer for that domain.

Dithermaster
  • 6,223
  • 1
  • 12
  • 20
2

CUDA and OpenCL are equipped with atomic counters and nicely designed kernels that could solve your problem easily. You create and run kernel that would distribute threads to work on several values that would be compared against the value that you're searching for and perhaps use atomic counter to set it (or as Dithermaster said, you don't even have to use atomic counters).

Even OpenGL could exploit compute shader functionality and solve your problem same as two technologies mentioned above.

WebGL has no write-from-shader thingy. The only way is to actually draw something.

Say, for example, fragment thread that tests certain (texture-sampled) value against the one you need to. If it's that value, write white fragment, otherwise - black fragment.

So then, you could perhaps render single point primitive to a 1x1 canvas. Problems is that it won't be too parallel, just few threads will be created. (see this)

So, you need bigger framebuffer/canvas to write to, to exploit more parallelism. You do that, create bigger canvas, then you could then generate smallest mip level fo the rendered framebuffer and check to see if that image is anything other than black.

(There are problems with rounding numbers, you may actually lose positive result if the framebuffer is too large)

Instead of using only fragment shader, you could try and draw many point primitives, each having certain set of values (array attribute or similar) that would be tested, and pass the result of testing the set to the fragment shader (via varying) and then use fragment shader to blend result with other fragments. So, using vertex shader for testing sets of values, while fragment shaders only write blend result on some simple 1x1 canvas.

Hope this helps.

Community
  • 1
  • 1
Dragan Okanovic
  • 7,509
  • 3
  • 32
  • 48
  • Ah, thank you and @Dithermaster, those were exactly the answers I was expecting. Is there any reason WebGL doesn't have such a write-from-shader thing? Seems like it overly complicates something that looks trivial in the other 3. – MaiaVictor May 30 '14 at 21:43
  • It is based on a much older technologie, OpenGL ES 2.0, which is (roughly said) around OpenGL 2.0. Those versions were written for the hardware of that time, which didn't have non-graphical, computational part. So this would be just an improvisation of the GPGPU. And probably an ugly one. – Dragan Okanovic May 30 '14 at 21:46
0

If the array is sorted, binary search would be a O(log(n)) solution. If the array is not sorted, linear search would be a O(n) solution.

  • This question is about GPU computing, not CPU. – MaiaVictor May 30 '14 at 17:39
  • @Viclib: WebGL is a *javascript* API for web browsers and OpenGL and OpenCL are completely hardware neutral APIs. Only CUDA is (at the moment anyway) specifically tied to GPUs. What is it you really want to ask here?? – talonmies May 30 '14 at 18:08
  • All of those are APIs for interfacing with the GPU and can be used for the same kind of computations. – MaiaVictor May 30 '14 at 19:53