Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cuda 为什么我不';在每次atomicAdd之后看不到我的变量值的不同/唯一输出?_Cuda_Atomic - Fatal编程技术网

Cuda 为什么我不';在每次atomicAdd之后看不到我的变量值的不同/唯一输出?

Cuda 为什么我不';在每次atomicAdd之后看不到我的变量值的不同/唯一输出?,cuda,atomic,Cuda,Atomic,我是CUDA的新手。 我试图在GPU上实现一个trie数据结构,但没有成功。我注意到我的atomicAdd没有像我预期的那样工作。 所以我用atomicAdd做了一些实验。我写了这段代码: #include <cstdio> //__device__ int *a; //I also tried the code with using this __device__ //variable and allocating it inside k

我是CUDA的新手。 我试图在GPU上实现一个trie数据结构,但没有成功。我注意到我的atomicAdd没有像我预期的那样工作。 所以我用atomicAdd做了一些实验。我写了这段代码:

#include <cstdio>

//__device__ int *a; //I also tried the code with using this __device__
                     //variable and allocating it inside kernel instead
                     //using cudaMalloc. Same Result

__global__ void AtomicTestKernel (int*a)
{
    *a = 0;
    __syncthreads();
    for (int i = 0; i < 2; i++)
    {
        if (threadIdx.x % 2)
        {
            atomicAdd(a, 1);
            printf("threadsIndex = %d\t&\ta : %d\n",threadIdx.x,*a);
        }
        else
        {
            atomicAdd(a, 1);
            printf("threadsIndex = %d\t&\ta : %d\n", threadIdx.x, *a);
        }
    }
}

int main()
{
    int * d_a;
    cudaMalloc((void**)&d_a, sizeof(int));

    AtomicTestKernel << <1, 10 >> > (d_a);

    cudaDeviceSynchronize();

    return 0;
}

由于所有指令在一个扭曲内同时执行,因此您的代码正在执行所有原子指令,然后执行printf,因此,您正在读取所有原子操作的结果

下面是一个warp中指令的解释:

Instruction | threadId 1 | threadId 2 | *a ____________________________________________________________ AtomicAdd | increasing value | waiting | 1 waiting | increasing value | 2 ---------------------------------------------- Warp finished instruction of all AtomicAdd reading *a | read value | read value | 2
这里有一些信息:

由于所有指令在一个扭曲中同时执行,因此代码执行所有原子指令,然后执行printf,因此,您正在读取所有原子操作的结果

下面是一个warp中指令的解释:

Instruction | threadId 1 | threadId 2 | *a ____________________________________________________________ AtomicAdd | increasing value | waiting | 1 waiting | increasing value | 2 ---------------------------------------------- Warp finished instruction of all AtomicAdd reading *a | read value | read value | 2
您可以在这里获得一些信息:

谢谢!是的,我试过了,我看到了独特的价值。谢谢!是的,我试过了,我看到了独特的价值观。
int previousValue = atomicAdd(a, 1);