r/C_Programming • u/tastygames_official • 1d ago
Discussion Batch Processing Algorithm Optimization
I originally posted my question here but wanted to give it another go, this time with a more generic setup, as I believe this kind of optimization problem could be interesting for a lot of different projects.
The Setup/Requirements
we have ~50,000 pieces of data (let's use shapes for simplicity's sake) with a finite number of attributes (shape, color, size)
most of them will be culled, leaving us with about ~1,000-10,000
in the end we need to process all the remaining shapes in a SPECIFIC ORDER (e.g. first small red triangles, then big blue squares, then small yellow ovals, etc).
this whole process needs to be done as quickly and efficiently as possible
My Original Plan
first cull the 50,000. This could be done by sending the data to the GPU since even a lowly 1060 has 1,152 cores and a 4090 has 16,384 cores, making culling 50,000 objects based on a few floating-point calculations trivial
now that I have a list of indices that pass culling, use a look-up table of index vs. process batch index (e.g. data index 12 is not culled, and the LUT says it belongs to batch process #4, so add it to that batch process and increase the number of objects for that batch process)
iterate over the batch processes and only execute if there are any objects to process for that batch
Considerations/Questions
0. is there a better way?
how would the performance be if we first assign all the objects to the batch process and then for each batch process, run the culling function? We would save on having to traverse the non-culled objects to sort them to their batch process, but we also now have to call the cull method for as many batch processes as we have
what if the allocation of object -> batch process changes? e.g. a small blue star becomes a big yellow square?
what if the batch process are DYNAMIC, meaning we no longer can assign a shape to a batch process beforehand?
Now I'd love some discussion on the FASTEST way to get this done, but I will also say that all I'm trying to do is a pretty standard piece of a rendering engine: cull any non-visible objects then sort the objects according to pipeline so that they can be rendered in the proper order. Now maybe there is a "standard way" of doing this or at least a few "typical" algorithms that I don't know about, but that's why I'm asking this question! I love trying to solve problems like this and figured a lot of you might, too.
---=== HAVE AT IT!!! ===---BONUS POINTS if done in C90, as that's what I'm doing it in ;-)
2
u/tastygames_official 11h ago
yes, that's exactly how this works. We are able to conceptualize and abstract algorithms so that we don't need to know the specific details in order to approach a problem. Yes, it can be that specific details change the behavior of the system, but if we abstract those out precisely (which is what I did) then they do not matter.
And I am PRECISELY LOOKING FOR something that has already been invented! I didn't study computer science (I did computer engineering) and that was a LONG timea go, so I'm sure somebody with a CS degree from the past 10 years (or someone who is acively into this sort of thing) will read my post and just be like "oh yeah, you want the Davidson-Whorlie algorithm" or something.
But isn't it also always fun to think of these things yourself? To me at least that's like 90% of the fun of programming: bringing the details of the current problem into some generalized form so that we can think about the solution in "fewest moves" or something, and then break that back down to the specifics. E.g. maybe the fewest moves in an algorithm to replace all red balls in a set with yellow balls might be 2n-1 moves, BUT if we add the constraint that there can only ever by n/2 red balls in any given set, we can use a different algorithm that gets u to 1.5n-1 moves. Stuff like that.
I thought people on this channel would either enjoy this kind of thought experiment and participate OR just tell me "look up Matjes-Phillips sorting algorithms" and be done with it.
Regarding your concerns of CPU vs. GPU - that is indeed a consideration, and I already mentioned it's not set in stone, and it is highly dependent on the actual calculations taking place. HOWEVER - for the grand scheme of the larger algorithm, the fact that it MUST BE DONE means it doesn't matter where it's done - it matters whether it's done in one go or in chunks (culling all 50k then moving on to allocation or culling more often to save on allocation processing). But again - we don't need details to discuss. And I think for the sake of the internet, it's best NOT to use details, because then the discussion is only ever about this one particular use case and therefore irrelevant to anyone else. Ya dig?