Parallel for loop in CUDA 7.0

With the release of CUDA 7.0 a subset of CP++11 features are available inside of CUDA kernels. These features are

Without these features a for loop inside a kernel looked very confusing, because the user had to deal with the grid size and the block size to access an element of the data array on the device.
__global__ void sum(int* array , int n, int* count){ 
 for (int i = blockDim.x * blockIdx.x + threadIdx.x;
         i < n;
         i += gridDim.x * blockDim.x)
    {
        atomicAdd(&(count[0]), array[i]);
    }
}
With the auto specifier and the range-based for loops the kernel can be rewritten more readable and shorter. Here we need some additional functionality [1] to iterate over the data array, because it is not possible to use the thrust_device_vector inside the kernel and use its iterator. The get_stride_range(0,n) function returns a iterator from 0 to N to iterate over the data array without dealing with the grid size and block size.
__global__ void sum(int* array,int n , int* count){
for (auto i : grid_stride_range(0,n))
        atomicAdd(&(count[0]), array[i]);
For the run time of both for loops, wee see that there is nearly no difference for the computational cost per array item. Thus, the generation and usage of the iterator does not affect the overall computational cost.

Links:
  1. Range-based for loops
  2. Files