GPU内存更新原子性 [英] GPU memory update atomicity

查看:242
本文介绍了GPU内存更新原子性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要的全局内存( gpu_mem )以及一个变量( gpu_mem_offset 这个全局内存的偏移量,线程将更新其内容。以下脚本保证内存内容和偏移值的原子性?

  __ global__ void kernel(int * gpu_mem,int * gpu_mem_offset)
{
gpu_mem [(* gpu_mem_offset)++] = some_value;
}

更新

我很快意识到上面的脚本是愚蠢的,但我确实描述了我的意图:如何保证内存内容和偏移值的原子性?或者,如何同时锁定他们两个?

$ p

解决方案

在该示例中确保计数器和数组一致更新的唯一方法是: / p>

  __ global__ void kernel(int * gpu_mem,int * gpu_mem_offset)
{
int offset = atomicAdd(gpu_mem_offset ,1);
gpu_mem [offset] = some_value;
}

如果您需要原子更新,则使用原子本征。这就是他们的目的。这里,对 gpu_mem_offset 的原子访问可以确保每个线程获得唯一的偏移值。然后写保证是安全的,因为每个线程访问唯一索引。


I have a main global memory (gpu_mem), along with a variable (gpu_mem_offset) to track the current offset of this global memory where a thread will update its content. Will the following script guarantee the atomicity of both the memory content and offset values?

__global__ void kernel(int *gpu_mem, int *gpu_mem_offset)
{
    gpu_mem[(*gpu_mem_offset)++] = some_value;
}

Update

I quickly realized that the above script is stupid, but I did describe my intention: how to guarantee the atomicity of both the memory content and offset values? Or, how to lock both of them at the same time? Maybe not achievable?

解决方案

The only way to ensure a consistent update of both counter and array in that example is like this:

__global__ void kernel(int *gpu_mem, int *gpu_mem_offset)
{
    int offset = atomicAdd(gpu_mem_offset, 1);
    gpu_mem[offset] = some_value;
}

i.e. if you need atomic updates, then use an atomic intrinsic. That is what they are for. Here the atomic access to gpu_mem_offset ensures every thread gets a unique value of the offset. Then the write is guaranteed to be safe, because each thread accesses a unique index.

这篇关于GPU内存更新原子性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆