Actual source code: ex32.c
1: static char help[] = "A benchmark for testing PetscSortInt(), PetscSortIntSemiOrdered(), PetscSortIntWithArrayPair(), PetscIntSortSemiOrderedWithArray(), and PetscSortIntWithArray()\n\
2: The array is filled with random numbers, but one can control average duplicates for each unique integer with the -d option.\n\
3: Usage:\n\
4: mpiexec -n 1 ./ex32 -n <length of the array to sort>, default=100 \n\
5: -r <repeat times for each sort>, default=10 \n\
6: -d <average duplicates for each unique integer>, default=1, i.e., no duplicates \n\n";
8: #include <petscvec.h>
9: int main(int argc, char **argv)
10: {
11: PetscInt i, l, n = 100, r = 10, d = 1, vsize = 1;
12: PetscInt *X, *X1, *XR, *XSO, *W, *Y, *Z, *XP, *X1P;
13: PetscReal val, norm1, nreal;
14: PetscRandom rdm, rdm2;
15: PetscLogDouble time, time1, time2;
16: PetscMPIInt size;
17: PetscViewer vwr;
18: Vec x;
19: PetscInt64 seedr, seedo;
20: PetscBool order = PETSC_FALSE;
22: PetscFunctionBeginUser;
23: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
24: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
25: PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!");
27: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
28: PetscCall(PetscOptionsGetInt(NULL, NULL, "-r", &r, NULL));
29: PetscCall(PetscOptionsGetInt(NULL, NULL, "-d", &d, NULL));
30: PetscCall(PetscOptionsGetInt(NULL, NULL, "-vsize", &vsize, NULL));
31: PetscCall(PetscOptionsGetBool(NULL, NULL, "-order", NULL, &order));
32: PetscCall(PetscOptionsCreateViewer(PETSC_COMM_WORLD, NULL, NULL, "-array_view", &vwr, NULL, NULL));
33: PetscCheck(n >= 1 && r >= 1 && d >= 1 && d <= n, PETSC_COMM_WORLD, PETSC_ERR_SUP, "Wrong input n=%" PetscInt_FMT ",r=%" PetscInt_FMT ",d=%" PetscInt_FMT ". They must be >=1 and n>=d", n, r, d);
35: PetscCall(PetscCalloc6(n, &X, n, &X1, n, &XR, n, &XSO, n, &Y, n, &Z));
36: PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &rdm));
37: PetscCall(PetscRandomSetFromOptions(rdm));
38: PetscCall(PetscRandomGetSeed(rdm, &seedr));
40: for (i = 0; i < n; ++i) {
41: PetscCall(PetscRandomGetValueReal(rdm, &val));
42: XR[i] = val * ((PetscReal)PETSC_INT_MAX);
43: if (d > 1) XR[i] = XR[i] % (n / d);
44: XSO[i] = i;
45: if (d > 1) XSO[i] = XSO[i] % (n / d);
46: }
48: nreal = (PetscReal)n;
49: PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &rdm2));
50: PetscCall(PetscRandomGetSeed(rdm, &seedo));
51: PetscCall(PetscRandomSetInterval(rdm2, 0, nreal));
52: for (i = 0; i < n / 10; ++i) {
53: PetscInt swapi, t;
54: PetscCall(PetscRandomGetValueReal(rdm2, &val));
55: swapi = (PetscInt)val;
56: t = XSO[swapi - 1];
57: XSO[swapi - 1] = XSO[swapi];
58: XSO[swapi] = t;
59: }
60: PetscCall(PetscRandomDestroy(&rdm2));
62: if (vwr) PetscCall(PetscIntView(n, order ? XSO : XR, vwr));
63: PetscCall(PetscViewerDestroy(&vwr));
64: PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
65: PetscCall(VecSetSizes(x, PETSC_DECIDE, vsize));
66: PetscCall(VecSetFromOptions(x));
67: PetscCall(VecSetRandom(x, rdm));
68: time = 0.0;
69: time1 = 0.0;
70: for (l = 0; l < r; l++) { /* r loops */
71: PetscCall(PetscArraycpy(X, order ? XSO : XR, n));
72: PetscCall(PetscArraycpy(X1, order ? XSO : XR, n));
74: PetscCall(VecNorm(x, NORM_1, &norm1));
75: PetscCall(PetscTimeSubtract(&time1));
76: PetscCall(PetscIntSortSemiOrdered(n, X1));
77: PetscCall(PetscTimeAdd(&time1));
79: PetscCall(VecNorm(x, NORM_1, &norm1));
80: PetscCall(PetscTimeSubtract(&time));
81: PetscCall(PetscSortInt(n, X));
82: PetscCall(PetscTimeAdd(&time));
84: for (i = 0; i < n - 1; i++) PetscCheck(X[i] <= X[i + 1], PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscSortInt() produced wrong results!");
85: for (i = 0; i < n; i++) {
86: PetscCheck(X[i] == X1[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscIntSortSemiOrdered() rep %" PetscInt_FMT " X1[%" PetscInt_FMT "]:%" PetscInt_FMT " does not match PetscSortInt() X[%" PetscInt_FMT "]:%" PetscInt_FMT "! randomSeed %" PetscInt64_FMT ", orderedSeed %" PetscInt64_FMT, l, i, X1[i], i, X[i], seedr, seedo);
87: }
88: for (i = 0; i < n - 1; i++) PetscCheck(X1[i] <= X1[i + 1], PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscIntSortSemiOrdered() produced wrong results! randomSeed %" PetscInt64_FMT "orderedSeed %" PetscInt64_FMT, seedr, seedo);
89: PetscCall(PetscArrayzero(X, n));
90: PetscCall(PetscArrayzero(X1, n));
91: }
92: PetscCall(PetscPrintf(PETSC_COMM_SELF, "PetscSortInt() with %" PetscInt_FMT " integers, %" PetscInt_FMT " duplicate(s) per unique value took %g seconds\n", n, d, time / r));
93: PetscCall(PetscPrintf(PETSC_COMM_SELF, "PetscIntSortSemiOrdered() with %" PetscInt_FMT " integers, %" PetscInt_FMT " duplicate(s) per unique value took %g seconds\n", n, d, time1 / r));
94: PetscCall(PetscPrintf(PETSC_COMM_SELF, "Speedup of PetscIntSortSemiOrdered() was %g (0:1 = slower, >1 means faster)\n", time / time1));
96: for (i = 0; i < n; i++) { /* Init X[] */
97: PetscCall(PetscRandomGetValueReal(rdm, &val));
98: X[i] = val * ((PetscReal)PETSC_INT_MAX);
99: if (d > 1) X[i] = X[i] % (n / d);
100: }
101: PetscCall(PetscCalloc3(n, &XP, n, &X1P, n, &W));
103: time = 0.0;
104: time1 = 0.0;
105: time2 = 0.0;
106: for (l = 0; l < r; l++) { /* r loops */
107: PetscCall(PetscArraycpy(X1, order ? XSO : XR, n));
108: PetscCall(PetscArraycpy(X1P, order ? XSO : XR, n));
109: PetscCall(PetscArraycpy(X, order ? XSO : XR, n));
110: PetscCall(PetscArraycpy(XP, order ? XSO : XR, n));
111: PetscCall(PetscArraycpy(W, order ? XSO : XR, n));
113: PetscCall(VecNorm(x, NORM_1, &norm1));
114: PetscCall(PetscTimeSubtract(&time1));
115: PetscCall(PetscIntSortSemiOrderedWithArray(n, X1, X1P));
116: PetscCall(PetscTimeAdd(&time1));
118: PetscCall(VecNorm(x, NORM_1, &norm1));
119: PetscCall(PetscTimeSubtract(&time2));
120: PetscCall(PetscSortIntWithArray(n, X, XP));
121: PetscCall(PetscTimeAdd(&time2));
123: PetscCall(PetscTimeSubtract(&time));
124: PetscCall(PetscSortIntWithArrayPair(n, W, Y, Z));
125: PetscCall(PetscTimeAdd(&time));
127: for (i = 0; i < n - 1; i++) {
128: if (Y[i] > Y[i + 1]) {
129: PetscCall(PetscIntView(n, Y, NULL));
130: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscSortIntWithArray() produced wrong results!");
131: }
132: }
133: for (i = 0; i < n - 1; i++) PetscCheck(W[i] <= W[i + 1], PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscSortIntWithArrayPair() produced wrong results!");
134: for (i = 0; i < n; i++) {
135: PetscCheck(X1P[i] == X[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscIntSortSemiOrdered() rep %" PetscInt_FMT " X1[%" PetscInt_FMT "]:%" PetscInt_FMT " does not match PetscSortIntWithArray() X[%" PetscInt_FMT "]:%" PetscInt_FMT "! randomSeed %" PetscInt64_FMT " orderedSeed %" PetscInt64_FMT, l, i, X1[i], i, X[i], seedr, seedo);
136: }
137: for (i = 0; i < n - 1; i++) PetscCheck(X1[i] <= X1[i + 1], PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscIntSortSemiOrdered() produced wrong results! randomSeed %" PetscInt64_FMT "orderedSeed %" PetscInt64_FMT, seedr, seedo);
138: PetscCall(PetscArrayzero(X1, n));
139: PetscCall(PetscArrayzero(X1P, n));
140: PetscCall(PetscArrayzero(X, n));
141: PetscCall(PetscArrayzero(XP, n));
142: PetscCall(PetscArrayzero(W, n));
143: }
144: PetscCall(PetscPrintf(PETSC_COMM_SELF, "PetscSortIntWithArrayPair() with %" PetscInt_FMT " integers, %" PetscInt_FMT " duplicate(s) per unique value took %g seconds\n", n, d, time / r));
145: PetscCall(PetscPrintf(PETSC_COMM_SELF, "PetscSortIntWithArray() with %" PetscInt_FMT " integers, %" PetscInt_FMT " duplicate(s) per unique value took %g seconds\n", n, d, time2 / r));
146: PetscCall(PetscPrintf(PETSC_COMM_SELF, "PetscIntSortSemiOrderedWithArray() with %" PetscInt_FMT " integers, %" PetscInt_FMT " duplicate(s) per unique value took %g seconds\n", n, d, time1 / r));
147: PetscCall(PetscPrintf(PETSC_COMM_SELF, "Speedup of PetscIntSortSemiOrderedWithArray() was %g (0:1 = slower, >1 means faster)\n", time2 / time1));
148: PetscCall(PetscPrintf(PETSC_COMM_SELF, "SUCCEEDED\n"));
150: PetscCall(VecDestroy(&x));
151: PetscCall(PetscRandomDestroy(&rdm));
152: PetscCall(PetscFree3(XP, X1P, W));
153: PetscCall(PetscFree6(X, X1, XR, XSO, Y, Z));
154: PetscCall(PetscFinalize());
155: return 0;
156: }
158: /*TEST
160: testset:
161: filter: grep -vE "per unique value took|Speedup of "
162: output_file: output/ex32.out
164: test:
165: suffix: small
166: args: -n 9 -r 1
168: test:
169: suffix: large
170: args: -n 1000 -r 10 -d 1
171: # Do not need to output timing results for test
173: TEST*/