You are all right; if you want to avoid modulation (since it is very expensive on gpus), you can just do with j what you did with i :
j = (index - (k*WIDTH*HEIGHT))/WIDTH
If you want the logic to be a little clearer and not need the original index , you can do
k = index/(WIDTH*HEIGHT); index -= k*WIDTH*HEIGHT; j = index/WIDTH; index -= j*WIDTH; i = index/1;
which then quite easily extends to arbitrary sizes. You can try changing the settings above by performing tasks such as precomputing WIDTH*HEIGHT , say, but I would just turn on the optimization and trust the compiler to do this for you.
The proposals for rounding to degree 2 are correct in the sense that this will speed up the calculation of the index, but at a rather significant price. In this (not so bad) case WIDTH=HEIGHT=100 this will increase the memory requirements of your 3D array by 60% ( WIDTH=HEIGHT=128 ), and the memory on the GPU is usually already dense; and creating arrays twice can lead to problems with bank conflicts, depending on your access patterns.
source share