我的Syncthreads功能似乎是一个**

问题描述 投票:0回答:0

所以我首先在 GPU 上使用 CUDA 尝试了 Bitonic Sort,并且成功运行。我也有 CUDA 合并排序,效果也很好。相同的函数,当我将它们组合成一个项目并在 if else 选择中运行时,我的 Bitonic Sort GPU 函数上的 FIRST __syncthreads() 总是抛出错误。似乎无法弄清楚为什么。

这就是功能

// GPU kernel for Bitonic Sort
global void bitonicSortGPU(int* arr, int size) {
shared int sharedArr[8192];

int tid = threadIdx.x;
int gid = threadIdx.x + blockIdx.x * blockDim.x;

// Load data from global memory to shared memory
if (gid < size) {
    sharedArr[tid] = arr[gid];
}
else {
    // Set out-of-range elements to a large value (sentinel)
    sharedArr[tid] = INT_MAX;
}

// Synchronize to ensure all threads have loaded the data
__syncthreads();

// Bitonic sort algorithm
for (int k = 2; k <= size; k *= 2) {
    for (int j = k / 2; j > 0; j /= 2) {
        int ixj = tid ^ j;

        // Check if the indices are within bounds
        if (ixj < size) {
            // Sort in ascending order
            if (tid < ixj) {
                if ((tid & k) == 0 && sharedArr[tid] > sharedArr[ixj]) {
                    int temp = sharedArr[tid];
                    sharedArr[tid] = sharedArr[ixj];
                    sharedArr[ixj] = temp;
                }
                if ((tid & k) != 0 && sharedArr[tid] < sharedArr[ixj]) {
                    int temp = sharedArr[tid];
                    sharedArr[tid] = sharedArr[ixj];
                    sharedArr[ixj] = temp;
                }
            }
        }

        // Synchronize after each comparison and swap
        __syncthreads();
    }
}

// Copy sorted data back to global memory
if (gid < size) {
    arr[gid] = sharedArr[tid];
}

这就是我调用该函数的方式。我已经在 else 块之外分配了必要的 cuda 容器,并在外部之后取消分配。

else
{
// GPU variables
int blocksPerGrid = (size + threadsPerBlock - 1) / threadsPerBlock;

    cudaEventRecord(startGPU);
    bitonicSortGPU <<<blocksPerGrid, threadsPerBlock >>> (gpuArr, size);
    cudaEventRecord(stopGPU);

    // Perform CPU Bitonic Sort and measure time
    startCPU = clock();
    bitonicSortCPU(carr, size);
    endCPU = clock();
}

尝试在每个函数的 else 块中引入所有与 cuda 相关的分配和释放。尝试重写该函数(尽管它已经单独运行),但没有任何效果。请帮忙。

c++ cuda synchronization thread-synchronization
© www.soinside.com 2019 - 2024. All rights reserved.