<s>
In-place	B-Algorithm
matrix	I-Algorithm
transposition	I-Algorithm
,	O
also	O
called	O
in-situ	B-Algorithm
matrix	I-Algorithm
transposition	I-Algorithm
,	O
is	O
the	O
problem	O
of	O
transposing	O
an	O
N×M	O
matrix	B-Architecture
in-place	B-Algorithm
in	O
computer	B-General_Concept
memory	I-General_Concept
,	O
ideally	O
with	O
O(1 )	O
(	O
bounded	O
)	O
additional	O
storage	O
,	O
or	O
at	O
most	O
with	O
additional	O
storage	O
much	O
less	O
than	O
NM	O
.	O
</s>
<s>
Typically	O
,	O
the	O
matrix	B-Architecture
is	O
assumed	O
to	O
be	O
stored	O
in	O
row-major	B-Data_Structure
or	I-Data_Structure
column-major	I-Data_Structure
order	I-Data_Structure
(	O
i.e.	O
,	O
contiguous	O
rows	O
or	O
columns	O
,	O
respectively	O
,	O
arranged	O
consecutively	O
)	O
.	O
</s>
<s>
Performing	O
an	O
in-place	B-Algorithm
transpose	I-Algorithm
(	O
in-situ	O
transpose	O
)	O
is	O
most	O
difficult	O
when	O
N	O
≠	O
M	O
,	O
i.e.	O
</s>
<s>
for	O
a	O
non-square	O
(	O
rectangular	O
)	O
matrix	B-Architecture
,	O
where	O
it	O
involves	O
a	O
complicated	O
permutation	B-Algorithm
of	O
the	O
data	O
elements	O
,	O
with	O
many	O
cycles	O
of	O
length	O
greater	O
than	O
2	O
.	O
</s>
<s>
In	O
contrast	O
,	O
for	O
a	O
square	O
matrix	B-Architecture
(	O
N	O
=	O
M	O
)	O
,	O
all	O
of	O
the	O
cycles	O
are	O
of	O
length	O
1	O
or	O
2	O
,	O
and	O
the	O
transpose	O
can	O
be	O
achieved	O
by	O
a	O
simple	O
loop	O
to	O
swap	O
the	O
upper	O
triangle	O
of	O
the	O
matrix	B-Architecture
with	O
the	O
lower	O
triangle	O
.	O
</s>
<s>
Further	O
complications	O
arise	O
if	O
one	O
wishes	O
to	O
maximize	O
memory	B-General_Concept
locality	I-General_Concept
in	O
order	O
to	O
improve	O
cache	B-General_Concept
line	I-General_Concept
utilization	O
or	O
to	O
operate	O
out-of-core	B-Application
(	O
where	O
the	O
matrix	B-Architecture
does	O
not	O
fit	O
into	O
main	O
memory	B-Architecture
)	O
,	O
since	O
transposes	O
inherently	O
involve	O
non-consecutive	O
memory	B-Architecture
accesses	O
.	O
</s>
<s>
The	O
problem	O
of	O
non-square	O
in-place	B-Algorithm
transposition	I-Algorithm
has	O
been	O
studied	O
since	O
at	O
least	O
the	O
late	O
1950s	O
,	O
and	O
several	O
algorithms	O
are	O
known	O
,	O
including	O
several	O
which	O
attempt	O
to	O
optimize	O
locality	O
for	O
cache	O
,	O
out-of-core	B-Application
,	O
or	O
similar	O
memory-related	O
contexts	O
.	O
</s>
<s>
On	O
a	O
computer	O
,	O
one	O
can	O
often	O
avoid	O
explicitly	O
transposing	O
a	O
matrix	B-Architecture
in	O
memory	B-Architecture
by	O
simply	O
accessing	O
the	O
same	O
data	O
in	O
a	O
different	O
order	O
.	O
</s>
<s>
For	O
example	O
,	O
software	B-Library
libraries	I-Library
for	O
linear	B-Language
algebra	I-Language
,	O
such	O
as	O
BLAS	B-Application
,	O
typically	O
provide	O
options	O
to	O
specify	O
that	O
certain	O
matrices	O
are	O
to	O
be	O
interpreted	O
in	O
transposed	O
order	O
to	O
avoid	O
data	O
movement	O
.	O
</s>
<s>
However	O
,	O
there	O
remain	O
a	O
number	O
of	O
circumstances	O
in	O
which	O
it	O
is	O
necessary	O
or	O
desirable	O
to	O
physically	O
reorder	O
a	O
matrix	B-Architecture
in	O
memory	B-Architecture
to	O
its	O
transposed	O
ordering	O
.	O
</s>
<s>
For	O
example	O
,	O
with	O
a	O
matrix	B-Architecture
stored	O
in	O
row-major	B-Data_Structure
order	I-Data_Structure
,	O
the	O
rows	O
of	O
the	O
matrix	B-Architecture
are	O
contiguous	O
in	O
memory	B-Architecture
and	O
the	O
columns	O
are	O
discontiguous	O
.	O
</s>
<s>
Frigo	O
&	O
Johnson	O
,	O
2005	O
)	O
,	O
transposing	O
the	O
matrix	B-Architecture
in	O
memory	B-Architecture
(	O
to	O
make	O
the	O
columns	O
contiguous	O
)	O
may	O
improve	O
performance	O
by	O
increasing	O
memory	B-General_Concept
locality	I-General_Concept
.	O
</s>
<s>
Since	O
these	O
situations	O
normally	O
coincide	O
with	O
the	O
case	O
of	O
very	O
large	O
matrices	O
(	O
which	O
exceed	O
the	O
cache	O
size	O
)	O
,	O
performing	O
the	O
transposition	O
in-place	B-Algorithm
with	O
minimal	O
additional	O
storage	O
becomes	O
desirable	O
.	O
</s>
<s>
Also	O
,	O
as	O
a	O
purely	O
mathematical	O
problem	O
,	O
in-place	B-Algorithm
transposition	I-Algorithm
involves	O
a	O
number	O
of	O
interesting	O
number	O
theory	O
puzzles	O
that	O
have	O
been	O
worked	O
out	O
over	O
the	O
course	O
of	O
several	O
decades	O
.	O
</s>
<s>
For	O
example	O
,	O
consider	O
the	O
2×4	O
matrix	B-Architecture
:	O
</s>
<s>
In	O
row-major	B-Data_Structure
format	O
,	O
this	O
would	O
be	O
stored	O
in	O
computer	B-General_Concept
memory	I-General_Concept
as	O
the	O
sequence	O
(	O
11	O
,	O
12	O
,	O
13	O
,	O
14	O
,	O
21	O
,	O
22	O
,	O
23	O
,	O
24	O
)	O
,	O
i.e.	O
</s>
<s>
If	O
we	O
transpose	O
this	O
,	O
we	O
obtain	O
the	O
4×2	O
matrix	B-Architecture
:	O
</s>
<s>
which	O
is	O
stored	O
in	O
computer	B-General_Concept
memory	I-General_Concept
as	O
the	O
sequence	O
(	O
11	O
,	O
21	O
,	O
12	O
,	O
22	O
,	O
13	O
,	O
23	O
,	O
14	O
,	O
24	O
)	O
.	O
</s>
<s>
If	O
we	O
number	O
the	O
storage	O
locations	O
0	O
to	O
7	O
,	O
from	O
left	O
to	O
right	O
,	O
then	O
this	O
permutation	B-Algorithm
consists	O
of	O
four	O
cycles	O
:	O
</s>
<s>
That	O
is	O
,	O
the	O
value	O
in	O
position	O
0	O
goes	O
to	O
position	O
0	O
(	O
a	O
cycle	B-Algorithm
of	O
length	O
1	O
,	O
no	O
data	O
motion	O
)	O
.	O
</s>
<s>
In	O
the	O
following	O
,	O
we	O
assume	O
that	O
the	O
N×M	O
matrix	B-Architecture
is	O
stored	O
in	O
row-major	B-Data_Structure
order	I-Data_Structure
with	O
zero-based	O
indices	O
.	O
</s>
<s>
This	O
means	O
that	O
the	O
(	O
n	O
,	O
m	O
)	O
element	O
,	O
for	O
n	O
=	O
0	O
,...,	O
N1	O
and	O
m	O
=	O
0	O
,...,	O
M1	O
,	O
is	O
stored	O
at	O
an	O
address	O
a	O
=	O
Mn	O
+	O
m	O
(	O
plus	O
some	O
offset	O
in	O
memory	B-Architecture
,	O
which	O
we	O
ignore	O
)	O
.	O
</s>
<s>
In	O
the	O
transposed	O
M×N	O
matrix	B-Architecture
,	O
the	O
corresponding	O
(	O
m	O
,	O
n	O
)	O
element	O
is	O
stored	O
at	O
the	O
address	O
a	O
 '	O
=	O
Nm	O
+	O
n	O
,	O
again	O
in	O
row-major	B-Data_Structure
order	I-Data_Structure
.	O
</s>
<s>
We	O
define	O
the	O
transposition	O
permutation	B-Algorithm
to	O
be	O
the	O
function	O
a	O
 '	O
=	O
P(a )	O
such	O
that	O
:	O
</s>
<s>
This	O
defines	O
a	O
permutation	B-Algorithm
on	O
the	O
numbers	O
.	O
</s>
<s>
Second	O
,	O
the	O
inverse	O
permutation	B-Algorithm
is	O
given	O
by	O
:	O
</s>
<s>
As	O
proved	O
by	O
Cate	O
&	O
Twigg	O
(	O
1977	O
)	O
,	O
the	O
number	O
of	O
fixed	O
points	O
(	O
cycles	O
of	O
length	O
1	O
)	O
of	O
the	O
permutation	B-Algorithm
is	O
precisely	O
,	O
where	O
gcd	O
is	O
the	O
greatest	O
common	O
divisor	O
.	O
</s>
<s>
For	O
example	O
,	O
with	O
N	O
=	O
M	O
the	O
number	O
of	O
fixed	O
points	O
is	O
simply	O
N	O
(	O
the	O
diagonal	O
of	O
the	O
matrix	B-Architecture
)	O
.	O
</s>
<s>
If	O
and	O
are	O
coprime	O
,	O
on	O
the	O
other	O
hand	O
,	O
the	O
only	O
two	O
fixed	O
points	O
are	O
the	O
upper-left	O
and	O
lower-right	O
corners	O
of	O
the	O
matrix	B-Architecture
.	O
</s>
<s>
Furthermore	O
,	O
the	O
cycle	B-Algorithm
containing	O
a	O
=	O
1	O
(	O
i.e.	O
</s>
<s>
the	O
second	O
element	O
of	O
the	O
first	O
row	O
of	O
the	O
matrix	B-Architecture
)	O
is	O
always	O
a	O
cycle	B-Algorithm
of	O
maximum	O
length	O
L	O
,	O
and	O
the	O
lengths	O
k	O
of	O
all	O
other	O
cycles	O
must	O
be	O
divisors	O
of	O
L	O
(	O
Cate	O
&	O
Twigg	O
,	O
1977	O
)	O
.	O
</s>
<s>
For	O
a	O
given	O
cycle	B-Algorithm
C	B-Language
,	O
every	O
element	O
has	O
the	O
same	O
greatest	O
common	O
divisor	O
.	O
</s>
<s>
Let	O
s	O
be	O
the	O
smallest	O
element	O
of	O
the	O
cycle	B-Algorithm
,	O
and	O
.	O
</s>
<s>
From	O
the	O
definition	O
of	O
the	O
permutation	B-Algorithm
P	O
above	O
,	O
every	O
other	O
element	O
x	O
of	O
the	O
cycle	B-Algorithm
is	O
obtained	O
by	O
repeatedly	O
multiplying	O
s	O
by	O
N	O
modulo	O
MN1	O
,	O
and	O
therefore	O
every	O
other	O
element	O
is	O
divisible	O
by	O
d	O
.	O
But	O
,	O
since	O
N	O
and	O
are	O
coprime	O
,	O
x	O
cannot	O
be	O
divisible	O
by	O
any	O
factor	O
of	O
larger	O
than	O
d	O
,	O
and	O
hence	O
.	O
</s>
<s>
This	O
theorem	O
is	O
useful	O
in	O
searching	O
for	O
cycles	O
of	O
the	O
permutation	B-Algorithm
,	O
since	O
an	O
efficient	O
search	O
can	O
look	O
only	O
at	O
multiples	O
of	O
divisors	O
of	O
MN1	O
(	O
Brenner	O
,	O
1973	O
)	O
.	O
</s>
<s>
In	O
particular	O
,	O
let	O
s	O
be	O
the	O
smallest	O
element	O
of	O
some	O
cycle	B-Algorithm
C	B-Language
of	O
length	O
k	O
.	O
It	O
follows	O
that	O
MN1s	O
is	O
also	O
an	O
element	O
of	O
a	O
cycle	B-Algorithm
of	O
length	O
k	O
(	O
possibly	O
the	O
same	O
cycle	B-Algorithm
)	O
.	O
</s>
<s>
The	O
length	O
k	O
of	O
the	O
cycle	B-Algorithm
containing	O
s	O
is	O
the	O
smallest	O
k	O
0	O
such	O
that	O
.	O
</s>
<s>
The	O
following	O
briefly	O
summarizes	O
the	O
published	O
algorithms	O
to	O
perform	O
in-place	B-Algorithm
matrix	I-Algorithm
transposition	I-Algorithm
.	O
</s>
<s>
Because	O
physically	O
transposing	O
a	O
matrix	B-Architecture
is	O
computationally	O
expensive	O
,	O
instead	O
of	O
moving	O
values	O
in	O
memory	B-Architecture
,	O
the	O
access	O
path	O
may	O
be	O
transposed	O
instead	O
.	O
</s>
<s>
For	O
a	O
square	O
N×N	O
matrix	B-Architecture
An	O
,	O
m	O
=	O
A(n,m )	O
,	O
in-place	B-Algorithm
transposition	I-Algorithm
is	O
easy	O
because	O
all	O
of	O
the	O
cycles	O
have	O
length	O
1	O
(	O
the	O
diagonals	O
An	O
,	O
n	O
)	O
or	O
length	O
2	O
(	O
the	O
upper	O
triangle	O
is	O
swapped	O
with	O
the	O
lower	O
triangle	O
)	O
.	O
</s>
<s>
Pseudocode	B-Language
to	O
accomplish	O
this	O
(	O
assuming	O
zero-based	O
array	B-Data_Structure
indices	O
)	O
is	O
:	O
</s>
<s>
This	O
type	O
of	O
implementation	O
,	O
while	O
simple	O
,	O
can	O
exhibit	O
poor	O
performance	O
due	O
to	O
poor	O
cache-line	O
utilization	O
,	O
especially	O
when	O
N	O
is	O
a	O
power	O
of	O
two	O
(	O
due	O
to	O
cache-line	O
conflicts	O
in	O
a	O
CPU	B-General_Concept
cache	I-General_Concept
with	O
limited	O
associativity	O
)	O
.	O
</s>
<s>
The	O
reason	O
for	O
this	O
is	O
that	O
,	O
as	O
m	O
is	O
incremented	O
in	O
the	O
inner	O
loop	O
,	O
the	O
memory	B-Architecture
address	O
corresponding	O
to	O
A(n,m )	O
or	O
A(m,n )	O
jumps	O
discontiguously	O
by	O
N	O
in	O
memory	B-Architecture
(	O
depending	O
on	O
whether	O
the	O
array	B-Data_Structure
is	O
in	O
column-major	O
or	O
row-major	B-Data_Structure
format	O
,	O
respectively	O
)	O
.	O
</s>
<s>
That	O
is	O
,	O
the	O
algorithm	O
does	O
not	O
exploit	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
.	O
</s>
<s>
One	O
solution	O
to	O
improve	O
the	O
cache	O
utilization	O
is	O
to	O
"	O
block	O
"	O
the	O
algorithm	O
to	O
operate	O
on	O
several	O
numbers	O
at	O
once	O
,	O
in	O
blocks	O
given	O
by	O
the	O
cache-line	O
size	O
;	O
unfortunately	O
,	O
this	O
means	O
that	O
the	O
algorithm	O
depends	O
on	O
the	O
size	O
of	O
the	O
cache	B-General_Concept
line	I-General_Concept
(	O
it	O
is	O
"	O
cache-aware	O
"	O
)	O
,	O
and	O
on	O
a	O
modern	O
computer	O
with	O
multiple	O
levels	O
of	O
cache	O
it	O
requires	O
multiple	O
levels	O
of	O
machine-dependent	O
blocking	O
.	O
</s>
<s>
Instead	O
,	O
it	O
has	O
been	O
suggested	O
(	O
Frigo	O
et	O
al.	O
,	O
1999	O
)	O
that	O
better	O
performance	O
can	O
be	O
obtained	O
by	O
a	O
recursive	O
algorithm	O
:	O
divide	O
the	O
matrix	B-Architecture
into	O
four	O
submatrices	O
of	O
roughly	O
equal	O
size	O
,	O
transposing	O
the	O
two	O
submatrices	O
along	O
the	O
diagonal	O
recursively	O
and	O
transposing	O
and	O
swapping	O
the	O
two	O
submatrices	O
above	O
and	O
below	O
the	O
diagonal	O
.	O
</s>
<s>
This	O
is	O
a	O
cache-oblivious	B-Application
algorithm	I-Application
,	O
in	O
the	O
sense	O
that	O
it	O
can	O
exploit	O
the	O
cache	B-General_Concept
line	I-General_Concept
without	O
the	O
cache-line	O
size	O
being	O
an	O
explicit	O
parameter	O
.	O
</s>
<s>
That	O
is	O
,	O
they	O
loop	O
over	O
the	O
cycles	O
,	O
moving	O
the	O
data	O
from	O
one	O
location	O
to	O
the	O
next	O
in	O
the	O
cycle	B-Algorithm
.	O
</s>
<s>
In	O
pseudocode	B-Language
form	O
:	O
</s>
<s>
The	O
differences	O
between	O
the	O
algorithms	O
lie	O
mainly	O
in	O
how	O
they	O
locate	O
the	O
cycles	O
,	O
how	O
they	O
find	O
the	O
starting	O
addresses	O
in	O
each	O
cycle	B-Algorithm
,	O
and	O
how	O
they	O
ensure	O
that	O
each	O
cycle	B-Algorithm
is	O
moved	O
exactly	O
once	O
.	O
</s>
<s>
Typically	O
,	O
as	O
discussed	O
above	O
,	O
the	O
cycles	O
are	O
moved	O
in	O
pairs	O
,	O
since	O
s	O
and	O
MN1s	O
are	O
in	O
cycles	O
of	O
the	O
same	O
length	O
(	O
possibly	O
the	O
same	O
cycle	B-Algorithm
)	O
.	O
</s>
<s>
Sometimes	O
,	O
a	O
small	O
scratch	O
array	B-Data_Structure
,	O
typically	O
of	O
length	O
M+N	O
(	O
e.g.	O
</s>
<s>
Brenner	O
,	O
1973	O
;	O
Cate	O
&	O
Twigg	O
,	O
1977	O
)	O
is	O
used	O
to	O
keep	O
track	O
of	O
a	O
subset	O
of	O
locations	O
in	O
the	O
array	B-Data_Structure
that	O
have	O
been	O
visited	O
,	O
to	O
accelerate	O
the	O
algorithm	O
.	O
</s>
<s>
In	O
order	O
to	O
determine	O
whether	O
a	O
given	O
cycle	B-Algorithm
has	O
been	O
moved	O
already	O
,	O
the	O
simplest	O
scheme	O
would	O
be	O
to	O
use	O
O(MN )	O
auxiliary	O
storage	O
,	O
one	O
bit	O
per	O
element	O
,	O
to	O
indicate	O
whether	O
a	O
given	O
element	O
has	O
been	O
moved	O
.	O
</s>
<s>
However	O
,	O
they	O
also	O
involve	O
a	O
considerable	O
amount	O
of	O
arithmetic	O
to	O
compute	O
the	O
cycles	O
,	O
and	O
require	O
heavily	O
non-consecutive	O
memory	B-Architecture
accesses	O
since	O
the	O
adjacent	O
elements	O
of	O
the	O
cycles	O
differ	O
by	O
multiplicative	O
factors	O
of	O
N	O
,	O
as	O
discussed	O
above	O
.	O
</s>
<s>
Several	O
algorithms	O
have	O
been	O
designed	O
to	O
achieve	O
greater	O
memory	B-General_Concept
locality	I-General_Concept
at	O
the	O
cost	O
of	O
greater	O
data	O
movement	O
,	O
as	O
well	O
as	O
slightly	O
greater	O
storage	O
requirements	O
.	O
</s>
<s>
That	O
is	O
,	O
they	O
may	O
move	O
each	O
data	O
element	O
more	O
than	O
once	O
,	O
but	O
they	O
involve	O
more	O
consecutive	O
memory	B-Architecture
access	O
(	O
greater	O
spatial	O
locality	O
)	O
,	O
which	O
can	O
improve	O
performance	O
on	O
modern	O
CPUs	O
that	O
rely	O
on	O
caches	O
,	O
as	O
well	O
as	O
on	O
SIMD	B-Device
architectures	O
optimized	O
for	O
processing	O
consecutive	O
data	O
blocks	O
.	O
</s>
<s>
The	O
oldest	O
context	O
in	O
which	O
the	O
spatial	O
locality	O
of	O
transposition	O
seems	O
to	O
have	O
been	O
studied	O
is	O
for	O
out-of-core	B-Application
operation	O
(	O
by	O
Alltop	O
,	O
1975	O
)	O
,	O
where	O
the	O
matrix	B-Architecture
is	O
too	O
large	O
to	O
fit	O
into	O
main	O
memory	B-Architecture
(	O
"	O
core	O
"	O
)	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
d	O
=	O
gcd(N,M )	O
is	O
not	O
small	O
,	O
one	O
can	O
perform	O
the	O
transposition	O
using	O
a	O
small	O
amount	O
(	O
NM/d	O
)	O
of	O
additional	O
storage	O
,	O
with	O
at	O
most	O
three	O
passes	O
over	O
the	O
array	B-Data_Structure
(	O
Alltop	O
,	O
1975	O
;	O
Dow	O
,	O
1995	O
)	O
.	O
</s>
<s>
Two	O
of	O
the	O
passes	O
involve	O
a	O
sequence	O
of	O
separate	O
,	O
small	O
transpositions	O
(	O
which	O
can	O
be	O
performed	O
efficiently	O
out	O
of	O
place	O
using	O
a	O
small	O
buffer	O
)	O
and	O
one	O
involves	O
an	O
in-place	B-Algorithm
dd	O
square	O
transposition	O
of	O
blocks	O
(	O
which	O
is	O
efficient	O
since	O
the	O
blocks	O
being	O
moved	O
are	O
large	O
and	O
consecutive	O
,	O
and	O
the	O
cycles	O
are	O
of	O
length	O
at	O
most	O
2	O
)	O
.	O
</s>
<s>
Frigo	O
&	O
Johnson	O
(	O
2005	O
)	O
describe	O
the	O
adaptation	O
of	O
these	O
algorithms	O
to	O
use	O
cache-oblivious	B-Application
techniques	O
for	O
general-purpose	O
CPUs	O
relying	O
on	O
cache	B-General_Concept
lines	I-General_Concept
to	O
exploit	O
spatial	O
locality	O
.	O
</s>
<s>
Work	O
on	O
out-of-core	B-Application
matrix	B-Architecture
transposition	O
,	O
where	O
the	O
matrix	B-Architecture
does	O
not	O
fit	O
in	O
main	O
memory	B-Architecture
and	O
must	O
be	O
stored	O
largely	O
on	O
a	O
hard	B-Device
disk	I-Device
,	O
has	O
focused	O
largely	O
on	O
the	O
N	O
=	O
M	O
square-matrix	O
case	O
,	O
with	O
some	O
exceptions	O
(	O
e.g.	O
</s>
<s>
Recent	O
reviews	O
of	O
out-of-core	B-Application
algorithms	O
,	O
especially	O
as	O
applied	O
to	O
parallel	B-Operating_System
computing	I-Operating_System
,	O
can	O
be	O
found	O
in	O
e.g.	O
</s>
