ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
heap.c
Go to the documentation of this file.
1#include "allocate.h"
2#include "inlineheap.h"
3#include "internal.h"
4#include "logging.h"
5
6#include <string.h>
7
9
16
17#ifndef KISSAT_NDEBUG
18
20 const unsigned *const stack = BEGIN_STACK (heap->stack);
21 const unsigned end = SIZE_STACK (heap->stack);
22 const unsigned *const pos = heap->pos;
23 const double *const score = heap->score;
24 for (unsigned i = 0; i < end; i++) {
25 const unsigned idx = stack[i];
26 const unsigned idx_pos = pos[idx];
27 KISSAT_assert (idx_pos == i);
28 unsigned child_pos = HEAP_CHILD (idx_pos);
29 unsigned parent_pos = HEAP_PARENT (child_pos);
30 KISSAT_assert (parent_pos == idx_pos);
31 if (child_pos < end) {
32 unsigned child = stack[child_pos];
33 KISSAT_assert (score[idx] >= score[child]);
34 if (++child_pos < end) {
35 parent_pos = HEAP_PARENT (child_pos);
36 KISSAT_assert (parent_pos == idx_pos);
37 child = stack[child_pos];
38 KISSAT_assert (score[idx] >= score[child]);
39 }
40 }
41 }
42}
43
44#endif
45
46void kissat_resize_heap (kissat *solver, heap *heap, unsigned new_size) {
47 const unsigned old_size = heap->size;
48 if (old_size >= new_size)
49 return;
50 LOG ("resizing %s heap from %u to %u",
51 (heap->tainted ? "tainted" : "untainted"), old_size, new_size);
52
53 heap->pos = (unsigned*)kissat_nrealloc (solver, heap->pos, old_size, new_size,
54 sizeof (unsigned));
55 if (heap->tainted) {
56 heap->score = (double*)kissat_nrealloc (solver, heap->score, old_size, new_size,
57 sizeof (double));
58 } else {
59 if (old_size)
60 DEALLOC (heap->score, old_size);
61 heap->score = (double*)kissat_calloc (solver, new_size, sizeof (double));
62 }
63 heap->size = new_size;
64#ifdef CHECK_HEAP
66#endif
67}
68
69void kissat_rescale_heap (kissat *solver, heap *heap, double factor) {
70 LOG ("rescaling scores on heap with factor %g", factor);
71 double *score = heap->score;
72 for (unsigned i = 0; i < heap->vars; i++)
73 score[i] *= factor;
74#ifndef KISSAT_NDEBUG
76#endif
77#ifndef LOGGING
78 (void) solver;
79#endif
80}
81
82void kissat_enlarge_heap (kissat *solver, heap *heap, unsigned new_vars) {
83 const unsigned old_vars = heap->vars;
84 KISSAT_assert (old_vars < new_vars);
85 KISSAT_assert (new_vars <= heap->size);
86 const size_t delta = new_vars - heap->vars;
87 memset (heap->pos + old_vars, 0xff, delta * sizeof (unsigned));
88 heap->vars = new_vars;
89 if (heap->tainted)
90 memset (heap->score + old_vars, 0, delta * sizeof (double));
91 LOG ("enlarged heap from %u to %u", old_vars, new_vars);
92#ifndef LOGGING
93 (void) solver;
94#endif
95}
96
97#ifndef KISSAT_NDEBUG
98
99static void dump_heap (heap *heap) {
100 for (unsigned i = 0; i < SIZE_STACK (heap->stack); i++)
101 printf ("heap.stack[%u] = %u\n", i, PEEK_STACK (heap->stack, i));
102 for (unsigned i = 0; i < heap->vars; i++)
103 printf ("heap.pos[%u] = %u\n", i, heap->pos[i]);
104 for (unsigned i = 0; i < heap->vars; i++)
105 printf ("heap.score[%u] = %g\n", i, heap->score[i]);
106}
107
108void kissat_dump_heap (heap *heap) { dump_heap (heap); }
109
110#endif
111
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
void * kissat_calloc(kissat *solver, size_t n, size_t size)
Definition allocate.c:97
void * kissat_nrealloc(kissat *solver, void *p, size_t o, size_t n, size_t size)
Definition allocate.c:151
#define BEGIN_STACK(S)
Definition stack.h:46
#define SIZE_STACK(S)
Definition stack.h:19
#define RELEASE_STACK(S)
Definition stack.h:71
#define PEEK_STACK(S, N)
Definition stack.h:29
#define LOG(...)
#define DEALLOC(P, N)
bool pos
Definition globals.c:30
void kissat_enlarge_heap(kissat *solver, heap *heap, unsigned new_vars)
Definition heap.c:82
void kissat_rescale_heap(kissat *solver, heap *heap, double factor)
Definition heap.c:69
ABC_NAMESPACE_IMPL_START void kissat_release_heap(kissat *solver, heap *heap)
Definition heap.c:10
void kissat_resize_heap(kissat *solver, heap *heap, unsigned new_size)
Definition heap.c:46
#define HEAP_PARENT(POS)
Definition inlineheap.h:13
#define HEAP_CHILD(POS)
Definition inlineheap.h:11
#define KISSAT_assert(ignore)
Definition global.h:13
#define kissat_check_heap(...)
Definition heap.h:83
#define solver
Definition kitten.c:211
Definition heap.h:19
unsigneds stack
Definition heap.h:23
unsigned * pos
Definition heap.h:25
double * score
Definition heap.h:24
unsigned vars
Definition heap.h:21
unsigned size
Definition heap.h:22
bool tainted
Definition heap.h:20
char * memset()