<s>
Automatic	B-General_Concept
vectorization	I-General_Concept
,	O
in	O
parallel	B-Operating_System
computing	I-Operating_System
,	O
is	O
a	O
special	O
case	O
of	O
automatic	O
parallelization	B-Operating_System
,	O
where	O
a	O
computer	B-Application
program	I-Application
is	O
converted	O
from	O
a	O
scalar	O
implementation	O
,	O
which	O
processes	O
a	O
single	O
pair	O
of	O
operands	O
at	O
a	O
time	O
,	O
to	O
a	O
vector	B-Data_Structure
implementation	O
,	O
which	O
processes	O
one	O
operation	O
on	O
multiple	O
pairs	O
of	O
operands	O
at	O
once	O
.	O
</s>
<s>
For	O
example	O
,	O
modern	O
conventional	O
computers	O
,	O
including	O
specialized	O
supercomputers	B-Architecture
,	O
typically	O
have	O
vector	B-Operating_System
operations	I-Operating_System
that	O
simultaneously	O
perform	O
operations	O
such	O
as	O
the	O
following	O
four	O
additions	O
(	O
via	O
SIMD	B-Device
or	O
SPMD	B-Operating_System
hardware	O
)	O
:	O
</s>
<s>
Here	O
is	O
an	O
example	O
of	O
such	O
a	O
loop	O
,	O
written	O
in	O
C	B-Language
:	O
</s>
<s>
A	O
vectorizing	B-General_Concept
compiler	I-General_Concept
transforms	O
such	O
loops	O
into	O
sequences	O
of	O
vector	B-Operating_System
operations	I-Operating_System
.	O
</s>
<s>
These	O
vector	B-Operating_System
operations	I-Operating_System
perform	O
additions	O
on	O
blocks	O
of	O
elements	O
from	O
the	O
arrays	O
a	O
,	O
b	O
and	O
c	B-Language
.	O
Automatic	B-General_Concept
vectorization	I-General_Concept
is	O
a	O
major	O
research	O
topic	O
in	O
computer	O
science	O
.	O
</s>
<s>
So	O
,	O
many	O
optimizing	O
compilers	B-Language
perform	O
automatic	B-General_Concept
vectorization	I-General_Concept
,	O
where	O
parts	O
of	O
sequential	O
programs	O
are	O
transformed	O
into	O
parallel	O
operations	O
.	O
</s>
<s>
Loop	O
vectorization	O
is	O
implemented	O
in	O
Intel	O
's	O
MMX	B-Architecture
,	O
SSE	B-General_Concept
,	O
and	O
AVX	B-General_Concept
,	O
in	O
Power	B-Architecture
ISA	I-Architecture
's	O
AltiVec	B-General_Concept
,	O
and	O
in	O
ARM	O
's	O
NEON	O
,	O
SVE	O
and	O
SVE2	O
instruction	O
sets	O
.	O
</s>
<s>
Sometimes	O
vectorization	O
can	O
slow	O
down	O
execution	O
,	O
for	O
example	O
because	O
of	O
pipeline	B-General_Concept
synchronization	O
or	O
data-movement	O
timing	O
.	O
</s>
<s>
Loop	B-Operating_System
dependence	I-Operating_System
analysis	I-Operating_System
identifies	O
loops	O
that	O
can	O
be	O
vectorized	O
,	O
relying	O
on	O
the	O
data	B-Operating_System
dependence	I-Operating_System
of	O
the	O
instructions	O
inside	O
loops	O
.	O
</s>
<s>
Automatic	B-General_Concept
vectorization	I-General_Concept
,	O
like	O
any	O
loop	O
optimization	O
or	O
other	O
compile-time	O
optimization	O
,	O
must	O
exactly	O
preserve	O
program	O
behavior	O
.	O
</s>
<s>
Integer	O
precision	B-General_Concept
(	O
bit-size	O
)	O
must	O
be	O
kept	O
during	O
vector	B-Data_Structure
instruction	O
execution	O
.	O
</s>
<s>
The	O
correct	O
vector	B-Data_Structure
instruction	O
must	O
be	O
chosen	O
based	O
on	O
the	O
size	O
and	O
behavior	O
of	O
the	O
internal	O
integers	O
.	O
</s>
<s>
Also	O
,	O
with	O
mixed	O
integer	O
types	O
,	O
extra	O
care	O
must	O
be	O
taken	O
to	O
promote/demote	O
them	O
correctly	O
without	O
losing	O
precision	B-General_Concept
.	O
</s>
<s>
Special	O
care	O
must	O
be	O
taken	O
with	O
sign	O
extension	O
(	O
because	O
multiple	O
integers	O
are	O
packed	O
inside	O
the	O
same	O
register	O
)	O
and	O
during	O
shift	O
operations	O
,	O
or	O
operations	O
with	O
carry	B-Algorithm
bits	I-Algorithm
that	O
would	O
otherwise	O
be	O
taken	O
into	O
account	O
.	O
</s>
<s>
Floating-point	B-Algorithm
precision	B-General_Concept
must	O
be	O
kept	O
as	O
well	O
,	O
unless	O
IEEE-754	O
compliance	O
is	O
turned	O
off	O
,	O
in	O
which	O
case	O
operations	O
will	O
be	O
faster	O
but	O
the	O
results	O
may	O
vary	O
slightly	O
.	O
</s>
<s>
To	O
vectorize	O
a	O
program	O
,	O
the	O
compiler	B-Language
's	O
optimizer	O
must	O
first	O
understand	O
the	O
dependencies	O
between	O
statements	O
and	O
re-align	O
them	O
,	O
if	O
necessary	O
.	O
</s>
<s>
Once	O
the	O
dependencies	O
are	O
mapped	O
,	O
the	O
optimizer	O
must	O
properly	O
arrange	O
the	O
implementing	O
instructions	O
changing	O
appropriate	O
candidates	O
to	O
vector	B-Data_Structure
instructions	O
,	O
which	O
operate	O
on	O
multiple	O
data	O
items	O
.	O
</s>
<s>
The	O
dependency	O
graph	O
contains	O
all	O
local	O
dependencies	O
with	O
distance	O
not	O
greater	O
than	O
the	O
vector	B-Data_Structure
size	O
.	O
</s>
<s>
So	O
,	O
if	O
the	O
vector	B-Data_Structure
register	O
is	O
128	O
bits	O
,	O
and	O
the	O
array	O
type	O
is	O
32	O
bits	O
,	O
the	O
vector	B-Data_Structure
size	O
is	O
128/32	O
=	O
4	O
.	O
</s>
<s>
All	O
other	O
non-cyclic	O
dependencies	O
should	O
not	O
invalidate	O
vectorization	O
,	O
since	O
there	O
wo	O
n't	O
be	O
any	O
concurrent	B-Operating_System
access	O
in	O
the	O
same	O
vector	B-Data_Structure
instruction	O
.	O
</s>
<s>
Suppose	O
the	O
vector	B-Data_Structure
size	O
is	O
the	O
same	O
as	O
4	O
ints	O
:	O
</s>
<s>
This	O
normally	O
involves	O
moving	O
them	O
to	O
vector	B-Data_Structure
registers	O
with	O
specific	O
patterns	O
that	O
will	O
be	O
used	O
in	O
vector	B-Data_Structure
instructions	O
.	O
</s>
<s>
Cleanup	O
:	O
Implement	O
plain	O
(	O
non-vectorized	O
)	O
loops	O
for	O
iterations	O
at	O
the	O
end	O
of	O
a	O
loop	O
that	O
are	O
not	O
a	O
multiple	O
of	O
the	O
vector	B-Data_Structure
size	O
or	O
for	O
when	O
run-time	O
checks	O
prohibit	O
vector	B-Operating_System
processing	I-Operating_System
.	O
</s>
<s>
Some	O
vectorizations	O
cannot	O
be	O
fully	O
checked	O
at	O
compile	B-Language
time	O
.	O
</s>
<s>
The	O
following	O
code	O
can	O
easily	O
be	O
vectorized	O
at	O
compile	B-Language
time	O
,	O
as	O
it	O
does	O
n't	O
have	O
any	O
dependence	O
on	O
external	O
parameters	O
.	O
</s>
<s>
Also	O
,	O
the	O
language	O
guarantees	O
that	O
neither	O
will	O
occupy	O
the	O
same	O
region	O
in	O
memory	O
as	O
any	O
other	O
variable	O
,	O
as	O
they	O
are	O
local	O
variables	O
and	O
live	O
only	O
in	O
the	O
execution	O
stack	B-Application
.	O
</s>
<s>
A	O
quick	O
run-time	O
check	O
on	O
the	O
address	B-General_Concept
of	O
both	O
a	O
and	O
b	O
,	O
plus	O
the	O
loop	O
iteration	O
space	O
(	O
128	O
)	O
is	O
enough	O
to	O
tell	O
if	O
the	O
arrays	O
overlap	O
or	O
not	O
,	O
thus	O
revealing	O
any	O
dependencies	O
.	O
</s>
<s>
There	O
exist	O
some	O
tools	O
to	O
dynamically	O
analyze	O
existing	O
applications	O
to	O
assess	O
the	O
inherent	O
latent	O
potential	O
for	O
SIMD	B-Device
parallelism	B-Operating_System
,	O
exploitable	O
through	O
further	O
compiler	B-Language
advances	O
and/or	O
via	O
manual	O
code	O
changes	O
.	O
</s>
<s>
An	O
example	O
would	O
be	O
a	O
program	O
to	O
multiply	O
two	O
vectors	O
of	O
numeric	B-Algorithm
data	O
.	O
</s>
<s>
Here	O
,	O
c[i:i+3]	O
represents	O
the	O
four	O
array	B-Data_Structure
elements	I-Data_Structure
from	O
c[i]	O
to	O
c[i+3]	O
and	O
the	O
vector	B-Operating_System
processor	I-Operating_System
can	O
perform	O
four	O
operations	O
for	O
a	O
single	O
vector	B-Data_Structure
instruction	O
.	O
</s>
<s>
Since	O
the	O
four	O
vector	B-Operating_System
operations	I-Operating_System
complete	O
in	O
roughly	O
the	O
same	O
time	O
as	O
one	O
scalar	O
instruction	O
,	O
the	O
vector	B-Data_Structure
approach	O
can	O
run	O
up	O
to	O
four	O
times	O
faster	O
than	O
the	O
original	O
code	O
.	O
</s>
<s>
There	O
are	O
two	O
distinct	O
compiler	B-Language
approaches	O
:	O
one	O
based	O
on	O
the	O
conventional	O
vectorization	O
technique	O
and	O
the	O
other	O
based	O
on	O
loop	B-Operating_System
unrolling	I-Operating_System
.	O
</s>
<s>
This	O
technique	O
,	O
used	O
for	O
conventional	O
vector	B-Data_Structure
machines	O
,	O
tries	O
to	O
find	O
and	O
exploit	O
SIMD	B-Device
parallelism	B-Operating_System
at	O
the	O
loop	O
level	O
.	O
</s>
<s>
In	O
the	O
first	O
step	O
,	O
the	O
compiler	B-Language
looks	O
for	O
obstacles	O
that	O
can	O
prevent	O
vectorization	O
.	O
</s>
<s>
A	O
major	O
obstacle	O
for	O
vectorization	O
is	O
true	B-Operating_System
data	I-Operating_System
dependency	I-Operating_System
shorter	O
than	O
the	O
vector	B-Data_Structure
length	O
.	O
</s>
<s>
Once	O
the	O
loop	O
is	O
determined	O
to	O
be	O
vectorizable	O
,	O
the	O
loop	O
is	O
stripmined	O
by	O
the	O
vector	B-Data_Structure
length	O
and	O
each	O
scalar	O
instruction	O
within	O
the	O
loop	O
body	O
is	O
replaced	O
with	O
the	O
corresponding	O
vector	B-Data_Structure
instruction	O
.	O
</s>
<s>
This	O
relatively	O
new	O
technique	O
specifically	O
targets	O
modern	O
SIMD	B-Device
architectures	O
with	O
short	O
vector	B-Data_Structure
lengths	O
.	O
</s>
<s>
Although	O
loops	O
can	O
be	O
unrolled	O
to	O
increase	O
the	O
amount	O
of	O
SIMD	B-Device
parallelism	B-Operating_System
in	O
basic	O
blocks	O
,	O
this	O
technique	O
exploits	O
SIMD	B-Device
parallelism	B-Operating_System
within	O
basic	O
blocks	O
rather	O
than	O
loops	O
.	O
</s>
<s>
The	O
innermost	O
loop	O
is	O
unrolled	O
by	O
a	O
factor	O
of	O
the	O
vector	B-Data_Structure
length	O
to	O
form	O
a	O
large	O
loop	O
body	O
.	O
</s>
<s>
Isomorphic	O
scalar	O
instructions	O
(	O
that	O
perform	O
the	O
same	O
operation	O
)	O
are	O
packed	O
into	O
a	O
vector	B-Data_Structure
instruction	O
if	O
dependencies	O
do	O
not	O
prevent	O
doing	O
so	O
.	O
</s>
<s>
Here	O
,	O
sA1	O
,	O
sB1	O
,	O
...	O
represent	O
scalar	O
variables	O
and	O
vA	O
,	O
vB	O
,	O
and	O
vC	O
represent	O
vector	B-Data_Structure
variables	O
.	O
</s>
<s>
Most	O
automatically	O
vectorizing	O
commercial	O
compilers	B-Language
use	O
the	O
conventional	O
loop-level	O
approach	O
except	O
the	O
IBM	O
XL	O
Compiler	B-Language
,	O
which	O
uses	O
both	O
.	O
</s>
<s>
One	O
general	O
approach	O
is	O
to	O
go	O
through	O
a	O
sequence	O
of	O
code	O
transformations	O
:	O
predication	O
→	O
vectorization(using one of the above methods )	O
→	O
remove	O
vector	B-Data_Structure
predicates	O
→	O
remove	O
scalar	O
predicates	O
.	O
</s>
<s>
Having	O
to	O
execute	O
the	O
instructions	O
in	O
all	O
control	O
paths	O
in	O
vector	B-Data_Structure
code	O
has	O
been	O
one	O
of	O
the	O
major	O
factors	O
that	O
slow	O
down	O
the	O
vector	B-Data_Structure
code	O
with	O
respect	O
to	O
the	O
scalar	O
baseline	O
.	O
</s>
<s>
To	O
reduce	O
this	O
vectorization	O
overhead	O
,	O
vector	B-Data_Structure
branches	O
can	O
be	O
inserted	O
to	O
bypass	O
vector	B-Data_Structure
instructions	O
similar	O
to	O
the	O
way	O
scalar	O
branches	O
bypass	O
scalar	O
instructions	O
.	O
</s>
<s>
Below	O
,	O
AltiVec	B-General_Concept
predicates	O
are	O
used	O
to	O
show	O
how	O
this	O
can	O
be	O
achieved	O
.	O
</s>
<s>
There	O
are	O
two	O
things	O
to	O
note	O
in	O
the	O
final	O
code	O
with	O
vector	B-Data_Structure
branches	O
;	O
First	O
,	O
the	O
predicate	O
defining	O
instruction	O
for	O
vPA	O
is	O
also	O
included	O
within	O
the	O
body	O
of	O
the	O
outer	O
vector	B-Data_Structure
branch	O
by	O
using	O
vec_any_gt	O
.	O
</s>
<s>
Second	O
,	O
the	O
profitability	O
of	O
the	O
inner	O
vector	B-Data_Structure
branch	O
for	O
vPB	O
depends	O
on	O
the	O
conditional	O
probability	O
of	O
vPB	O
having	O
false	O
values	O
in	O
all	O
fields	O
given	O
vPA	O
has	O
false	O
values	O
in	O
all	O
fields	O
.	O
</s>
<s>
The	O
intermediate	O
case	O
above	O
,	O
without	O
vector	B-Data_Structure
branches	O
,	O
executes	O
all	O
vector	B-Data_Structure
instructions	O
.	O
</s>
<s>
The	O
final	O
code	O
,	O
with	O
vector	B-Data_Structure
branches	O
,	O
executes	O
both	O
the	O
comparison	O
and	O
the	O
branch	O
in	O
vector	B-Data_Structure
mode	O
,	O
potentially	O
gaining	O
performance	O
over	O
the	O
scalar	O
baseline	O
.	O
</s>
<s>
In	O
most	O
C	B-Language
and	O
C++	B-Language
compilers	B-Language
,	O
it	O
is	O
possible	O
to	O
use	O
intrinsic	B-Application
functions	I-Application
to	O
manually	O
vectorise	O
,	O
at	O
the	O
expense	O
of	O
programmer	O
effort	O
and	O
maintainability	O
.	O
</s>
