       long stride_d = 0;
       for (int d=0; d<src_nDim; d++)
       {
+        long src_stride_d = src_stride[d];
         if (d < dim)
         {
-          stride_d = src_stride[d] / size_dim;
+          stride_d = src_stride_d / size_dim;
           coeff = leftover / stride_d;
           leftover -= coeff * stride_d;
           targetIdx += coeff * stride_d * idx_size;

}

__global__ void THCudaTensor_kernel_indexSelect(
   float *tensor, float *src, float* src_stride, float *index, 
   long src_nDim, int dim, long idx_size, long tensor_size, long size_dim
)
{

  indices_ = THCudaTensor_new();
  THCudaTensor_copyLong(indices_, indices);

  nRes = THCudaTensor_nElement(res_);
  dim3 nthreads(16, 16);
  dim3 nblocks(ceil((float)nRes / nIndex / (16*16)));
  
  THCudaCheck(cudaMalloc((void**)&stride_, src->nDimension * sizeof(long)));
  THCudaCheck(cudaMemcpy(stride_, src->stride, src->nDimension * sizeof(long), cudaMemcpyHostToDevice));
  
  THCudaTensor_kernel_indexSelect<<<nblocks, nthreads>>>(
    THCudaTensor_data(res_), THCudaTensor_data(src), 
    stride_, THCudaTensor_data(indices_), 
    src->nDimension, dim, indices->size[1], nRes, src->size[dim]
  );
    
  THCudaCheck(cudaFree(stride_));
  THCudaTensor_free(indices_);
}
