Atomic operations with double, OpenCL

前端 未结 2 2061
臣服心动
臣服心动 2021-01-27 02:45

I would like to know if there\'s a way to implement atomic operations (particularly atomic_add) with double type.

For floats this code works, but atomic_xchg doesn\'t su

2条回答
  •  误落风尘
    2021-01-27 03:27

    I was looking for for the same in the past and I found this: https://github.com/ddemidov/vexcl-experiments/blob/master/sort-by-key-atomic.cpp. At the end I figured out different approach to my problem so I did not use it. Here is the code:

        "#pragma OPENCL EXTENSION cl_khr_fp64: enable\n"
        "#pragma OPENCL EXTENSION cl_khr_int64_base_atomics: enable\n"
        "void AtomicAdd(__global double *val, double delta) {\n"
        "  union {\n"
        "    double f;\n"
        "    ulong  i;\n"
        "  } old;\n"
        "  union {\n"
        "    double f;\n"
        "    ulong  i;\n"
        "  } new;\n"
        "  do {\n"
        "    old.f = *val;\n"
        "    new.f = old.f + delta;\n"
        "  } while (atom_cmpxchg ( (volatile __global ulong *)val, old.i, new.i) != old.i);\n"
        "}\n"
        "kernel void atomic_reduce(\n"
        "  ulong n,\n"
        "  global const int    * key,\n"
        "  global const double * val,\n"
        "  global double * sum\n"
        ")\n"
        "{\n"
        "  for(size_t idx = get_global_id(0); idx < n; idx += get_global_size(0))\n"
        "    AtomicAdd(sum + key[idx], val[idx]);\n"
        "}\n",
        "atomic_reduce"
    

提交回复
热议问题