advanced c programming - max planck...
TRANSCRIPT
![Page 1: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/1.jpg)
Advanced C Programming
Sebastian [email protected]
Christoph [email protected]
Winter Term 2008/09
computer science
saarlanduniversity
1
![Page 2: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/2.jpg)
Why Advanced C?
“Our”
I we need experienced C programmers
“Religious”
I portability
I efficiency
I powerful and flexible
“Real”
I unix
I network software
I embedded systems
I research: graphics, vision, formal methods
I entertainment: games, films
2
![Page 3: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/3.jpg)
Content I
SAT Solving I
Basic C Setup
Efficient Algorithms I
SAT Solving II
Style, Signals, Timing and Tools
SAT Solving III
Memory Management and Tools
3
![Page 4: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/4.jpg)
Content II
Software Engineering in the Small
Know the Compiler and Processor
Efficient Algorithms II
Parallelism
Recent C Standards
4
![Page 5: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/5.jpg)
Propositional logic
I logic of truth values
I decidable (but NP-complete)
I can be used to describe functions over a finite domain
I important for hardware applications (e.g., model checking)
5
![Page 6: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/6.jpg)
Syntax
I propositional variables: P, Q, R ∈ Π
I logical symbols: ∧ and, ∨ or, ¬ not, > true, ⊥ false
I literals are propositional variables or their negation: P, ¬P
I clauses are (posssibly empty) disjunctions of literals: P ∨ ¬Q ∨ R
I clause sets are sets of clauses interpreted as the conjunction of allclauses
I literals, clauses and clause sets are formulas
6
![Page 7: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/7.jpg)
Semantics
Classical
In classical logic (dating back to Aristoteles) there are “only” two truthvalues “true” and “false” which we shall denote, respectively, by 1 and 0.
7
![Page 8: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/8.jpg)
Valuations
A propositional variable has no intrinsic meaning. The meaning of apropositional variable has to be defined by a valuation.
A Π-valuation is a mapA : Π → {0, 1}.
where {0, 1} is the set of truth values.
8
![Page 9: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/9.jpg)
Truth Value of a Literal, Clause, Clause Set
Given a Π-valuation A, it can be extended to formulasA : formulas → {0, 1} inductively as follows:
A(⊥) = 0
A(>) = 1
A(P) = A(P)
A(¬P) = 1−A(P)
A(A ∨ B) = max(A(A),A(B))
A(C ∧ D) = min(A(C ),A(D))
9
![Page 10: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/10.jpg)
Models, Validity, and Satisfiability
Validity
F is valid in A (A is a model of F ; F holds under A):
A |= F :⇔ A(F ) = 1
F is valid (or is a tautology):
|= F :⇔ A |= F for all Π-valuations A
(Un)Satisfiability
F is called satisfiable if there exists an A such that A |= F . Otherwise Fis called unsatisfiable (or contradictory).
Hence, F is valid iff ¬F is unsatisfiable.We say that N |= F iff N ∧ ¬F is unsatisfiable.
10
![Page 11: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/11.jpg)
Checking Unsatisfiability
Every formula F contains only finitely many propositional variables.Obviously, A(F ) depends only on the values of those finitely manyvariables in F under A.
If F contains n distinct propositional variables, then it is sufficient tocheck 2n valuations to see whether F is satisfiable or not ⇒ truth table.
So the satisfiability problem is clearly decidable (but, by Cook’s Theorem,NP-complete). Nevertheless, in practice, there are (much) bettermethods than truth tables to check the satisfiability of a formula.
11
![Page 12: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/12.jpg)
The DPLL Procedure
Goal
Given a propositional formula in CNF (or alternatively, a finite set N ofclauses), check whether it is satisfiable (and optionally: output onesolution, if it is satisfiable).
Assumption
Clauses contain neither duplicated literals nor complementary literals.
Notation
L is the complementary literal of L, i.e., P = ¬P and ¬P = P.
12
![Page 13: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/13.jpg)
Partial Valuations
Since we will construct satisfying valuations incrementally, we considerpartial valuations (that is, partial mappings A : Π → {0, 1}).
Every partial valuation A corresponds to a set M of literals that does notcontain complementary literals, and vice versa:
I A(L) is true, if L ∈ M.
I A(L) is false, if L ∈ M.
I A(L) is undefined, if neither L ∈ M nor L ∈ M.
A clause is true under a partial valuation A (or under a set M of literals)if one of its literals is true; it is false if all its literals are false; otherwise itis undefined.
13
![Page 14: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/14.jpg)
Unit Clauses
Observation
Let A be a partial valuation. If the set N contains a clause C , such thatall literals but one in C are false under A, then the following propertiesare equivalent:
I there is a valuation that is a model of N and extends A.
I there is a valuation that is a model of N and extends A and makesthe remaining literal L of C true.
C is called a unit clause; L is called a unit literal.
14
![Page 15: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/15.jpg)
The Davis-Putnam-Logemann-Loveland Procedure
booleanDPLL(literal set M, clause set N) {if (all clauses in N are true under M) return true;elsif (some clause in N is false under M) return false;elsif (N contains unit clause P) return DPLL(M ∪ {P}, N);elsif (N contains unit clause ¬P) return DPLL(M ∪ {¬P}, N);else {
let P be some undefined variable in N;if (DPLL(M ∪ {¬P}, N)) return true;else return DPLL(M ∪ {P}, N);
}}
Initially, DPLL is called with an empty literal set and the clause set N.
15
![Page 16: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/16.jpg)
DPLL Iteratively
In practice, there are several changes to the procedure:
I The branching variable is not chosen randomly.
I The algorithm is implemented iteratively;the backtrack stack is managed explicitly(it may be possible and useful to backtrack more than one level).
I Information is reused by learning.
16
![Page 17: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/17.jpg)
Formalizing DPLL with Refinements
The DPLL procedure is modelled by a transition relation ⇒DPLL on a setof states.
States
I fail
I M ‖ N,
where M is a list of annotated literals and N is a set of clauses.
Annotated literal
I L: deduced literal, due to unit propagation.
I Ld: decision literal (guessed literal).
17
![Page 18: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/18.jpg)
DPLL Rules
Unit Propagate
M ‖ N ∪ {C ∨ L} ⇒DPLL M L ‖ N ∪ {C ∨ L}if C is false under M and L is undefined under M.
Decide
M ‖ N ⇒DPLL M Ld ‖ N
if L is undefined under M.
Fail
M ‖ N ∪ {C} ⇒DPLL fail
if C is false under M and M contains no decision literals.
18
![Page 19: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/19.jpg)
DPLL Rules
Backjump
M ′ Ld M ′′ ‖ N ⇒DPLL M ′ L′ ‖ N
if there is some “backjump clause” C ∨ L′ such thatN |= C ∨ L′,C is false under M ′, andL′ is undefined under M ′.
19
![Page 20: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/20.jpg)
Backtracking
The Backjump rule is always applicable, if the list of literals M containsat least one decision literal and some clause in N is false under M.
There are many possible backjump clauses. One candidate: L1 ∨ . . . ∨ Ln,where the Li are all the decision literals in M Ld M ′. (But usually thereare better choices.)
20
![Page 21: Advanced C Programming - Max Planck Societyresources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/... · Advanced C Programming Sebastian Hack hack@cs.uni-sb.de Christoph](https://reader031.vdocuments.mx/reader031/viewer/2022021816/5a788eba7f8b9aa2448db360/html5/thumbnails/21.jpg)
DIMACS SAT File Input Format
Syntax
{c <comment>}∗p cnf <number of variables> <number of clauses>
{<clause> 0}∗
A <clause> is a sequence of integers from + <number of variables> to− <number of variables>, except 0, separated by blanks.
Example
The clauses P ∨ ¬Q ∨ R,Q ∨ ¬R can be coded by the file
c first, simple example
p cnf 3 2
1 -2 3 0
2 -3 0
21