In my recent studies, I have come across a very strange anomaly with C compiled functions. When I call a function that has the 'Listable' attribute while global variables are accessed through MainEvaluate[...], certain arguments lose their value in the calculation and are treated as 0.
I have constructed a minimal (non) working example below where I declare a listable function with Compile.
func = Compile[{
{arg1,_Integer}, {arg2,_Real,2}, {arg3,_Integer,1},
{arg4,_Integer},{arg5,_Real,2},{arg6,_Integer,1},
{arg7,_Real,3},{arg8,_Real},{arg9,_Real},{arg10,_Real},
{arg11,_Real,2}, {arg12,_Real},{arg13,_Real},
{arg15, True|False}},
If[arg4 == 1,
varG = True;
];
{arg1,arg4,arg8,arg9,arg10,arg12,arg13}
,CompilationTarget->"C"
,RuntimeAttributes->{Listable}, Parallelization->True
];
varG = False;
Calling this function with the following arguments will return:
func [
1,{{1.,1.,1.}},{1},
Range[2],
Table[{{1.,1.,1.}},2],
Table[{1},2],
{{{1.,1.,1.}}},
1.,1.,1.,
{{1.,1.}},
1.,1.,
True
]
(*Return: {{1, 1, 1., 1., 2.04315*10^-316, 1., 1.},
{1., 2., 1., 1., 1., 1., 1.}}*)
Does someone know why the 10th argument gets treated as aproximately 0 instead of 1? I have chosen this example to also showcase that this anomily does not happen if the computation does not call the MainEvaluate function.
I am using Mathematica 13.1 on Windows 10 and the C-compiler GCC 12.2.0 for compilation.