<s>
A	O
thread	B-Operating_System
block	I-Operating_System
is	O
a	O
programming	O
abstraction	O
that	O
represents	O
a	O
group	O
of	O
threads	B-Operating_System
that	O
can	O
be	O
executed	O
serially	O
or	O
in	O
parallel	O
.	O
</s>
<s>
For	O
better	O
process	B-Operating_System
and	O
data	O
mapping	O
,	O
threads	B-Operating_System
are	O
grouped	O
into	O
thread	B-Operating_System
blocks	I-Operating_System
.	O
</s>
<s>
The	O
number	O
of	O
threads	B-Operating_System
in	O
a	O
thread	B-Operating_System
block	I-Operating_System
was	O
formerly	O
limited	O
by	O
the	O
architecture	O
to	O
a	O
total	O
of	O
512	O
threads	B-Operating_System
per	O
block	O
,	O
but	O
as	O
of	O
March	O
2010	O
,	O
with	O
compute	O
capability	O
2.x	O
and	O
higher	O
,	O
blocks	O
may	O
contain	O
up	O
to	O
1024	O
threads	B-Operating_System
.	O
</s>
<s>
The	O
threads	B-Operating_System
in	O
the	O
same	O
thread	B-Operating_System
block	I-Operating_System
run	O
on	O
the	O
same	O
stream	O
processor	O
.	O
</s>
<s>
Threads	B-Operating_System
in	O
the	O
same	O
block	O
can	O
communicate	O
with	O
each	O
other	O
via	O
shared	B-Operating_System
memory	I-Operating_System
,	O
barrier	O
synchronization	O
or	O
other	O
synchronization	O
primitives	O
such	O
as	O
atomic	O
operations	O
.	O
</s>
<s>
All	O
the	O
blocks	O
in	O
the	O
same	O
grid	O
contain	O
the	O
same	O
number	O
of	O
threads	B-Operating_System
.	O
</s>
<s>
The	O
number	O
of	O
threads	B-Operating_System
in	O
a	O
block	O
is	O
limited	O
,	O
but	O
grids	O
can	O
be	O
used	O
for	O
computations	O
that	O
require	O
a	O
large	O
number	O
of	O
thread	B-Operating_System
blocks	I-Operating_System
to	O
operate	O
in	O
parallel	O
and	O
to	O
use	O
all	O
available	O
multiprocessors	O
.	O
</s>
<s>
CUDA	B-Architecture
is	O
a	O
parallel	B-Operating_System
computing	I-Operating_System
platform	O
and	O
programming	O
model	O
that	O
higher	O
level	O
languages	O
can	O
use	O
to	O
exploit	O
parallelism	B-Operating_System
.	O
</s>
<s>
In	O
CUDA	B-Architecture
,	O
the	O
kernel	B-Operating_System
is	O
executed	O
with	O
the	O
aid	O
of	O
threads	B-Operating_System
.	O
</s>
<s>
The	O
thread	B-Operating_System
is	O
an	O
abstract	O
entity	O
that	O
represents	O
the	O
execution	O
of	O
the	O
kernel	B-Operating_System
.	O
</s>
<s>
A	O
kernel	B-Operating_System
is	O
a	O
function	O
that	O
compiles	O
to	O
run	O
on	O
a	O
special	O
device	O
.	O
</s>
<s>
Multi	O
threaded	O
applications	O
use	O
many	O
such	O
threads	B-Operating_System
that	O
are	O
running	O
at	O
the	O
same	O
time	O
,	O
to	O
organize	O
parallel	B-Operating_System
computation	I-Operating_System
.	O
</s>
<s>
Every	O
thread	B-Operating_System
has	O
an	O
index	O
,	O
which	O
is	O
used	O
for	O
calculating	O
memory	O
address	O
locations	O
and	O
also	O
for	O
taking	O
control	O
decisions	O
.	O
</s>
<s>
CUDA	B-Architecture
operates	O
on	O
a	O
heterogeneous	O
programming	O
model	O
which	O
is	O
used	O
to	O
run	O
host	O
device	O
application	O
programs	O
.	O
</s>
<s>
It	O
has	O
an	O
execution	O
model	O
that	O
is	O
similar	O
to	O
OpenCL	B-Application
.	O
</s>
<s>
In	O
this	O
model	O
,	O
we	O
start	O
executing	O
an	O
application	O
on	O
the	O
host	O
device	O
which	O
is	O
usually	O
a	O
CPU	B-General_Concept
core	O
.	O
</s>
<s>
The	O
device	O
is	O
a	O
throughput	O
oriented	O
device	O
,	O
i.e.	O
,	O
a	O
GPU	B-Architecture
core	O
which	O
performs	O
parallel	B-Operating_System
computations	I-Operating_System
.	O
</s>
<s>
Kernel	B-Operating_System
functions	O
are	O
used	O
to	O
do	O
these	O
parallel	O
executions	O
.	O
</s>
<s>
Once	O
these	O
kernel	B-Operating_System
functions	O
are	O
executed	O
the	O
control	O
is	O
passed	O
back	O
to	O
the	O
host	O
device	O
that	O
resumes	O
serial	O
execution	O
.	O
</s>
<s>
As	O
many	O
parallel	O
applications	O
involve	O
multidimensional	O
data	O
,	O
it	O
is	O
convenient	O
to	O
organize	O
thread	B-Operating_System
blocks	I-Operating_System
into	O
1D	O
,	O
2D	O
or	O
3D	O
arrays	O
of	O
threads	B-Operating_System
.	O
</s>
<s>
'	O
When	O
a	O
kernel	B-Operating_System
is	O
launched	O
the	O
number	O
of	O
threads	B-Operating_System
per	O
thread	B-Operating_System
block	I-Operating_System
,	O
and	O
the	O
number	O
of	O
thread	B-Operating_System
blocks	I-Operating_System
is	O
specified	O
,	O
this	O
,	O
in	O
turn	O
,	O
defines	O
the	O
total	O
number	O
of	O
CUDA	B-Architecture
threads	B-Operating_System
launched.	O
'	O
</s>
<s>
The	O
maximum	O
x	O
,	O
y	O
and	O
z	O
dimensions	O
of	O
a	O
block	O
are	O
1024	O
,	O
1024	O
and	O
64	O
,	O
and	O
it	O
should	O
be	O
allocated	O
such	O
that	O
x	O
×	O
y	O
×	O
z	O
≤	O
1024	O
,	O
which	O
is	O
the	O
maximum	O
number	O
of	O
threads	B-Operating_System
per	O
block	O
.	O
</s>
<s>
Unlike	O
the	O
maximum	O
threads	B-Operating_System
per	O
block	O
,	O
there	O
is	O
not	O
a	O
blocks	O
per	O
grid	O
limit	O
distinct	O
from	O
the	O
maximum	O
grid	O
dimensions	O
.	O
</s>
<s>
Every	O
thread	B-Operating_System
in	O
CUDA	B-Architecture
is	O
associated	O
with	O
a	O
particular	O
index	O
so	O
that	O
it	O
can	O
calculate	O
and	O
access	O
memory	O
locations	O
in	O
an	O
array	O
.	O
</s>
<s>
One	O
of	O
the	O
organization	O
structure	O
is	O
taking	O
a	O
grid	O
with	O
a	O
single	O
block	O
that	O
has	O
a	O
512	O
threads	B-Operating_System
.	O
</s>
<s>
Every	O
thread	B-Operating_System
has	O
an	O
index	O
i	O
and	O
it	O
performs	O
the	O
multiplication	O
of	O
ith	O
element	O
of	O
A	O
and	O
B	O
and	O
then	O
store	O
the	O
result	O
in	O
the	O
ith	O
element	O
of	O
C	O
.	O
i	O
is	O
calculated	O
by	O
using	O
blockIdx	O
(	O
which	O
is	O
0	O
in	O
this	O
case	O
as	O
there	O
is	O
only	O
one	O
block	O
)	O
,	O
blockDim	O
(	O
512	O
in	O
this	O
case	O
as	O
the	O
block	O
has	O
512	O
elements	O
)	O
and	O
threadIdx	O
that	O
varies	O
from	O
0	O
to	O
511	O
for	O
each	O
block	O
.	O
</s>
<s>
The	O
thread	B-Operating_System
index	O
i	O
is	O
calculated	O
by	O
the	O
following	O
formula	O
:	O
</s>
<s>
If	O
we	O
want	O
to	O
consider	O
computations	O
for	O
an	O
array	O
that	O
is	O
larger	O
than	O
1024	O
we	O
can	O
have	O
multiple	O
blocks	O
with	O
1024	O
threads	B-Operating_System
each	O
.	O
</s>
<s>
In	O
this	O
case	O
we	O
have	O
2	O
thread	B-Operating_System
blocks	I-Operating_System
with	O
1024	O
threads	B-Operating_System
each	O
.	O
</s>
<s>
Thus	O
the	O
thread	B-Operating_System
identifiers	O
 '	O
values	O
will	O
vary	O
from	O
0	O
to	O
1023	O
,	O
the	O
block	O
identifier	O
will	O
vary	O
from	O
0	O
to	O
1	O
and	O
the	O
block	O
dimension	O
will	O
be	O
1024	O
.	O
</s>
<s>
Thus	O
each	O
thread	B-Operating_System
will	O
first	O
calculate	O
the	O
index	O
of	O
memory	O
that	O
it	O
has	O
to	O
access	O
and	O
then	O
proceed	O
with	O
the	O
calculation	O
.	O
</s>
<s>
Consider	O
an	O
example	O
in	O
which	O
elements	O
from	O
arrays	O
A	O
and	O
B	O
are	O
added	O
in	O
parallel	O
by	O
using	O
threads	B-Operating_System
and	O
the	O
results	O
is	O
stored	O
in	O
an	O
array	O
C	O
.	O
The	O
corresponding	O
code	O
in	O
a	O
thread	B-Operating_System
is	O
shown	O
below	O
:	O
</s>
<s>
In	O
the	O
same	O
way	O
in	O
particularly	O
complex	O
grids	O
,	O
the	O
blockId	O
as	O
well	O
as	O
the	O
threadId	O
need	O
to	O
be	O
calculated	O
by	O
each	O
thread	B-Operating_System
depending	O
on	O
geometry	O
of	O
the	O
grid	O
.	O
</s>
<s>
Although	O
we	O
have	O
stated	O
the	O
hierarchy	O
of	O
threads	B-Operating_System
,	O
we	O
should	O
note	O
that	O
,	O
threads	B-Operating_System
,	O
thread	B-Operating_System
blocks	I-Operating_System
and	O
grid	O
are	O
essentially	O
a	O
programmer	O
's	O
perspective	O
.	O
</s>
<s>
In	O
order	O
to	O
get	O
a	O
complete	O
gist	O
of	O
thread	B-Operating_System
block	I-Operating_System
,	O
it	O
is	O
critical	O
to	O
know	O
it	O
from	O
a	O
hardware	O
perspective	O
.	O
</s>
<s>
The	O
hardware	O
groups	O
threads	B-Operating_System
that	O
execute	O
the	O
same	O
instruction	O
into	O
warps	O
.	O
</s>
<s>
Several	O
warps	O
constitute	O
a	O
thread	B-Operating_System
block	I-Operating_System
.	O
</s>
<s>
Several	O
thread	B-Operating_System
blocks	I-Operating_System
are	O
assigned	O
to	O
a	O
Streaming	O
Multiprocessor	O
(	O
SM	O
)	O
.	O
</s>
<s>
Several	O
SM	O
constitute	O
the	O
whole	O
GPU	B-Architecture
unit	O
(	O
which	O
executes	O
the	O
whole	O
Kernel	B-Operating_System
Grid	O
)	O
.	O
</s>
<s>
Each	O
architecture	O
in	O
GPU	B-Architecture
(	O
say	O
Kepler	B-General_Concept
or	O
Fermi	B-General_Concept
)	O
consists	O
of	O
several	O
SM	O
or	O
Streaming	O
Multiprocessors	O
.	O
</s>
<s>
These	O
are	O
general	O
purpose	O
processors	O
with	O
a	O
low	O
clock	O
rate	O
target	O
and	O
a	O
small	O
cache	B-General_Concept
.	O
</s>
<s>
An	O
SM	O
is	O
able	O
to	O
execute	O
several	O
thread	B-Operating_System
blocks	I-Operating_System
in	O
parallel	O
.	O
</s>
<s>
As	O
soon	O
as	O
one	O
of	O
its	O
thread	B-Operating_System
blocks	I-Operating_System
has	O
completed	O
execution	O
,	O
it	O
takes	O
up	O
the	O
serially	O
next	O
thread	B-Operating_System
block	I-Operating_System
.	O
</s>
<s>
In	O
general	O
,	O
SMs	O
support	O
instruction-level	B-Operating_System
parallelism	I-Operating_System
but	O
not	O
branch	B-General_Concept
prediction	I-General_Concept
.	O
</s>
<s>
Caches	B-General_Concept
:	O
</s>
<s>
L1	O
cache	B-General_Concept
.	O
</s>
<s>
Shared	B-Operating_System
memory	I-Operating_System
.	O
</s>
<s>
(	O
for	O
shared	B-Operating_System
data	I-Operating_System
between	O
threads	B-Operating_System
)	O
.	O
</s>
<s>
Constant	O
cache	B-General_Concept
(	O
for	O
broadcasting	O
of	O
reads	O
from	O
a	O
read-only	O
memory	O
)	O
.	O
</s>
<s>
Texture	O
cache	B-General_Concept
.	O
</s>
<s>
(	O
an	O
SM	O
may	O
be	O
running	O
a	O
large	O
number	O
of	O
active	O
threads	B-Operating_System
at	O
a	O
time	O
,	O
so	O
it	O
is	O
a	O
must	O
to	O
have	O
registers	O
in	O
thousands	O
.	O
)	O
</s>
<s>
The	O
hardware	O
schedules	O
thread	B-Operating_System
blocks	I-Operating_System
to	O
an	O
SM	O
.	O
</s>
<s>
In	O
general	O
an	O
SM	O
can	O
handle	O
multiple	O
thread	B-Operating_System
blocks	I-Operating_System
at	O
the	O
same	O
time	O
.	O
</s>
<s>
An	O
SM	O
may	O
contain	O
up	O
to	O
8	O
thread	B-Operating_System
blocks	I-Operating_System
in	O
total	O
.	O
</s>
<s>
A	O
thread	B-Operating_System
ID	O
is	O
assigned	O
to	O
a	O
thread	B-Operating_System
by	O
its	O
respective	O
SM	O
.	O
</s>
<s>
Whenever	O
an	O
SM	O
executes	O
a	O
thread	B-Operating_System
block	I-Operating_System
,	O
all	O
the	O
threads	B-Operating_System
inside	O
the	O
thread	B-Operating_System
block	I-Operating_System
are	O
executed	O
at	O
the	O
same	O
time	O
.	O
</s>
<s>
Hence	O
to	O
free	O
a	O
memory	O
of	O
a	O
thread	B-Operating_System
block	I-Operating_System
inside	O
the	O
SM	O
,	O
it	O
is	O
critical	O
that	O
the	O
entire	O
set	O
of	O
threads	B-Operating_System
in	O
the	O
block	O
have	O
concluded	O
execution	O
.	O
</s>
<s>
Each	O
thread	B-Operating_System
block	I-Operating_System
is	O
divided	O
in	O
scheduled	O
units	O
known	O
as	O
a	O
warp	O
.	O
</s>
<s>
On	O
the	O
hardware	O
side	O
,	O
a	O
thread	B-Operating_System
block	I-Operating_System
is	O
composed	O
of	O
‘	O
warps’	O
.	O
</s>
<s>
A	O
warp	O
is	O
a	O
set	O
of	O
32	O
threads	B-Operating_System
within	O
a	O
thread	B-Operating_System
block	I-Operating_System
such	O
that	O
all	O
the	O
threads	B-Operating_System
in	O
a	O
warp	O
execute	O
the	O
same	O
instruction	O
.	O
</s>
<s>
These	O
threads	B-Operating_System
are	O
selected	O
serially	O
by	O
the	O
SM	O
.	O
</s>
<s>
Once	O
a	O
thread	B-Operating_System
block	I-Operating_System
is	O
launched	O
on	O
a	O
multiprocessor	O
(	O
SM	O
)	O
,	O
all	O
of	O
its	O
warps	O
are	O
resident	O
until	O
their	O
execution	O
finishes	O
.	O
</s>
<s>
Thus	O
a	O
new	O
block	O
is	O
not	O
launched	O
on	O
an	O
SM	O
until	O
there	O
is	O
sufficient	O
number	O
of	O
free	O
registers	O
for	O
all	O
warps	O
of	O
the	O
new	O
block	O
,	O
and	O
until	O
there	O
is	O
enough	O
free	O
shared	B-Operating_System
memory	I-Operating_System
for	O
the	O
new	O
block	O
.	O
</s>
<s>
Consider	O
a	O
warp	O
of	O
32	O
threads	B-Operating_System
executing	O
an	O
instruction	O
.	O
</s>
<s>
have	O
not	O
yet	O
been	O
fetched	O
from	O
global	O
memory	O
)	O
,	O
a	O
process	B-Operating_System
called	O
‘	O
context	O
switching’	O
takes	O
place	O
which	O
transfers	O
control	O
to	O
another	O
warp	O
.	O
</s>
<s>
Round	B-Algorithm
Robin	I-Algorithm
(	O
RR	O
)	O
-	O
Instructions	O
are	O
fetched	O
in	O
round	B-Algorithm
robin	I-Algorithm
manner	O
.	O
</s>
<s>
Least	B-General_Concept
Recently	I-General_Concept
Fetched	I-General_Concept
(	O
LRF	O
)	O
-	O
In	O
this	O
policy	O
,	O
warp	O
for	O
which	O
instruction	O
has	O
not	O
been	O
fetched	O
for	O
the	O
longest	O
time	O
gets	O
priority	O
in	O
the	O
fetching	O
of	O
an	O
instruction	O
.	O
</s>
<s>
Thread	O
block-based	O
CAWS	O
(	O
criticality	O
aware	O
warp	O
scheduling	O
)	O
-	O
The	O
emphasis	O
of	O
this	O
scheduling	O
policy	O
is	O
on	O
improving	O
the	O
execution	O
time	O
of	O
the	O
thread	B-Operating_System
blocks	I-Operating_System
.	O
</s>
<s>
By	O
giving	O
priority	O
to	O
the	O
most	O
critical	O
warp	O
,	O
this	O
policy	O
allows	O
thread	B-Operating_System
blocks	I-Operating_System
to	O
finish	O
faster	O
,	O
such	O
that	O
the	O
resources	O
become	O
available	O
quicker	O
.	O
</s>
<s>
Traditional	O
CPU	B-General_Concept
thread	B-Operating_System
context	O
"	O
switching	O
"	O
requires	O
saving	O
and	O
restoring	O
allocated	O
register	O
values	O
and	O
the	O
program	O
counter	O
to	O
off-chip	O
memory	O
(	O
or	O
cache	B-General_Concept
)	O
and	O
is	O
therefore	O
a	O
much	O
more	O
heavyweight	O
operation	O
than	O
with	O
warp	O
context	B-Operating_System
switching	I-Operating_System
.	O
</s>
<s>
All	O
of	O
a	O
warp	O
's	O
register	O
values	O
(	O
including	O
its	O
program	O
counter	O
)	O
remain	O
in	O
the	O
register	O
file	O
,	O
and	O
the	O
shared	B-Operating_System
memory	I-Operating_System
(	O
and	O
cache	B-General_Concept
)	O
remain	O
in	O
place	O
too	O
since	O
these	O
are	O
shared	O
between	O
all	O
the	O
warps	O
in	O
the	O
thread	B-Operating_System
block	I-Operating_System
.	O
</s>
<s>
If	O
each	O
thread	B-Operating_System
in	O
a	O
warp	O
takes	O
a	O
different	O
execution	O
path	O
or	O
if	O
each	O
thread	B-Operating_System
accesses	O
significantly	O
divergent	O
memory	O
then	O
the	O
benefits	O
of	O
the	O
warp	O
architecture	O
are	O
lost	O
and	O
performance	O
will	O
significantly	O
degrade	O
.	O
</s>
