Message Boards Message Boards

0
|
2077 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Argument value loss using Listable C-Compiled Function

Posted 1 year ago

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.

POSTED BY: Michael Haring
4 Replies

Crossposted here.

POSTED BY: Rohit Namjoshi

You may have figure this out by now, but as far as I can tell some of the augments entered were not the same rank as those declared leading to the unexpected behavior- so I fixed that. You also had an uninitialized variable (varG). Everything should be initialized in a Compile[] to avoid a call to the Evaluator. You can check that with the Needs[] included below. Hope it helps,

    func = Compile[{{arg1, _Integer}, {arg2, _Real, 2}, 
    {arg3, _Integer, 1}, {arg4, _Integer}, {arg5, _Real, 2},
     {arg6, _Integer,2}, {arg7, _Real, 3}, {arg8, _Real}, 
    {arg9, _Real}, {arg10, _Real}, {arg11, _Real, 2},
     {arg12, _Real}, {arg13, _Real}, {arg15, True | False}},
        Module[{varG = False}, 
        If[arg4 == 1, varG = True;];
        {arg1, arg4, arg8, arg9, arg10, arg12, arg13}
    ]];


 func[1, {{1., 1., 1.}}, {1}, 1, {{1}, {1}}, 
Table[{1},2], {{{1., 1., 1.}}}, 1., 1., 1., {{1., 1.}}, 1., 1., True]

     << CompiledFunctionTools`;
        CompilePrint@func
POSTED BY: Neal Maroney

Thank you very much for your answer to my issue.

Unfortunately, there are some problems with your suggestion. First, the variable "varG" was meant to be a global variable accessed on rare occaisions of the execution in func and therefore needs to be accesed through the MainEvatuate[] function.

Second, changing the rank of the arguments is not an option for my calculations as i use the "listable" attribute to transfer non-tensor lists to the C library. (For example, the 5th argument would not be a tensor of depth 3, but a nested list of max depth 3.)

My best solution to the problem so far is to change the order of the arguments so that the higher depth elements (for listable mapping) are at the end of the argument chain.

POSTED BY: Michael Haring

Yes, given your situation those changes makes sense. Glad you got things solved.

POSTED BY: Neal Maroney
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract