/******************************************************************* QuickSort : Perform the quick sort operation. If size of array is : below the threshold, then use bubble sort, else : divide the job and insert one part into the task stack *******************************************************************/ QuickSort(register int i, register int j) { register int pivot, k; QSORT: /* pivot is index of the pivot element */ if(j-i+1 < BubbleThresh) { if(bubble) BubbleSort(i,j); else LocalQuickSort(i,j); return; } pivot = FindPivot(i,j); k = Partition(i,j,gMem->A[pivot]); if(k-i > j-k) { /* The lower half [i through (k-1)] contains more entries. */ /* Put the other half into the queue of unsorted subarrays */ /* and recurse on the other half. */ PushWork(k,j); j=k-1; goto QSORT; /* QuickSort(i, k-1); */ /* eliminating tail recursion */ } else { PushWork(i,k-1); i=k; goto QSORT; /* QuickSort(k,j); */ /* Eliminating tail recursion */ } }
// // void QuickSort(int,int); // void QuickSort(int i, int j){ int pivot, k; if (debug) { fprintf(stderr,"\t%d: QuickSorting %d-%d.\n", Tmk_proc_id, i, j); } // pivot is index of the pivot element if (j-i+1 < BubbleThresh) { if (bubble) { BubbleSort(i,j); } else { LocalQuickSort(i,j); } return; } pivot = FindPivot(i,j); k = Partition(i, j, gMem->A[pivot]); if(debug){ fprintf(stderr,"\t%d: Splitting at %d-%d:%d-%d\n", Tmk_proc_id,i,k-1,k,j); } #define FAST_SEQUENTIAL #ifdef FAST_SEQUENTIAL if (Sequential) { QuickSort(i, k-1); QuickSort(k, j); return; } #endif // FAST_SEQUENTIAL if (k-i > j-k) { // The lower half [i through (k-1)] contains more entries. // Put the other half into the queue of unsorted subarrays // and recurse on the other half. PushWork(k, j); QuickSort(i, k-1); } else { // The lower half [i through (k-1)] contains more entries. // Put the other half into the queue of unsorted subarrays // and recurse on the other half. PushWork(i,k-1); QuickSort(k,j); } }
// // void InitData(void); // // do this in single-thread mode // void InitData(void){ int i; gMem = (GlobalMemory *)malloc(sizeof(GlobalMemory)); if(gMem == NULL){ fprintf(stderr, "Unable to allocate memory for gMem\n"); exit(-1); } memset(gMem, '\0', sizeof(gMem)); // Create locks and give hints. if (!quiet){ printf("\tROOT: Creating locks, barriers, and condition vars.\n"); } #ifdef NOTDEF // Chuck's comments: not defined in the source code? // not sure if they are part of TMK system call //TaskStackLock = CreateLock(); // Lock number 0x1000 //AssociateDataAndSynch(TaskStack, TaskStackLock); //AssociateDataAndSynch(&gMem->TaskStackTop, TaskStackLock); #endif // NOTDEF INITPAUSE(pauseFlag, 2); gMem->TaskStackLock = 1; // All of the elements are unique. for (i = 0; i < size; i++){ gMem->A[i] = i; } //Shuffle data randomly. #ifdef SWAP for (i = 0; i < size; i++) { SWAP(gMem->A, i, (lrand48() % (size-i)) + i); } #else for (i = 0; i < size; i++) { Swap(i, (random() % (size-i)) + i); } #endif gMem->TaskStackTop = 0; gMem->NumWaiting = 0; // Special: On this node, there are *2* threads (worker and master). if (!quiet){ printf("\tSorting %d-entry array on %d procs! Bubble thresh: %d.\n", size, Tmk_nprocs, BubbleThresh); } // Push the initial value. PushWork(0, size-1); }