 
 	// Clear the root
 	if (index == 0) {
-		temp[num - 1] = 0;
+		temp[stride - 1] = 0;
 	}
 
 	// Down sweep
-	for (int d = 1; d < num; d *= 2) {
+	for (int d = 1; d < stride; d *= 2) {
 		offset >>= 1;
 		__syncthreads();
 

	data[index] += incr[blockIdx.x];
}

__global__ void exclusiveScan(const int* src, int num, int* dst)
{
	extern __shared__ int temp[];

	int index = threadIdx.x;
	int offset = 1;

	int n = blockIdx.x * blockDim.x + threadIdx.x;
	if (n * 2 >= num) {
		return;
	}

	// Copy input data to shared memory
	temp[2 * index] = src[2 * index + (blockIdx.x * blockDim.x * 2)];
	temp[2 * index + 1] = src[2 * index + 1 + (blockIdx.x * blockDim.x * 2)];

	// Up sweep
	for (int d = num >> 1; d > 0; d >>= 1) {
		__syncthreads();

		if (index < d) {

		int blocksize = 8;
		int blockPerGrid = (x.size() - 1) / blocksize + 1;

		exclusiveScan<<<blockPerGrid, blocksize / 2, blocksize  * sizeof(int)>>>(
			src.ptr(), 
			src.maxNum(),
			blocksize, 
			dst.ptr());

		std::vector<int> tmp(src.maxNum());
		dst.readByNum(&tmp[0], tmp.size());
