Results 1 to 8 of 8

Thread: Kernels and mutexes

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    7

    Kernels and mutexes

    The goal of a mutex is to make sure only one work-item can access critical data at a time. I've tried to implement a mutex with the atomic_cmpxchg function, which atomically performs the ternary operation (*x == y) ? z : *x

    To test the mutex, I tried the following code:

    Code :
    __local mutex;
    mutex = 0;
     
    while(atomic_cmpxchg(&mutex, 0, 1) == 1);
    ...
    atomic_xchg(&mutex, 0);

    The while statement checks the value of mutex. If the value is 1 (the locked state), it keeps waiting. But if the value is 0 (the unlocked state), the work-item should set the value to 1 and proceed to the following code. Then the last line sets the mutex to 0 (the unlocked state).

    But when I run this code, my graphics card hangs. Has anyone else tried to implement a mutex in OpenCL?

  2. #2
    Junior Member
    Join Date
    Aug 2010
    Posts
    5

    Re: Kernels and mutexes

    Does the following help you? Creating OpenCL semaphors

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    7

    Re: Kernels and mutexes

    That link is very informative, but I'm looking for a good way to implement a spinlock, where one work-item waits repeatedly for the lock to be available.

    I've tried implementing this with atomic_cmpxchg, but it's not working. The work-items wait, but they never access the lock.

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    1

    Re: Kernels and mutexes

    I don't see anything particularly wrong with your code. It looks ok.

    But, I have to say @daa did give a link to an article in which they do the exact same thing with atomic_xchg(). It's a spinlock too, even though the author calls it a semaphore. Also, his solution seems a little more straightforward.

    Try running the code from the link @daa gave you. If it hangs too, then you probably have an issue with the OpenCL implementation that you use. Otherwise just use the atomic_xchg() solution.

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    3

    Re: Kernels and mutexes

    I have come to a similar problem even with code really similar to the one posted by daa (on the Linux machine where I run the code there is not Mono so I can't run the C# example).
    After some time I have realized what could be the problem: The first work-item succesfully locks the spinlock, the second finds it locked (so far everything is OK). However, because of the SIMD fashion of instructions execution, the cycle executed by second work-item blocks also the first work-item (because there is no scheduling as on CPU and all work-items execute the same instructions). This causes the first work-item to never unlock the spinlock and therefore it hangs.
    Can anybody confirm that? I experience the hangs on NVidia GeForce GTX 580, my own ATI Radeon Mobile 4500 does not support atomic instructions to test it
    I have posted the problem on NVidia's forums but according to other topics, the OpenCL implementors from NVidia do not reply very often there... http://forums.nvidia.com/index.php?showtopic=197738

  6. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    7

    Re: Kernels and mutexes

    I think I've fixed my problems. I've coded a global memory barrier that synchronizes work-items in different work-groups. The important functions are atom_cmpxchg and atom_xchg, and these are available if the device supports cl_khr_int64_base_atomics or cl_khr_global_int32_base_atomics.

    Here's the theory. Software barriers generally have two stages. First, each thread atomically increments a count. Then, each thread atomically checks the count to see if it equals the total number of threads. If it does, each of the threads can proceed. If not, each thread continues reading the count.

    Here's my kernel code:

    Code :
    #pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
     
    #define LOCK(a) atom_cmpxchg(a, 0, 1)
    #define UNLOCK(a) atom_xchg(a, 0)
     
    __kernel void mutex_test(__global int *count, __global int *mutex) {
     
       if(get_local_id(0) == 0) {
     
          /* Increment the count */
          while(LOCK(mutex));
          *count += 1;
          UNLOCK(mutex);
     
          /* Wait for everyone else to increment the count */
          int waiting = 1;
          while(waiting) {
             while(LOCK(mutex));
             if(*count == get_num_groups(0)) {
                waiting = 0;
             }
             UNLOCK(mutex);
          }
       }
    }

    I've tested this with an AMD 5850 on Linux, and it works. I'd really like to know it works on other systems. If so, we can use this barrier instead of redeploying kernels every time we want to synchronize multiple work-groups.

    One last thing. If the moderator SteveBaker is the same person who writes the sjbaker.org site, I'd like to extend my gratitude. That's a great site.

  7. #7
    Junior Member
    Join Date
    Apr 2011
    Posts
    3

    Re: Kernels and mutexes

    How can you be sure that the other work-groups will be run? You could be looping forever waiting for other work-groups which will be never scheduled. Try your code with a lot of work-groups (e.g. > 16000), I beg it will hang...

    I think that if OpenCL explicitely forbids work-groups synchronization, you should not try such non-portable sometimes working hacks.

    By the way, for the discussion above, I have solved the problem with inter-warp locking. See
    http://forums.amd.com/devforum/messa...&enterthread=y.

  8. #8
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: Kernels and mutexes

    How can you be sure that the other work-groups will be run? You could be looping forever waiting for other work-groups which will be never scheduled. Try your code with a lot of work-groups (e.g. > 16000), I beg it will hang...

    I think that if OpenCL explicitely forbids work-groups synchronization, you should not try such non-portable sometimes working hacks.
    flavius is completely right. The code you have posted is non-portable and it's certain that it will deadlock for large enough NDRanges.
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

Similar Threads

  1. Native Kernels
    By wscrosswy in forum OpenCL
    Replies: 0
    Last Post: 07-03-2012, 09:50 AM
  2. Can Native Kernels Enqueue Non-Native Kernels?
    By sean.settle in forum OpenCL
    Replies: 3
    Last Post: 02-20-2012, 07:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •