Software Archive
Read-only legacy content
17060 Discussions

Cilk plus Critical section and locks

Sushek_Shekar
Beginner
562 Views

Hi All,

I'm a grad student and I have difficulties in implementing critical section and locks in cilkplus. I used cilkplus on the latest gcc. Could someone plese put a simple pseudocode along with the header files that have to be used for critical section and locks, so that I can understnad & implement using my code. Thanks.

Regards,

Sushek Shekar.

0 Kudos
6 Replies
Barry_T_Intel
Employee
562 Views

The limitation to be aware of is that there are no guarantees about which thread your code is working on at any spawn or sync boundary.  CRITICAL_SECTIONs on Windows an mutexes on Linux require the release to be on the same thread as the acquire.

The Cilkscreen tool should issue an error or warning if you're holding a lock at spawn or sync boundary.  You can download if from the Cilk Plus website at the http://cilkplus.org/download page.

    - Barry

0 Kudos
Glenn_E_
Beginner
562 Views

Barry Tannenbaum (Intel) wrote:
 ...mutexes on Linux require the release to be on the same thread as the acquire. 

Does that limitation hold for spinlocks or semaphores?  At the very least, you could implement your own spinlock using gcc's built-in compare-and-swap routine.  A semaphore would be a better choice if you want blocked threads to go to sleep.

0 Kudos
Barry_T_Intel
Employee
562 Views

It probably holds for semaphores, though of course you should check the OS documentation to be sure.

It won't hold for spinlocks, but be aware that the Cilk runtime has its own locks, and holding any kind of lock across a spawn or sync boundary runs the risk of lock order inversion which can lead to a deadlock.

    - Barry

0 Kudos
Sushek_Shekar
Beginner
562 Views

Thanks for the reply.

Consider,

cilk_for (int j=0; j<innerreps/nthreads; j++){

delay(delaylength);

}

1. The function delay(delaylength) has to be implemented in the critical section. How do I use mutexes there?

2. I used #include<cilk_mutex.h> in the header, it says, no such file or directory exists. I did check going back to the insatllation files, there's a file called cilk_mutex.h. but #include<cilk.h> is working fine.

thanks.

0 Kudos
Barry_T_Intel
Employee
562 Views

cilk_mutex.h was provided by cilk++, a prior implementation of Cilk that's no longer supported.  cilk_mutex provided a thin wrapper around a pthread_mutex_t.  We withdrew the feature because it's provided by other tools, like tbb and the new C++ standard.

You can use any locking mechanism supported by your OS.  Wikipedia has a reasonable introduction to critical sections:  http://en.wikipedia.org/wiki/Critical_section .  However, keep my warning about holding a lock across a spawn or sync boundary in mind.  You'll be safe as long as you acquire and release the lock to within the cilk_for loop body.

Note that you can frequently avoid using locks by using reducers, and reducers have some very nice properties that aren't possible with locks.  But that's probably the next topic to be covered, once you've got your locks working.  :o)

    - Barry

0 Kudos
Sushek_Shekar
Beginner
562 Views

Thanks a lot for the reply. It helped me a lot. Will get back if I have any doubts/questions.

Regards,

Sushek Shekar.

0 Kudos
Reply