// // CS 4331: UPC Parallel Permutation Fall, 2008 // // Code the permute the elements of an array with initial contexts // 0, 1, 2, ..., BLK*THREADS-1 // // Each thread swaps the elements it has affinity to with randomly // selected other elements. Race conditions are avoided by // associating a unique lock with each array element. #include #include #include #define BLK 5 shared [BLK] int A[BLK*THREADS]; //upc_lock_t *shared [BLK] lk[BLK*THREADS]; shared [BLK] upc_lock_t *lk[BLK*THREADS]; // shared [BLK] int *shared p[BLK*THREADS]; // shared int *shared [BLK] q[BLK*THREADS]; int main(int argc, char **argv) { int i, s, t; // Generate inital, sorted array elements and allocate locks //&A[i] upc_forall(i=0; i i) { upc_lock(lk[s]); upc_lock(lk[i]); // Swap t = A[i]; A[i] = A[s]; A[s] = t; // Dont forget to unlock upc_unlock(lk[i]); upc_unlock(lk[s]); } } upc_barrier; if (MYTHREAD==0) { // Print permuted array elements for (i=0; i 1) printf("%d occurs %d times\n", i, count); } } }