First load the package so we can do CompilePrint
Needs["CompiledFunctionTools`"]
Simplifying your minimal model further we see that Compile
does not like Sum
with s
as a symbol; MainEvaluate
gets called.
f = Compile[
{{m, _Integer}, {n, _Integer}},
Sum[
Module[
{s},
s = 1;
{{0, 1}, {1, 0}}
],
{j, 1, 2}
]
];
f[1, 1] (* {{0, 2}, {2, 0}} with error message *)
CompilePrint[f]
2 arguments
3 Integer registers
1 Real register
1 Tensor register
Underflow checking off
Overflow checking off
Integer overflow checking on
RuntimeAttributes -> {}
I0 = A1
I1 = A2
I2 = 1
T(I2)0 = {{0, 1}, {1, 0}}
Result = R0
1 R0 = MainEvaluate[ Function[{m, n}, Sum[Module[{s}, s = 1; {{0, 1}, \
{1, 0}}], {j, 1, 2}]][ I0, I1]]
2 Return
If we initialise s=1
in the Module
Compile
is happier and can proceed with the Sum
g = Compile[
{{m, _Integer}, {n, _Integer}},
Sum[
Module[
{s = 1},
{{0, 1}, {1, 0}}
],
{j, 1, 2}
]
]
g[1, 1] (* {{0, 2}, {2, 0}} *)
CompilePrint[g]
2 arguments
15 Integer registers
4 Tensor registers
Underflow checking off
Overflow checking off
Integer overflow checking on
RuntimeAttributes -> {}
I0 = A1
I1 = A2
I12 = 0
I14 = 2
I2 = 1
T(I2)0 = {{0, 1}, {1, 0}}
Result = T(I2)3
1 T(I3)1 = {T(I2)0}
2 T(I2)3 = Part[ T(I3)1, I2]
3 I8 = Length[ T(I2)3]
4 T(I3)3 = {T(I2)0}
5 T(I2)1 = Part[ T(I3)3, I2]
6 T(I1)3 = Part[ T(I2)1, I2]
7 I10 = Length[ T(I1)3]
8 I3 = I12
9 T(I2)3 = Table[ I8, I10]
10 I6 = I12
11 goto 16
12 I9 = I12
13 goto 15
14 Element[ T(I2)3, I3] = I12
15 if[ ++ I9 <= I10] goto 14
16 if[ ++ I6 <= I8] goto 12
17 I4 = I14
18 I5 = I12
19 goto 22
20 T(I2)1 = T(I2)3 + T(I2)0
21 T(I2)3 = CopyTensor[ T(I2)1]]
22 if[ ++ I5 <= I4] goto 20
23 Return