相关疑难解决方法(0)

在CUDA中实现关键部分

我正在尝试使用原子指令在CUDA中实现一个关键部分,但我遇到了一些麻烦.我创建了测试程序来显示问题:

#include <cuda_runtime.h>
#include <cutil_inline.h>
#include <stdio.h>

__global__ void k_testLocking(unsigned int* locks, int n) {
    int id = threadIdx.x % n;
    while (atomicExch(&(locks[id]), 1u) != 0u) {} //lock
    //critical section would go here
    atomicExch(&(locks[id]),0u); //unlock
}

int main(int argc, char** argv) {
    //initialize the locks array on the GPU to (0...0)
    unsigned int* locks;
    unsigned int zeros[10]; for (int i = 0; i < 10; i++) {zeros[i] = 0u;}
    cutilSafeCall(cudaMalloc((void**)&locks, sizeof(unsigned int)*10));
    cutilSafeCall(cudaMemcpy(locks, zeros, sizeof(unsigned int)*10, cudaMemcpyHostToDevice));

    //Run …
Run Code Online (Sandbox Code Playgroud)

synchronization cuda locking critical-section

13
推荐指数
2
解决办法
2万
查看次数

标签 统计

critical-section ×1

cuda ×1

locking ×1

synchronization ×1