/* This file contains routines for sorting integers. Values are sorted in place. One can use src/sys/tests/ex52.c for benchmarking. */ #include /*I "petscsys.h" I*/ #include #define MEDIAN3(v,a,b,c) \ (v[a] */ #define SWAP2Data(a,b,c,d,t1,t2,siz) \ do { \ t1=a; a=b; b=t1; \ PetscCall(PetscMemcpy(t2,c,siz)); \ PetscCall(PetscMemcpy(c,d,siz)); \ PetscCall(PetscMemcpy(d,t2,siz)); \ } while (0) /* Partition X[lo,hi] into two parts: X[lo,l) <= pivot; X[r,hi] > pivot Input Parameters: + X - array to partition . pivot - a pivot of X[] . t1 - temp variable for X - lo,hi - lower and upper bound of the array Output Parameters: + l,r - of type PetscInt Notes: The TwoWayPartition2/3 variants also partition other arrays along with X. These arrays can have different types, so they provide their own temp t2,t3 */ #define TwoWayPartition1(X,pivot,t1,lo,hi,l,r) \ do { \ l = lo; \ r = hi; \ while (1) { \ while (X[l] < pivot) l++; \ while (X[r] > pivot) r--; \ if (l >= r) {r++; break;} \ SWAP1(X[l],X[r],t1); \ l++; \ r--; \ } \ } while (0) /* Partition X[lo,hi] into two parts: X[lo,l) >= pivot; X[r,hi] < pivot Input Parameters: + X - array to partition . pivot - a pivot of X[] . t1 - temp variable for X - lo,hi - lower and upper bound of the array Output Parameters: + l,r - of type PetscInt Notes: The TwoWayPartition2/3 variants also partition other arrays along with X. These arrays can have different types, so they provide their own temp t2,t3 */ #define TwoWayPartitionReverse1(X,pivot,t1,lo,hi,l,r) \ do { \ l = lo; \ r = hi; \ while (1) { \ while (X[l] > pivot) l++; \ while (X[r] < pivot) r--; \ if (l >= r) {r++; break;} \ SWAP1(X[l],X[r],t1); \ l++; \ r--; \ } \ } while (0) #define TwoWayPartition2(X,Y,pivot,t1,t2,lo,hi,l,r) \ do { \ l = lo; \ r = hi; \ while (1) { \ while (X[l] < pivot) l++; \ while (X[r] > pivot) r--; \ if (l >= r) {r++; break;} \ SWAP2(X[l],X[r],Y[l],Y[r],t1,t2); \ l++; \ r--; \ } \ } while (0) #define TwoWayPartition3(X,Y,Z,pivot,t1,t2,t3,lo,hi,l,r) \ do { \ l = lo; \ r = hi; \ while (1) { \ while (X[l] < pivot) l++; \ while (X[r] > pivot) r--; \ if (l >= r) {r++; break;} \ SWAP3(X[l],X[r],Y[l],Y[r],Z[l],Z[r],t1,t2,t3); \ l++; \ r--; \ } \ } while (0) /* Templates for similar functions used below */ #define QuickSort1(FuncName,X,n,pivot,t1) \ do { \ PetscCount i,j,p,l,r,hi=n-1; \ if (n < 8) { \ for (i=0; i X[j]) { \ SWAP1(X[i],X[j],t1); \ pivot = X[i]; \ } \ } \ } \ } else { \ p = MEDIAN(X,hi); \ pivot = X[p]; \ TwoWayPartition1(X,pivot,t1,0,hi,l,r); \ PetscCall(FuncName(l,X)); \ PetscCall(FuncName(hi-r+1,X+r)); \ } \ } while (0) /* Templates for similar functions used below */ #define QuickSortReverse1(FuncName,X,n,pivot,t1) \ do { \ PetscCount i,j,p,l,r,hi=n-1; \ if (n < 8) { \ for (i=0; i X[j]) { \ SWAP2(X[i],X[j],Y[i],Y[j],t1,t2); \ pivot = X[i]; \ } \ } \ } \ } else { \ p = MEDIAN(X,hi); \ pivot = X[p]; \ TwoWayPartition2(X,Y,pivot,t1,t2,0,hi,l,r); \ PetscCall(FuncName(l,X,Y)); \ PetscCall(FuncName(hi-r+1,X+r,Y+r)); \ } \ } while (0) #define QuickSort3(FuncName,X,Y,Z,n,pivot,t1,t2,t3) \ do { \ PetscCount i,j,p,l,r,hi=n-1; \ if (n < 8) { \ for (i=0; i X[j]) { \ SWAP3(X[i],X[j],Y[i],Y[j],Z[i],Z[j],t1,t2,t3); \ pivot = X[i]; \ } \ } \ } \ } else { \ p = MEDIAN(X,hi); \ pivot = X[p]; \ TwoWayPartition3(X,Y,Z,pivot,t1,t2,t3,0,hi,l,r); \ PetscCall(FuncName(l,X,Y,Z)); \ PetscCall(FuncName(hi-r+1,X+r,Y+r,Z+r)); \ } \ } while (0) /*@ PetscSortedInt - Determines whether the array is sorted. Not Collective Input Parameters: + n - number of values - X - array of integers Output Parameters: . sorted - flag whether the array is sorted Level: intermediate .seealso: `PetscSortInt()`, `PetscSortedMPIInt()`, `PetscSortedReal()` @*/ PetscErrorCode PetscSortedInt(PetscInt n,const PetscInt X[],PetscBool *sorted) { PetscFunctionBegin; if (n) PetscValidIntPointer(X,2); PetscValidBoolPointer(sorted,3); PetscSorted(n,X,*sorted); PetscFunctionReturn(0); } /*@ PetscSortInt - Sorts an array of integers in place in increasing order. Not Collective Input Parameters: + n - number of values - X - array of integers Notes: This function serves as an alternative to PetscIntSortSemiOrdered(), and may perform faster especially if the array is completely random. There are exceptions to this and so it is __highly__ recommended that the user benchmark their code to see which routine is fastest. Level: intermediate .seealso: `PetscIntSortSemiOrdered()`, `PetscSortReal()`, `PetscSortIntWithPermutation()` @*/ PetscErrorCode PetscSortInt(PetscInt n,PetscInt X[]) { PetscInt pivot,t1; PetscFunctionBegin; if (n) PetscValidIntPointer(X,2); QuickSort1(PetscSortInt,X,n,pivot,t1); PetscFunctionReturn(0); } /*@ PetscSortReverseInt - Sorts an array of integers in place in decreasing order. Not Collective Input Parameters: + n - number of values - X - array of integers Level: intermediate .seealso: `PetscIntSortSemiOrdered()`, `PetscSortInt()`, `PetscSortIntWithPermutation()` @*/ PetscErrorCode PetscSortReverseInt(PetscInt n,PetscInt X[]) { PetscInt pivot,t1; PetscFunctionBegin; if (n) PetscValidIntPointer(X,2); QuickSortReverse1(PetscSortReverseInt,X,n,pivot,t1); PetscFunctionReturn(0); } /*@ PetscSortedRemoveDupsInt - Removes all duplicate entries of a sorted input array Not Collective Input Parameters: + n - number of values - X - sorted array of integers Output Parameter: . n - number of non-redundant values Level: intermediate .seealso: `PetscSortInt()` @*/ PetscErrorCode PetscSortedRemoveDupsInt(PetscInt *n,PetscInt X[]) { PetscInt i,s = 0,N = *n, b = 0; PetscFunctionBegin; PetscValidIntPointer(n,1); PetscCheckSorted(*n,X); for (i=0; i 1) { PetscInt mid = lo + (hi - lo)/2; if (key < X[mid]) hi = mid; else lo = mid; } *loc = key == X[lo] ? lo : -(lo + (key > X[lo]) + 1); PetscFunctionReturn(0); } /*@ PetscCheckDupsInt - Checks if an integer array has duplicates Not Collective Input Parameters: + n - number of values in the array - X - array of integers Output Parameter: . dups - True if the array has dups, otherwise false Level: intermediate .seealso: `PetscSortRemoveDupsInt()`, `PetscSortedCheckDupsInt()` @*/ PetscErrorCode PetscCheckDupsInt(PetscInt n,const PetscInt X[],PetscBool *dups) { PetscInt i; PetscHSetI ht; PetscBool missing; PetscFunctionBegin; if (n) PetscValidIntPointer(X,2); PetscValidBoolPointer(dups,3); *dups = PETSC_FALSE; if (n > 1) { PetscCall(PetscHSetICreate(&ht)); PetscCall(PetscHSetIResize(ht,n)); for (i=0; i 1) { PetscInt mid = lo + (hi - lo)/2; if (key < X[mid]) hi = mid; else lo = mid; } *loc = key == X[lo] ? lo : -(lo + (key > X[lo]) + 1); PetscFunctionReturn(0); } /*@ PetscSortIntWithArray - Sorts an array of integers in place in increasing order; changes a second array to match the sorted first array. Not Collective Input Parameters: + n - number of values . X - array of integers - Y - second array of integers Level: intermediate .seealso: `PetscIntSortSemiOrderedWithArray()`, `PetscSortReal()`, `PetscSortIntWithPermutation()`, `PetscSortInt()`, `PetscSortIntWithCountArray()` @*/ PetscErrorCode PetscSortIntWithArray(PetscInt n,PetscInt X[],PetscInt Y[]) { PetscInt pivot,t1,t2; PetscFunctionBegin; QuickSort2(PetscSortIntWithArray,X,Y,n,pivot,t1,t2); PetscFunctionReturn(0); } /*@ PetscSortIntWithArrayPair - Sorts an array of integers in place in increasing order; changes a pair of integer arrays to match the sorted first array. Not Collective Input Parameters: + n - number of values . X - array of integers . Y - second array of integers (first array of the pair) - Z - third array of integers (second array of the pair) Level: intermediate .seealso: `PetscSortReal()`, `PetscSortIntWithPermutation()`, `PetscSortIntWithArray()`, `PetscIntSortSemiOrdered()`, `PetscSortIntWithIntCountArrayPair()` @*/ PetscErrorCode PetscSortIntWithArrayPair(PetscInt n,PetscInt X[],PetscInt Y[],PetscInt Z[]) { PetscInt pivot,t1,t2,t3; PetscFunctionBegin; QuickSort3(PetscSortIntWithArrayPair,X,Y,Z,n,pivot,t1,t2,t3); PetscFunctionReturn(0); } /*@ PetscSortIntWithCountArray - Sorts an array of integers in place in increasing order; changes a second array to match the sorted first array. Not Collective Input Parameters: + n - number of values . X - array of integers - Y - second array of PetscCounts (signed integers) Level: intermediate .seealso: `PetscIntSortSemiOrderedWithArray()`, `PetscSortReal()`, `PetscSortIntPermutation()`, `PetscSortInt()`, `PetscSortIntWithArray()` @*/ PetscErrorCode PetscSortIntWithCountArray(PetscCount n,PetscInt X[],PetscCount Y[]) { PetscInt pivot,t1; PetscCount t2; PetscFunctionBegin; QuickSort2(PetscSortIntWithCountArray,X,Y,n,pivot,t1,t2); PetscFunctionReturn(0); } /*@ PetscSortIntWithIntCountArrayPair - Sorts an array of integers in place in increasing order; changes an integer array and a PetscCount array to match the sorted first array. Not Collective Input Parameters: + n - number of values . X - array of integers . Y - second array of integers (first array of the pair) - Z - third array of PetscCounts (second array of the pair) Level: intermediate Notes: Usually X, Y are matrix row/column indices, and Z is a permutation array and therefore Z's type is PetscCount to allow 2B+ nonzeros even with 32-bit PetscInt. .seealso: `PetscSortReal()`, `PetscSortIntPermutation()`, `PetscSortIntWithArray()`, `PetscIntSortSemiOrdered()`, `PetscSortIntWithArrayPair()` @*/ PetscErrorCode PetscSortIntWithIntCountArrayPair(PetscCount n,PetscInt X[],PetscInt Y[],PetscCount Z[]) { PetscInt pivot,t1,t2; /* pivot is take from X[], so its type is still PetscInt */ PetscCount t3; /* temp for Z[] */ PetscFunctionBegin; QuickSort3(PetscSortIntWithIntCountArrayPair,X,Y,Z,n,pivot,t1,t2,t3); PetscFunctionReturn(0); } /*@ PetscSortedMPIInt - Determines whether the array is sorted. Not Collective Input Parameters: + n - number of values - X - array of integers Output Parameters: . sorted - flag whether the array is sorted Level: intermediate .seealso: `PetscMPIIntSortSemiOrdered()`, `PetscSortMPIInt()`, `PetscSortedInt()`, `PetscSortedReal()` @*/ PetscErrorCode PetscSortedMPIInt(PetscInt n,const PetscMPIInt X[],PetscBool *sorted) { PetscFunctionBegin; PetscSorted(n,X,*sorted); PetscFunctionReturn(0); } /*@ PetscSortMPIInt - Sorts an array of MPI integers in place in increasing order. Not Collective Input Parameters: + n - number of values - X - array of integers Level: intermediate Notes: This function serves as an alternative to PetscMPIIntSortSemiOrdered(), and may perform faster especially if the array is completely random. There are exceptions to this and so it is __highly__ recommended that the user benchmark their code to see which routine is fastest. .seealso: `PetscMPIIntSortSemiOrdered()`, `PetscSortReal()`, `PetscSortIntWithPermutation()` @*/ PetscErrorCode PetscSortMPIInt(PetscInt n,PetscMPIInt X[]) { PetscMPIInt pivot,t1; PetscFunctionBegin; QuickSort1(PetscSortMPIInt,X,n,pivot,t1); PetscFunctionReturn(0); } /*@ PetscSortRemoveDupsMPIInt - Sorts an array of MPI integers in place in increasing order removes all duplicate entries Not Collective Input Parameters: + n - number of values - X - array of integers Output Parameter: . n - number of non-redundant values Level: intermediate .seealso: `PetscSortReal()`, `PetscSortIntWithPermutation()`, `PetscSortInt()` @*/ PetscErrorCode PetscSortRemoveDupsMPIInt(PetscInt *n,PetscMPIInt X[]) { PetscInt s = 0,N = *n,b = 0; PetscFunctionBegin; PetscCall(PetscSortMPIInt(N,X)); for (PetscInt i=0; i X[j]) { SWAP2Data(X[i],X[j],YY+size*i,YY+size*j,t1,t2,size); pivot = X[i]; } } } } else { /* Two way partition */ PetscInt l = 0,r = hi; pivot = X[MEDIAN(X,hi)]; while (1) { while (X[l] < pivot) l++; while (X[r] > pivot) r--; if (l >= r) {r++; break;} SWAP2Data(X[l],X[r],YY+size*l,YY+size*r,t1,t2,size); l++; r--; } PetscCall(PetscSortIntWithDataArray(l,X,Y,size,t2)); PetscCall(PetscSortIntWithDataArray(hi-r+1,X+r,YY+size*r,size,t2)); } PetscFunctionReturn(0); } /*@ PetscMergeIntArray - Merges two SORTED integer arrays, removes duplicate elements. Not Collective Input Parameters: + an - number of values in the first array . aI - first sorted array of integers . bn - number of values in the second array - bI - second array of integers Output Parameters: + n - number of values in the merged array - L - merged sorted array, this is allocated if an array is not provided Level: intermediate .seealso: `PetscSortReal()`, `PetscSortIntWithPermutation()`, `PetscSortInt()`, `PetscSortIntWithArray()` @*/ PetscErrorCode PetscMergeIntArray(PetscInt an,const PetscInt aI[], PetscInt bn, const PetscInt bI[], PetscInt *n, PetscInt **L) { PetscInt *L_ = *L, ak, bk, k; PetscFunctionBegin; if (!L_) { PetscCall(PetscMalloc1(an+bn, L)); L_ = *L; } k = ak = bk = 0; while (ak < an && bk < bn) { if (aI[ak] == bI[bk]) { L_[k] = aI[ak]; ++ak; ++bk; ++k; } else if (aI[ak] < bI[bk]) { L_[k] = aI[ak]; ++ak; ++k; } else { L_[k] = bI[bk]; ++bk; ++k; } } if (ak < an) { PetscCall(PetscArraycpy(L_+k,aI+ak,an-ak)); k += (an-ak); } if (bk < bn) { PetscCall(PetscArraycpy(L_+k,bI+bk,bn-bk)); k += (bn-bk); } *n = k; PetscFunctionReturn(0); } /*@ PetscMergeIntArrayPair - Merges two SORTED integer arrays that share NO common values along with an additional array of integers. The additional arrays are the same length as sorted arrays and are merged in the order determined by the merging of the sorted pair. Not Collective Input Parameters: + an - number of values in the first array . aI - first sorted array of integers . aJ - first additional array of integers . bn - number of values in the second array . bI - second array of integers - bJ - second additional of integers Output Parameters: + n - number of values in the merged array (== an + bn) . L - merged sorted array - J - merged additional array Notes: if L or J point to non-null arrays then this routine will assume they are of the approproate size and use them, otherwise this routine will allocate space for them Level: intermediate .seealso: `PetscIntSortSemiOrdered()`, `PetscSortReal()`, `PetscSortIntWithPermutation()`, `PetscSortInt()`, `PetscSortIntWithArray()` @*/ PetscErrorCode PetscMergeIntArrayPair(PetscInt an,const PetscInt aI[], const PetscInt aJ[], PetscInt bn, const PetscInt bI[], const PetscInt bJ[], PetscInt *n, PetscInt **L, PetscInt **J) { PetscInt n_, *L_, *J_, ak, bk, k; PetscFunctionBegin; PetscValidPointer(L,8); PetscValidPointer(J,9); n_ = an + bn; *n = n_; if (!*L) { PetscCall(PetscMalloc1(n_, L)); } L_ = *L; if (!*J) { PetscCall(PetscMalloc1(n_, J)); } J_ = *J; k = ak = bk = 0; while (ak < an && bk < bn) { if (aI[ak] <= bI[bk]) { L_[k] = aI[ak]; J_[k] = aJ[ak]; ++ak; ++k; } else { L_[k] = bI[bk]; J_[k] = bJ[bk]; ++bk; ++k; } } if (ak < an) { PetscCall(PetscArraycpy(L_+k,aI+ak,an-ak)); PetscCall(PetscArraycpy(J_+k,aJ+ak,an-ak)); k += (an-ak); } if (bk < bn) { PetscCall(PetscArraycpy(L_+k,bI+bk,bn-bk)); PetscCall(PetscArraycpy(J_+k,bJ+bk,bn-bk)); } PetscFunctionReturn(0); } /*@ PetscMergeMPIIntArray - Merges two SORTED integer arrays. Not Collective Input Parameters: + an - number of values in the first array . aI - first sorted array of integers . bn - number of values in the second array - bI - second array of integers Output Parameters: + n - number of values in the merged array (<= an + bn) - L - merged sorted array, allocated if address of NULL pointer is passed Level: intermediate .seealso: `PetscIntSortSemiOrdered()`, `PetscSortReal()`, `PetscSortIntWithPermutation()`, `PetscSortInt()`, `PetscSortIntWithArray()` @*/ PetscErrorCode PetscMergeMPIIntArray(PetscInt an,const PetscMPIInt aI[],PetscInt bn,const PetscMPIInt bI[],PetscInt *n,PetscMPIInt **L) { PetscInt ai,bi,k; PetscFunctionBegin; if (!*L) PetscCall(PetscMalloc1((an+bn),L)); for (ai=0,bi=0,k=0; ai min) sorted = PETSC_FALSE; PetscCallMPI(MPI_Allreduce(&sorted, is_sorted, 1, MPIU_BOOL, MPI_LAND, comm)); PetscFunctionReturn(0); }