Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maxlevesque/a5c3ec08ddd8726682a2ae7c1a92235b to your computer and use it in GitHub Desktop.
Save maxlevesque/a5c3ec08ddd8726682a2ae7c1a92235b to your computer and use it in GitHub Desktop.
index2coord
/* 2D */
KOKKOS_INLINE_FUNCTION
void index2coord(int index, int &i, int &j, int Nx, int Ny)
{
UNUSED(Nx);
UNUSED(Ny);
#ifdef KOKKOS_ENABLE_CUDA
j = index / Nx;
i = index - j*Nx;
#else
i = index / Ny;
j = index - i*Ny;
#endif
}
KOKKOS_INLINE_FUNCTION
int coord2index(int i, int j, int Nx, int Ny)
{
UNUSED(Nx);
UNUSED(Ny);
#ifdef KOKKOS_ENABLE_CUDA
return i + Nx*j; // left layout
#else
return j + Ny*i; // right layout
#endif
}
/* 3D */
KOKKOS_INLINE_FUNCTION
void index2coord(int index,
int &i, int &j, int &k,
int Nx, int Ny, int Nz)
{
UNUSED(Nx);
UNUSED(Nz);
#ifdef KOKKOS_ENABLE_CUDA
int NxNy = Nx*Ny;
k = index / NxNy;
j = (index - k*NxNy) / Nx;
i = index - j*Nx - k*NxNy;
#else
int NyNz = Ny*Nz;
i = index / NyNz;
j = (index - i*NyNz) / Nz;
k = index - j*Nz - i*NyNz;
#endif
}
KOKKOS_INLINE_FUNCTION
int coord2index(int i, int j, int k,
int Nx, int Ny, int Nz)
{
UNUSED(Nx);
UNUSED(Nz);
#ifdef KOKKOS_ENABLE_CUDA
return i + Nx*j + Nx*Ny*k; // left layout
#else
return k + Nz*j + Nz*Ny*i; // right layout
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment