Is there any simple way to define and access CUDA GPU 2D matrix?
Something like M[i][j]
.
Maybe there is some libraries already?
Is there any simple way to define and access CUDA GPU 2D matrix?
Something like M[i][j]
.
Maybe there is some libraries already?
Usually in CUDA, you will have to convert your arrays to linear memory (in case of 2D arrays, they should be converted to linear memory using cudaMallocPitch
)
If you insist on using the M[i][j] notation, you may allocate arrays on the device as "arrays of arrays". In this case, you will allocate each row of the array using cudaMalloc
and then store the pointer to each row in an array of pointers. You will then have to allocate this array of pointers on the device!
Therefore, when you say M[i], it will give you the pointer for the i
th row and you can use the [j] index of that pointer.
From the guy who was heavily looking into this stuff for the past 3 weeks (aka me!), take it that it's the worst thing you could do. The allocations are scattered all over the global memory and most probably none of them meet CUDA alignment requirements. Therefore the accesses are not fully coalesced and the access latency will kill your kernel's performance. Stick to the linear and pitched memory for best performance. The indexing convention may be a little confusing and awkward at first, but you will get used to it :-)