Parallel Programming Guide for HP-UX Systems

Parallel synchronization
Synchronization tools
Chapter 8 143
MY_UNLOCK = LCK
RETURN
END
In this example, MY_LOCK and MY_UNLOCK are user functions that call the
LOCK_GATE and UNLOCK_GATE intrinsics. The SYNC_ROUTINE directive
prevents the compiler from moving code across the calls to MY_LOCK and
MY_UNLOCK.
Programming techniques such as this are used to implement portable
code across several parallel architectures that support critical sections.
This would be done using different syntax. For example, MY_LOCK and
MY_UNLOCK could simply be modiļ¬ed to call the correct locking and
unlocking functions.
Example 8-2 sync_routine
The following C example achieves the same task as shown in the
previous Fortran example:
#include <spp_prog_model.h>
main() {
int i, n, lck, sum, a[1000];
gate_t lock;
#pragma _CNX sync_routine(mylock, myunlock)
.
.
.
lck = alloc_gate(&lock);
#pragma _CNX loop_parallel(ivar=i)
for(i=0; i<n; i++) {
lck = mylock(&lock);
.
.
.
sum = sum+a[i];
lck = myunlock(&lock);
}
}
int mylock(gate_t *lock) {
int lck;
lck = lock_gate(lock); return lck;
}
int myunlock(gate_t *lock) {
int lck;
lck = unlock_gate(lock);
return lck;
}