[英]Problems in parallelizing radix sort in C with CUDA
我正在嘗試使用 C 中的 CUDA 來實現一個基數排序算法,以便能夠並行化它; 代碼如下:
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <cuda_profiler_api.h>
int* populateArray(int * arr, int n){
int j=0;
for (int i=n; i>0 ; i--){
arr[j]= i;
j=j+1;
}
return arr;
}
void printArray(int * array, int size){
printf("Ordered array:\n");
for (int j=0; j<size; j++){
printf("%d ", array[j]);
}
printf("\n");
}
__device__ int getMax(int* array, int n) {
int max = array[0];
for (int i = 1; i < n; i++)
if (array[i] > max)
max = array[i];
return max;
}
__device__ void countingSort(int* array,int size,int digit, int index, int* output) {
int count[10]={0};
for (int i = 0; i < size; i++)
count[(array[i] / digit) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
for (int i = size - 1; i >= 0; i--) {
output[count[(array[i] /digit) % 10] - 1] = array[i];
count[(array[i] / digit) % 10]--;
}
for (int i = 0; i < size; i++)
array[i]= output[i];
}
__global__ void radixsort(int* array, int size, int* output) {
// define index
int index = blockIdx.x * blockDim.x + threadIdx.x;
// check that the thread is not out of the vector boundary
if (index >= size) return;
int max = getMax(array, size);
for (int digit = 1; max / digit > 0; digit *= 10){
countingSort(array, size, digit, index, output);
}
}
int main(int argc, char *argv[]) {
// Init array
int n = 1000;
int* array_h = (int*) malloc(sizeof(int) * n);
populateArray(array_h, n);
// allocate memory on device
int* array_dev;
cudaMalloc((void**)&array_dev, n*sizeof(int));
int* output;
cudaMalloc((void**)&output, n*sizeof(int));
// copy data from host to device
cudaMemcpy(array_dev, array_h, n*sizeof(int), cudaMemcpyHostToDevice);
dim3 block(32);
dim3 grid((n-1)/block.x + 1);
printf("Number of threads for each block: %d\n", block.x);
printf("Number of blocks in the grid: %d\n", grid.x);
// Create start and stop CUDA events
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
cudaError_t mycudaerror;
mycudaerror = cudaGetLastError();
// define the execution configuration
radixsort<<<grid,block>>>(array_dev, n, output);
// device synchronization and cudaGetLastError call
mycudaerror = cudaGetLastError();
if(mycudaerror != cudaSuccess) {
fprintf(stderr,"%s\n",cudaGetErrorString(mycudaerror)) ;
//printf("Error in kernel!");
exit(1);
}
// event record, synchronization, elapsed time and destruction
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float elapsed;
cudaEventElapsedTime(&elapsed, start, stop);
elapsed = elapsed/1000.f; // convert to seconds
cudaEventDestroy(start);
cudaEventDestroy(stop);
printf("Number of elements in the array: %d\n", n);
printf("Kernel elapsed time: %.5f s\n", elapsed);
// copy back results from device
cudaMemcpy(array_h, array_dev, n*sizeof(int), cudaMemcpyDeviceToHost);
// print ordered array
printArray(array_h, n);
// free resources on device
cudaFree(array_dev);
cudaFree(output);
// free resources on host
free(array_h);
cudaProfilerStop();
return 0;
}
為了運行它,我使用的是 Google Colab。 每個塊的最大線程數固定為 32(網格變量),而使用的塊數在 main 中根據需要排序的元素數(塊變量)計算。
當我開始更改要排序的數組中的元素數量(main 中存在的變量“n”)時,就會出現問題,因為一旦超過某個閾值,排序將不再正確執行。
為了獲得有關此錯誤執行的更多信息,我還使用了命令
cuda-memcheck
和nvcc -lineinfo
我發現錯誤是由於 kernel 中的這行代碼引起的:
output[count[(array[i] /digit) % 10] - 1] = array[i];
進行了幾次嘗試,錯誤似乎主要在於計算我要寫入的“輸出”數組的索引; 但是,例如,當我嘗試對 32 或 64 個元素進行排序時,不會發生此錯誤。 因此,我想知道我是否在代碼中做錯了什么,或者簡單地說,基數排序在我嘗試的方式中是否不可並行化。 我知道讓每個線程在不使用相對於每個線程的索引的情況下進行排序在計算上是非常繁重的,但首先我想嘗試解決這個問題,然后嘗試總體上優化代碼。
各種經過驗證的方法包括:
感謝您的任何回復。
您似乎已經明白這不是解決此問題的方法:
我知道讓每個線程在不使用相對於每個線程的索引的情況下進行排序在計算上是非常繁重的,但首先我想嘗試解決這個問題,然后嘗試總體上優化代碼。
幾乎任何本質上是串行的代碼都可以放在 CUDA kernel 中,在單線程中運行,它應該產生相同的結果。 然而,這不是編寫 CUDA 代碼的方法; 表現會很慘淡。
在許多情況下,本質上是串行的算法不容易適應並行化。 您的排序方法就是其中之一。 一種(有效)並行化基數排序的典型方法在這里。
盡管如此,將串行算法放入單個線程“應該”工作。 當您在多個線程中(不必要地)運行相同的串行代碼時,就會出現問題。
如果所有線程都完美地步調一致,那么它們都會在相同的指令或時鍾周期內做完全相同的事情。 但是 GPU 不是這樣工作的。 您可以看到的最大“鎖步”是在經線級別(線程塊中有 32 個線程)。 只要你 go 到多個 warp,無論它們是在同一個線程塊還是不同的線程塊中,那么你的串行代碼中的不同點都會有線程,有效地相互踩踏。
一個簡單的證明點是將網格配置更改為單個線程:
dim3 block(1);
dim3 grid(1);
然后錯誤消失,即使對於n = 1000
,並且數組已正確排序。 由於單個經線的典型鎖步性質,這也應該有效:
dim3 block(32);
dim3 grid(1);
某些較大的配置在某些情況下可能會起作用,但是如果塊數量足夠多,最終所有線程都不會同時啟動,這會導致麻煩。
另一種解決方案是使用具有最高有效位基數排序的單個線程將數組分成多個 bin,例如如果使用 base 256,則為 256 個 bin。最低有效位第一基數排序。 為了使其正常工作,數據需要合理統一(至少對於最重要的數字),以便多個 bin 的大小有點相似。
嘗試並行執行初始步驟很復雜。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.