xref: /petsc/src/mat/tests/ex29.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
1 static char help[] = "Tests PetscHeapCreate()\n\n";
2 
3 #include <petscsys.h>
4 #include <petscviewer.h>
5 
6 int main(int argc, char **args) {
7   PetscHeap h;
8   PetscInt  id, val, cnt, *values;
9 
10   PetscFunctionBeginUser;
11   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
12   PetscCall(PetscHeapCreate(9, &h));
13   PetscCall(PetscHeapAdd(h, 0, 100));
14   PetscCall(PetscHeapAdd(h, 1, 19));
15   PetscCall(PetscHeapAdd(h, 2, 36));
16   PetscCall(PetscHeapAdd(h, 3, 17));
17   PetscCall(PetscHeapAdd(h, 4, 3));
18   PetscCall(PetscHeapAdd(h, 5, 25));
19   PetscCall(PetscHeapAdd(h, 6, 1));
20   PetscCall(PetscHeapAdd(h, 8, 2));
21   PetscCall(PetscHeapAdd(h, 9, 7));
22   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Initial heap:\n"));
23   PetscCall(PetscHeapView(h, NULL));
24 
25   PetscCall(PetscHeapPop(h, &id, &val));
26   PetscCall(PetscHeapStash(h, id, val + 10));
27   PetscCall(PetscHeapPop(h, &id, &val));
28   PetscCall(PetscHeapStash(h, id, val + 10));
29   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Pop two items, increment, and place in stash:\n"));
30   PetscCall(PetscHeapView(h, NULL));
31 
32   PetscCall(PetscHeapUnstash(h));
33   PetscCall(PetscPrintf(PETSC_COMM_SELF, "After unpacking the stash:\n"));
34   PetscCall(PetscHeapView(h, NULL));
35 
36   PetscCall(PetscMalloc1(9, &values));
37   PetscCall(PetscHeapPop(h, &id, &val));
38   cnt = 0;
39   while (id >= 0) {
40     values[cnt++] = val;
41     PetscCall(PetscHeapPop(h, &id, &val));
42   }
43   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Sorted values:\n"));
44   PetscCall(PetscIntView(cnt, values, PETSC_VIEWER_STDOUT_SELF));
45   PetscCall(PetscFree(values));
46   PetscCall(PetscHeapDestroy(&h));
47   PetscCall(PetscFinalize());
48   return 0;
49 }
50 
51 /*TEST
52 
53    test:
54 
55 TEST*/
56