This is the strangest bug I've encountered. In my experience, variations in the code will unexpectedly crash the kernel, give inconsistent output, or produce expected results with no problems.
1) Code-Quits-Kernel Version
Warning: this will crash your kernel.
To reproduce this bug, follow the steps below.
- Copy the following code as is into a notebook. (It is all one cell.)
ClearAll[compbbbeven]
compbbbeven =
Compile[{{filled, _Integer, 2}, {prohibited, _Integer,
2}, {col, _Integer}},
Union[
Join[prohibited,
Flatten[
MapIndexed[
Function[{r, c}, Join[{{1, 1}, {2, 2}}]][First[#2], #1] &,
filled[[All, 1]]], 1]]], RuntimeAttributes -> {Listable},
Parallelization -> True];
filled = {{1, 0}, {5, 0}};
prohibited = {{4, 4}, {5, 2}, {3, 3}, {6, 3}};
col = {1, 2};
Array[compbbbeven[filled, prohibited, col] &, 50] // Column
- Quite your kernel.
- Start your kernel.
- Run the code from step 1.
2) No problems Version
This is the same code as in version 1 above except the 50 is changed to a 1 in the call to Array
. Notice there is no kernel crash and output is produced with no error.
ClearAll[compbbbeven]
compbbbeven =
Compile[{{filled, _Integer, 2}, {prohibited, _Integer,
2}, {col, _Integer}},
Union[
Union[prohibited,
Flatten[
MapIndexed[
Function[{r, c}, Join[{{1, 1}, {2, 2}}]][First[#2], #1] &,
filled[[All, 1]]], 1]]], RuntimeAttributes -> {Listable},
Parallelization -> True];
filled = {{1, 0}, {5, 0}};
prohibited = {{4, 4}, {5, 2}, {3, 3}, {6, 3}};
col = {1, 2};
Array[compbbbeven[filled, prohibited, col] &, 1] // Column
(*output is {{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 2}, {6, 3}}, {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 2}, {6, 3}}}*)
3) Inconsistent Output Version
Running the following code produces inconsistent output.
n = 6;
row = 3;
filled = {{1, 0}, {5, 0}};
prohibited = {{4, 4}, {5, 2}, {3, 3}, {6, 3}};
ClearAll[compbbbeven]
compbbbeven =
Compile[{{n, _Integer}, {row, _Integer}, {filled, _Integer,
2}, {prohibited, _Integer, 2}, {col, _Integer}},
{Join[filled, {{col, 0}},
Array[{0, 0} &, Length[#] - 1 - Length[filled]]], #} &[
IntegerPart@Union[Join[prohibited,
{{Mod[row + n/2, n, 1], Mod[col + n/2, n, 1]}},
DeleteCases[
Flatten[
MapIndexed[
Function[{r, c}, Join[{{1, 1}, {2, 2}}]][First[#2], #1] &,
DeleteCases[filled[[All, 1]], 0]], 1], {0., 0.}]
]]], RuntimeAttributes -> {Listable}, CompilationTarget -> "C",
Parallelization -> True];
col = {1, 2};
Equal @@ Array[compbbbeven[n, row, filled, prohibited, col] &, 50]
Equal @@ Array[compbbbeven[n, row, filled, prohibited, col] &, 50]
Equal @@ Array[compbbbeven[n, row, filled, prohibited, col] &, 50]
Equal @@ Array[compbbbeven[n, row, filled, prohibited, col] &, 50]
Equal @@ Array[compbbbeven[n, row, filled, prohibited, col] &, 50]
The output I received from running the code in one trial was as follows, but it changes from trial to trial.
The original code was bigger, but I was able to reproduce the problems with the above shorter code segments many times. If the bug can be reproduced, I can update my question with shorter code exhibiting the same behavior found by others.
What is going on here?
Notes
- My Mathematica is version 12.3.1.0 and my OS is macOS Big Sur Version 11.6
- The code quits kernel behavior was reproduced with Mathematica 12.3.0.0 on Windows 10 and with Mathematica version 12.3.1 on macOS 10.15.7 (Catalina). (see comments)
- There is a report the version 1 code runs fine with Mathematica 12.0 on macOS 10.15.7 (Catalina). (see comments)