版本比较
23ms 实现 5e8 长度前缀和
正文
增加 15 行删除 15 行
9 行未改动1011121314151617181920212223247 行未改动3233343536373839409 行未改动5051525354555657585960616263646566676869707172737475767778798053 行未改动13413513613713813914014114265 行未改动208209210211212213214215216143 行未改动全部展开36036136236336436536636736865 行未改动434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464558 行未改动全部展开9 行未改动## 1. Problem DescriptionWe want to implement the Inclusive Scan:- Given an array $[x_0,x_1,x_2,\ldots,x_n]$,calculate its prefix sum array $[y_0,y_1,y_2,\ldots,y_n]$, where $y_i = \sum_{i=0}^n x_i$.CPU version of C++:```c++void get_presum_cpu(DTYPE *x, DTYPE *y, int len) {DTYPE sum = DTYPE();for (int i = 0; i < len; i++) {sum += x[i];y[i] = sum;}7 行未改动### 2.1. For array length less than 1024The algorithm shown above is an in-place algorithm which operates on the initial array $\{x\}$ that originally contains input elements. Subsequently, it iteratively evolves the content of the array output elements, $\{y\}$.We first write a kernel that performs scan on only one section (length of 1024) of the input, which is small enough for one block to handle. We use shared memory to achieve IO efficiency.```c++9 行未改动for (unsigned int stride = 1; stride < blockDim.x; stride <<= 1) {__syncthreads();state ^= 1;if (threadIdx.x >= stride)XY[threadIdx.x + BLOCK_SIZE * state] =XY[threadIdx.x + BLOCK_SIZE * (state ^ 1)] +XY[threadIdx.x - stride + BLOCK_SIZE * (state ^ 1)];else XY[threadIdx.x + BLOCK_SIZE * state] = XY[threadIdx.x + BLOCK_SIZE * (state ^ 1)];}Y[i] = XY[threadIdx.x + state * BLOCK_SIZE];}```The loop iterates through the reduction tree (the above picture) for the original `X` array position assigned to a thread. Every thread is responsible for one position in the array. We use a `__syncthreads()` to ensure that all threads have finished their previous iteration of additions in the reduction tree before any of them starts the next iteration. We add **double buffer** in the shared memory to avoid competitive IO, this can be achieved by simply extending the length of shared memory to its twice. When the stride value exceeds the `threadIdx.x` value of a thread, the assigned X position of the thread is supposed to have accumulated all required input values.The result is shown below:- - This version of parallel scan only works for array whose length is less than 1024 (so as to be stored in one block).- It is seen that this version achieves no speed-up for the small-size data, However, it is used to justify our kernel is correct under this circumstance.Now, we'll consider extend this version of scan to process arbitray size of data. However, we cannot easily synchronize among the blocks, thus we need to further change the way of implementation.### 2.2 For arbitrary array lengthDespite we cannot easily synchronize among blocks during the computing process, instead, we can synchronize in the **caller** function by hand, as is shown:`Version i:````c++53 行未改动```We divide different iterations mannually in the caller's `for`, then we use double buffer to avoid competitive IO among blocks. However, we have to call `cudaMemcpyDeviceToDevice()` to copy buffers' data. This may cost lots of time. We can further improve it into version ii, which involves copying data in the kernel function.`Version ii`:```c++__global__ void Scan_kernel_naive_double_buffer_1(DTYPE *X, DTYPE *Y, int len, int stride) {int i = blockIdx.x * blockDim.x + threadIdx.x;65 行未改动## 3. Work-Efficient Scan### 3.1 For array length less than 1024Work-efficient scan can be achieved mainly in two steps.- The first step is the up-sweep phase which calculates the reduction tree of the given array, the basic idea of which is the same as `reduction` that is mentioned in the class.143 行未改动全部展开CHECK(cudaMemcpy(d_input, input, len * sizeof(DTYPE), cudaMemcpyHostToDevice));GpuTimer timer;timer.Start();// Phase oneWork_efficient_scan_multi_section<<<grid, block>>>(d_input, S, len);// Phase twocaller_double_buffer_S(S, SECTION_NUM);65 行未改动struct GpuTimer{cudaEvent_t start;cudaEvent_t stop;GpuTimer(){cudaEventCreate(&start);cudaEventCreate(&stop);}~GpuTimer(){cudaEventDestroy(start);cudaEventDestroy(stop);}void Start(){cudaEventRecord(start, 0);}void Stop(){cudaEventRecord(stop, 0);}float Elapsed(){float elapsed;cudaEventSynchronize(stop);558 行未改动全部展开9 行未改动1011121314151617181920212223247 行未改动3233343536373839409 行未改动5051525354555657585960616263646566676869707172737475767778798053 行未改动13413513613713813914014114265 行未改动208209210211212213214215216143 行未改动全部展开36036136236336436536636736865 行未改动434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464558 行未改动全部展开9 行未改动## 1. Problem DescriptionWe want to implement the Inclusive Scan:- Given an array $[x_0,x_1,x_2,\ldots,x_n]$,calculate its prefix sum array $[y_0,y_1,y_2,\ldots,y_n]$, where $y_i = \sum_{i=0}^n x_i$.CPU version of C++:```c++void get_presum_cpu(DTYPE *x, DTYPE *y, int len) {DTYPE sum = DTYPE();for (int i = 0; i < len; i++) {sum += x[i];y[i] = sum;}7 行未改动### 2.1. For array length less than 1024The algorithm shown above is an in-place algorithm which operates on the initial array $\{x\}$ that originally contains input elements. Subsequently, it iteratively evolves the content of the array output elements, $\{y\}$.We first write a kernel that performs scan on only one section (length of 1024) of the input, which is small enough for one block to handle. We use shared memory to achieve IO efficiency.```c++9 行未改动for (unsigned int stride = 1; stride < blockDim.x; stride <<= 1) {__syncthreads();state ^= 1;if (threadIdx.x >= stride)XY[threadIdx.x + BLOCK_SIZE * state] =XY[threadIdx.x + BLOCK_SIZE * (state ^ 1)] +XY[threadIdx.x - stride + BLOCK_SIZE * (state ^ 1)];else XY[threadIdx.x + BLOCK_SIZE * state] = XY[threadIdx.x + BLOCK_SIZE * (state ^ 1)];}Y[i] = XY[threadIdx.x + state * BLOCK_SIZE];}```The loop iterates through the reduction tree (the above picture) for the original `X` array position assigned to a thread. Every thread is responsible for one position in the array. We use a `__syncthreads()` to ensure that all threads have finished their previous iteration of additions in the reduction tree before any of them starts the next iteration. We add **double buffer** in the shared memory to avoid competitive IO, this can be achieved by simply extending the length of shared memory to its twice. When the stride value exceeds the `threadIdx.x` value of a thread, the assigned X position of the thread is supposed to have accumulated all required input values.The result is shown below:- - This version of parallel scan only works for array whose length is less than 1024 (so as to be stored in one block).- It is seen that this version achieves no speed-up for the small-size data, However, it is used to justify our kernel is correct under this circumstance.Now, we'll consider extend this version of scan to process arbitray size of data. However, we cannot easily synchronize among the blocks, thus we need to further change the way of implementation.### 2.2 For arbitrary array lengthDespite we cannot easily synchronize among blocks during the computing process, instead, we can synchronize in the **caller** function by hand, as is shown:`Version i:````c++53 行未改动```We divide different iterations mannually in the caller's `for`, then we use double buffer to avoid competitive IO among blocks. However, we have to call `cudaMemcpyDeviceToDevice()` to copy buffers' data. This may cost lots of time. We can further improve it into version ii, which involves copying data in the kernel function.`Version ii`:```c++__global__ void Scan_kernel_naive_double_buffer_1(DTYPE *X, DTYPE *Y, int len, int stride) {int i = blockIdx.x * blockDim.x + threadIdx.x;65 行未改动## 3. Work-Efficient Scan### 3.1 For array length less than 1024Work-efficient scan can be achieved mainly in two steps.- The first step is the up-sweep phase which calculates the reduction tree of the given array, the basic idea of which is the same as `reduction` that is mentioned in the class.143 行未改动全部展开CHECK(cudaMemcpy(d_input, input, len * sizeof(DTYPE), cudaMemcpyHostToDevice));GpuTimer timer;timer.Start();// Phase oneWork_efficient_scan_multi_section<<<grid, block>>>(d_input, S, len);// Phase twocaller_double_buffer_S(S, SECTION_NUM);65 行未改动struct GpuTimer{cudaEvent_t start;cudaEvent_t stop;GpuTimer(){cudaEventCreate(&start);cudaEventCreate(&stop);}~GpuTimer(){cudaEventDestroy(start);cudaEventDestroy(stop);}void Start(){cudaEventRecord(start, 0);}void Stop(){cudaEventRecord(stop, 0);}float Elapsed(){float elapsed;cudaEventSynchronize(stop);558 行未改动全部展开