0
$\begingroup$

I'm making simulations like Ant colony simulation, boids, and cellular automata in Go (to learn Go), but I believe this can apply to most languages.

From what I've read (this), using 1D arrays is generally better, but the post only addresses CPU processing. I want to do my simulations with the GPU and that may have some implications I'm not aware of.

In my case, a Map object is a 2D array of Cell objects.

struct Cell {...}
struct Map {
  Cell[][] cells;
}

Is it better to use 1D arrays instead of multi-dimensionals when working with the GPU?

$\endgroup$
4
  • $\begingroup$ I don't really understand about those things, but my guess is that the GPU knows how to do matrix multiplication fast. So depending on the use of 2D arrays, it could be faster $\endgroup$
    – nir shahar
    Commented May 3, 2021 at 11:03
  • 1
    $\begingroup$ Hardware that I worked on rearranged indexes so that small areas would be in few cache lines, like a(I,j) and a(I+1,j-1) would likely be in the same cache line. And the driver padded arrays so that alignment of multiple lines was cache friendly. If you simulated this in a 1d array, you’d lose a lot of performance. $\endgroup$
    – gnasher729
    Commented Jun 2, 2021 at 21:55
  • $\begingroup$ Also make sure you know what type of memory you are using on the GPU (i.e. texture memory, general purpose memory, constant memory, shared memory... to name a few, each having different performance based on the access pattern to the type of memory). Since you may want to mix different memory types for different operations in the simulation. So for instance a common 2D array lookup may in some instances be faster than 1D in texture memory but 1D may be faster than 2D in general purpose, it all highly depends on access patterns of your algorithm. (also take caching into account) $\endgroup$
    – yosmo78
    Commented Sep 26, 2022 at 4:50
  • $\begingroup$ I am afraid that an answer that applies to your particular case can only be found by benchmarking the two approaches. $\endgroup$
    – user16034
    Commented Jan 25, 2023 at 17:26

1 Answer 1

0
$\begingroup$

This is likely going to be dependent on what technology and libraries you use to program the GPU (PyTorch? TensorFlow? CUDA? BLAS? something else?), and thus seems likely to be out of scope here. Typically the data must be transferred/copied onto the GPU anyway, so I'd expect the structure of it in RAM only affects the time to transfer to the GPU's memory, not the time to do the GPU computation once it has been copied into GPU memory in the memory layout that the GPU wants.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.