AtomicAdd double

Sep 2, 2013 at 2:45 PM
Hi,

I want to implement atomic add for double values using CAS from:

http://stackoverflow.com/questions/16077464/atomicadd-for-double-on-gpu

I have no idea how to implement this in CUDAfy with the casts.

How can I achieve this function in C# for CUDAfy?
Sep 2, 2013 at 5:15 PM
Hi
Try this. I didn't test it since my laptop is only arch 1.1.
        [Cudafy(eCudafyType.Device)]
        public static ulong double_as_longlong(double d)
        {
            ulong retval = 0;
            GThread.InsertCode("{0} = (unsigned long long int)__double_as_longlong({1});", retval, d);
            return retval;
        }

        [Cudafy(eCudafyType.Device)]
        public static double longlong_as_double(ulong l)
        {
            double retval = 0;
            GThread.InsertCode("{0} = __longlong_as_double((unsigned long long int){1});", retval, l);
            return retval;
        }

        [Cudafy(eCudafyType.Device)]
        public unsafe static double atomicAdd(ref double d1, double d2)
        {
            fixed (double* d_address = &d1)
            {
                ulong* address_as_ull = (ulong*)d_address;
                ulong old = *address_as_ull;
                ulong assumed;

                do
                {
                    assumed = old;
                    old = thread.atomicCAS(ref *address_as_ull, assumed,
                                    double_as_longlong(d2 +
                                    longlong_as_double(assumed)));
                } while (assumed != old);
                return longlong_as_double(old);
            }
        }