首页 > 解决方案 > 有什么方法可以在 OpenCL 中使用屏障来实现原子功能?

问题描述

我有一些使用 atomic_inc 的代码,但不幸的是 Xilinx FPGA 中的 OpenCL 支持缺乏原子功能。

Xilinx 提供对屏障的支持(clk_global_mem_fence 和 clk_local_mem_fence)

如果缺少原子函数,您知道有什么方法可以在下面的代码中替换“atomic_inc”吗?我可以创建一些可以在 OpenCL 中使用障碍的函数吗?

非常感谢

__attribute__((reqd_work_group_size(WORKSIZE, 1, 1)))
__kernel void search(__global hash_t* restrict hashes, __global uint* restrict output, const ulong target)
{
    uint gid = get_global_id(0);
    uint offset = get_global_offset(0);
    __global hash_t *hash = &(hashes[gid-offset]);

    bool result = (hash->h8[3] <= target);
    if (result) {
        // TODO(ajaraber): Disable atomic_inc as not available in Xilinx FPGAs
#ifdef ATOMIC_SUPPORTED
        uint i = atomic_inc(output + 0xFF);
#else
        uint i = atomicAdd(output+0xFF, 1);
#endif
        if (i < 0xFF)
            output[i] = SWAP4(gid);
    }

    // TODO(ajaraber): Use memory barrier but only for Xilinx FPGAs
#ifndef ATOMIC_SUPPORTED
    if (WORKSIZE != 1) {
        barrier(CLK_GLOBAL_MEM_FENCE);
    }
#endif
}

标签: c++multithreadingopenclatomicmemory-barriers

解决方案


推荐阅读