Lecture 9 - UCSD CSE

Lecture
9
More
on
synchroniza2on
Applica2on
Study:
Protein
sequence
querying
Announcements
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
2
More
on
Synchroniza2on
Synchroniza2on
in
Java
•  What’s
the
simplest
synchroniza2on
mechanism
in
Java?
•  Semaphores
come
in
handy
for
a
producer
consumer
applica2on
•  Revisit
CAS
(Compare
and
Swap)
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
4
Semaphores
•  Locks
handle
mutual
exclusion
•  But
they
aren’t
an
appropriate
solu2on
for
long
cri2cal
sec2ons
•  Semaphores
▸  Coun2ng
▸  Binary
(mutex)
•  Reference:
Silberschatz
et
al.,
Opera&ng
Systems
Concepts,
7th
Ed,
Wiley,
2005,
Chapter
6
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
5
What
is
a
semaphore?
•  An
abstract
data
type
that
provides
mutual
exclusion
to
cri2cal
sec2ons
[Dijkstra,1968]
•  Semaphore S:
a
special
integer
variable
that
supports
two
opera2ons
▸  acquire():
wait
un2l
S>0, then
decrement
[Dijkstra’s
P()
or
down()]
▸  release():
increment,
allow
another
thread
to
enter
[Dijkstra’s
V(),
or
up()]
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
6
Two
types
of
semaphores
•  Binary
semaphore
(or
mutex
semaphore)
•  Coun2ng
semaphore
▸  Ini2alized
with
general
integer
values
▸  Keeps
track
of
mul2ple
resources,
also
manages
certain
kinds
of
unsynchronized
concurrent
access
(e.g.,
reading)
•  Semaphores
have
no
no2on
of
ownernship
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
7
Producer
Consumer
Example
•  Located
in
$pub/examples/MT/PC
•  Used
in
A1
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
8
Mul2threaded
implementa2on
import
java.u2l.concurrent.Semaphore;
public
class
Producer
extends
Thread{
public
void
run()
{
int
N
=
PC.N;
for
(int
r
=
0;
r<N;
r++){
int
offs
=
r*N
+
1;
for
(int
i
=
0;
i
<
N;
++
i)
PC.x[i]
=
i
+
offs;
PC.produced.release();
try
{
PC.produced.acquire();
}
catch
(InterruptedExcep2on
e)
{}
}
}
}
public
class
PC{
sta2c
int
N;
sta2c
int[]
x;
sta2c
Semaphore
produced;
public
sta2c
void
main
(String[]
args)
throws
Excep2on
{
produced
=
new
Semaphore(1,true);
produced.acquire();
Producer
producer
=
new
Producer();
producer.start();
int
numMatched
=
Consumer();
}
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
9
Incorrect
results
import
java.u2l.concurrent.Semaphore;
public
class
Producer
extends
Thread{
public
void
run()
{
int
N
=
PC.N;
for
(int
r
=
0;
r<N;
r++){
int
offs
=
r*N
+
1;
for
(int
i
=
0;
i
<
N;
++
i)
PC.x[i]
=
i
+
offs;
PC.produced.release();
try
{
PC.produced.acquire();
}
catch
(InterruptedExcep2on
e)
{}
}
}
java
PC
2
}
in
Consumer
Producer
about
to
release
@
0
Producer
released
@
0
Producer
about
to
release
@
1
Consumer
is
wai2ng
@
0
Producer
released
@
1
Consumer
acquired
@
0
Sums
didn't
match
at
round
0;
was
7,
should
be
3
Consumer
is
wai2ng
@
1
private
sta2c
int
Consumer(){
int
numMatched
=
0;
for
(int
r
=
0;
r<N;
r++){
try
{
produced.acquire();
}
catch
(InterruptedExcep2on
e)
{}
for
(int
i
=
0,
sum=0;
i
<
N;
++
i)
sum
+=
x[i];
produced.release();
int
sumE
=
(N*(N+1)/2)
+
r*N*N;
if
(sumE
==
sum)
numMatched++;
else
SUM
DIDN'T
MATCH
}
return(numMatched);
}
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
10
Drawbacks
•  Doesn’t
provide…
▸  A
connec2on
between
the
semaphore
and
the
data
it
controls
▸  control
or
guarantee
of
proper
usage
•  Alterna2ves
▸  Monitors
▸  PL
Support
•  java.u2l.concurrent
provides
higher
level
abstrac2ons,
like
BlockingQueue
•  How
would
a
queue
affect
our
producer
consumer
example?
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
11
CAS
Compare
and
set
•  Atomically
set
value
to
updated
value
if
current
value
=
expected
value
•  Returns
false
if
actual
value
≠
expected
value,
else
true
•  Let
value
=
0
▸  value.compareAndSet(0,100):
value

100
▸  value.compareAndSet(10,100):
no
change
boolean
compareAndSet(int
expect,
int
update)
©
2010
ScoA
B.
Baden
/
CSE
160
/
Winter
2010
13
Building
atomic
counters
public
class
AtomicCounter{
private
value;
public
int
getValue()
{
return
value;
}
public
int
increment()
{
int
oldVal
=
value.getValue();
while
(!value.CASw(oldVal,
oldVal
+
1)
)
oldVal
=
value.getValue();
return
oldVal
+
1;
}
value

updated
if
current
=
expected
}
©
2010
ScoA
B.
Baden
/
CSE
160
/
Winter
2010
14
Sequence
Querying
Sequence
alignment
Ini8al
sequencing
and
compara8ve
analysis
of
the
mouse
genome
Nature
420,
520‐562
(5
December
2002)
Mouse
Genome
Sequencing
Consor8um
The
sequence
of
the
mouse
genome
is
a
key
informa2onal
tool
for
understanding
the
contents
of
the
human
genome
and
a
key
experimental
tool
for
biomedical
research.
Here,
we
report
the
results
of
an
interna2onal
collabora2on
to
produce
a
high‐quality
drav
sequence
of
the
mouse
genome.
We
also
present
an
ini2al
compara2ve
analysis
of
the
mouse
and
human
genomes,
describing
some
of
the
insights
that
can
be
gleaned
from
the
two
sequences....
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
16
Why
is
this
interes2ng?
• 
Compara2ve
genomics
allows
one
to
read
evolu2on's
laboratory
notebook.
In
the
roughly
75
million
years
since
the
divergence
of
the
human
and
mouse
lineages,
the
process
of
evolu2on
has
altered
their
genome
sequences
and
caused
them
to
diverge
…
• 
At
the
nucleo2de
level,
approximately
40%
of
the
human
genome
can
be
aligned
to
the
mouse
genome.
These
sequences
seem
to
represent
most
of
the
orthologous
sequences
that
remain
in
both
lineages
from
the
common
ancestor,
with
the
rest
likely
to
have
been
deleted
in
one
or
both
genomes.
• 
Roughly
3
billion
nucleo2des
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
17
Background
•  We’ll
look
at
proteins,
sequences
of
(20)
amino
acids
•  Alphabet
of
amino
acids:
single
leAer
symbols
•  Goal:
form
an
alignment;
compare
two
sequences
to
gauge
their
similarity
•  Score:
α
=
+2
for
match
β=‐1
for
mismatch
Query X A R K M I
R K C W D
Query
Subject F F A R K Q M I K B W LSubject
X
X A R K M I
R K C W D
F F A R K Q M I K B W L X
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
18
Local
alignments
and
gaps
•  We
may
be
able
to
increase
the
score
by
crea2ng
gaps:
γ
=
‐1
•  Score
=
+9
Query
X A R K
M I
R K C W DQuery
Subject F F A R K Q M I K B W L Subject
X
X A R K
M I
F F A R K Q M I
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
R K C W D
K B W L X
19
Best
alignment
•  What
is
the
score?
•  Another
example:
www.med.nyu.edu/rcr/rcr/course/sim‐sw.html
Query
X A R K
M I
R K C W D
Subject F F A R K Q M I
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
K B W L X
20
Smith
Waterman
algorithm
•  Computes
the
best
possible
alignment
•  For
symbols
ai
and
bi
,
S[i,j]
=
best
possible
alignment
score
when
ai
is
aligned
with
bi
•  Suppose
we
have
computed
the
alignment
up
to
an
including
ai
and
bi
•  We
have
S[0,0]
through
S[i‐1,j‐1],
S[I,j‐1],
S[i‐1,j]
•  We
compute
S[i,j]
using
a
simple
formula
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
21
The
next
alignment
score
•  ai
aligns
with
bi
S[i,j]
=
S[i‐1,j‐1
+
α
if
match,
else
β=‐
•  Introduce
a
gap
in
B
S = Max(S + f (a ,b ),S − γ,S
S[i,j]
=
S[i‐1,j]
+
γ
a = b → f (a ,b ) = α
•  Introduce
a
gap
in
A
a ≠ b → f (a ,b ) = β
€
S[i,j]
=
S[i,j‐1]
+
γ
•  Neither
a
nor
b
in
the
alignment:
S[i,j]
=
0
€
•  We
take
the
maximum
value
i, j
i, j
i
i
i
i
i
i
i
i
i
i
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
i−1, j
i, j−1
− γ)
22
Dynamic
programming
solu2on
and
the
Traceback
Subject Sequence
Query Sequence
F
F
A
R
K
Q
M
I
K
B
W
L
X
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
0
X
0
0
A
R
K
M
I
R
K
C
W
D
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
1
0
0
0
0
0
0
0
0
1
4
3
2
1
2
1
0
0
0
0
3
6
5
4
3
4
3
2
1
0
2
5
5
4
3
3
3
2
1
0
1
4
7
6
5
4
3
2
1
0
0
3
6
9
8
7
6
5
4
0
0
2
5
8
8
10
9
8
7
0
0
1
4
7
7
9
9
8
7
0
0
0
3
6
6
8
8
11
10
0
0
0
2
5
5
7
7
10
10
1
0
0
1
4
4
6
6
9
9
2
0
1
2
3
4
5
6
7
8
9
10
11
12
13
Query
X A R K
M I
1
3
4
5
6
7
8
9
10
11
R K C W D
Subject F F A R K Q M I
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
K B W L X
23
Dependence
Structure
1,1
1,2
1,3
1,4
1,5
1,6
1,7
1,8
1,9
1,10
1,11
1,12
2,1
2,2
2,3
2,4
2,5
2,6
2,7
2,8
2,9
2,10
2,11
2,12
3,1
3,2
3,3
3,4
3,5
3,6
3,7
3,8
3,9
3,10
3,11
3,12
4,1
4,2
4,3
4,4
4,5
4,6
4,7
4,8
4,9
4,10
4,11
4,12
5,1
5,2
5,3
5,4
5,5
5,6
5,7
5,8
5,9
5,10
5,11
5,12
6,1
6,2
6,3
6,4
6,5
6,6
6,7
6,8
6,9
6,10
6,11
6,12
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
24
Protein
Sequence
Example
•  hAp://www.uniprot.org/downloads
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
25
Parallel
Control
Flow
•  2
choices
▸  Distribute
the
query
sequence
across
threads,
one
subject
sequence
at
a
2me
▸  Compute
separate
queries
in
different
threads,
each
thread
has
a
different
subject
sequence
•  What
are
the
issues?
Protein sequence
database
Subject
sequence
Thr
0
Thr
1
Thr
2
Alignment
Query
sequence
Query
sequence
Thr
3
Subject
Subject
sequence
sequence
Thr
0
Process 0
Thr
Thr
Thr
1
2
0
Alignment
Alignment
Query
Subject sequence
Subject
sequence
sequence
Thr
Thr
3
1
Alignment Process
Alignment
1
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
List of
matching proteins
Thr
2
Protein sequence
database
Subject
sequence
Query
sequence
Subject
sequence
Thr
3
Alignment
Thr
0
Alignment
Process 0
26
List of
matching proteins
Subj
seque
Th
1
Alignm
Dependence
Structure
1,1
1,2
1,3
1,4
1,5
1,6
1,7
1,8
1,9
1,10
1,11
1,12
2,1
2,2
2,3
2,4
2,5
2,6
2,7
2,8
2,9
2,10
2,11
2,12
3,1
3,2
3,3
3,4
3,5
3,6
3,7
3,8
3,9
3,10
3,11
3,12
4,1
4,2
4,3
4,4
4,5
4,6
4,7
4,8
4,9
4,10
4,11
4,12
5,1
5,2
5,3
5,4
5,5
5,6
5,7
5,8
5,9
5,10
5,11
5,12
6,1
6,2
6,3
6,4
6,5
6,6
6,7
6,8
6,9
6,10
6,11
6,12
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
29
Inter‐block
Dependencies
Thread 0
Thread 1
Thread 2
Thread 3
1,1 1,2 1,3
1,4 1,5 1,6
1,7 1,8 1,9
1,10 1,11 1,12
2,1 2,2 2,3
2,4 2,5 2,6
2,7 2,8 2,9
2,10 2,11 2,12
3,1 3,2 3,3
3,4 3,5 3,6
3,7 3,8 3,9
3,10 3,11 3,12
4,1 4,2 4,3
4,4 4,5 4,6
4,7 4,8 4,9
4,10 4,11 4,12
5,1 5,2 5,3
5,4 5,5 5,6
5,7 5,8 5,9
5,10 5,11 5,12
6,1 6,2 6,3
6,4 6,5 6,6
6,7 6,8 6,9
6,10 6,11 6,12
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
30
Computa2onal
Rounds
Thread 0
Thread 1
Thread 2
Thread 3
Round 1 1,1 1,2 1,3
Round 2 2,1 2,2 2,3
1,4 1,5 1,6
Round 3 3,1 3,2 3,3
2,4 2,5 2,6
1,7 1,8 1,9
Round 4 4,1 4,2 4,3
3,4 3,5 3,6
2,7 2,8 2,9
1,10 1,11 1,12
Round 5 5,1 5,2 5,3
4,4 4,5 4,6
3,7 3,8 3,9
2,10 2,11 2,12
Round 6 6,1 6,2 6,3
5,4 5,5 5,6
4,7 4,8 4,9
3,10 3,11 3,12
Round 7
6,4 6,5 6,6
5,7 5,8 5,9
4,10 4,11 4,12
6,7 6,8 6,9
5,10 5,11 5,12
Round 8
Round 9
6,10 6,11 6,12
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
31
How
do
we
implement
this?
• 
• 
• 
• 
Synchroniza2on?
Locality?
False
sharing?
Load
imbalance?
©2010
ScoA
B.
Baden
/CSE
160/
Winter
2010
32