Message Boards Message Boards

GROUPS:

What Input Types should I use in my Compiled Function?

Posted 11 months ago
1499 Views
|
4 Replies
|
3 Total Likes
|
    cIterReal = 
         Compile[{{vecPosAct, _Real}, {boxWidth, _Real}, {boxWidth2, _Real}, \
        {c, _Integer}},
          (*Module[{vecPosActNew,check},*)
          vecPosActNew = vecPosAct;
          Do[
           vecPosActNew[[check]] -= 
            Round[(vecPosAct[[check]] - boxWidth2)/boxWidth]*
             boxWidth2*1., {check, 1, c, 1}], CompilationTarget -> "C", 
          CompilationOptions -> {"InlineExternalDefinitions" -> True}];

        cIterReal[{{3.2, 4., 9.}, {2.3, 4., 9.}, {9.2, 3.2, 5.0}}, 2.2, 1.1,
          3];

Then I get this: CompiledFunction::cfsa: Argument {{3.2,4.,9.},{2.3,4.,9.},{9.2,3.2,5.}} at position 1 should be a machine-size real number.

If anyone could give me some advice, I also tried {_Real,2 } and then it continuous with the uncompiled code.

4 Replies
Posted 11 months ago

I would be surprised if the performance is improved since Plus, Round, Minus, Power have the Listable attribute, so no loop is needed.

boxWidth = 2.2; boxWidth2 = 1.1;
list = {{3.2, 4., 9.}, {2.3, 4., 9.}, {9.2, 3.2, 5.0}};

list -= Round[(list - boxWidth2)/boxWidth]*boxWidth2
(* {{2.1, 2.9, 4.6}, {1.2, 2.9, 4.6}, {4.8, 2.1, 2.8}} *)
Posted 11 months ago

Martin:

Please bear with me. I am also learning about compiled functions. And you taught me how to pass in lists and matrices to a compiled function. THANKS

I managed to get this to work:

cIterReal = Compile[
   {
    {vecPosAct, _Real, 2},
    {boxWidth, _Real},
    {boxWidth2, _Real},
    {c, _Integer}
    }
   ,
   Module[
    {
     vecPosActNew,
     check
     }
    ,
    vecPosActNew = vecPosAct;
    Do[
     vecPosActNew[[check]] -= 
      Round[(vecPosAct[[check]] - boxWidth2)/boxWidth]*boxWidth2*1.
     ,
     {check, 1, c, 1}
     ];
    vecPosActNew
    ],

   CompilationTarget -> "C"
   , CompilationOptions -> {"InlineExternalDefinitions" -> True}
   ];

Now,

cIterReal[{{3.2, 4., 9.}, {2.3, 4., 9.}, {9.2, 3.2, 5.0}}, 2.2, 1.1, 3]

Returns:

{{2.1, 2.9, 4.6}, {1.2, 2.9, 4.6}, {4.8, 2.1, 2.8}}

Remember that Do does not return anything. The docs say:

Unless an explicit Return is used, the value returned by Do is Null.

I hope that helps. I'd be interested to hear how much of a performance boost this gives you.

Have a great rest of your week.

Thank you very much Mike fyi its a part of an Verlet Algorithm that I'm trying to speed up.

So the argument vecPosAct is in reality a long nested list and thats pretty much the problem, I don't know how to tell the compiler that its a nested list with reals.

For my understanding {_Real,2} should work, but it does not and I don't know why since vecPosAct is a rank 2 argument?

Again thank you very much for helping me out.

Posted 11 months ago

Martin:

I am a bit confused as to what you are trying to do. Your compiled function code has some syntax errors. I'm not sure, but I think this fixes those errors:

cIterReal = Compile[
   {
    {vecPosAct, _Real},
    {boxWidth, _Real},
    {boxWidth2, _Real}, 
    {c, _Integer}
    },
   Module[
    {vecPosActNew, check}, 
    vecPosActNew = vecPosAct; 
    Do[vecPosActNew[[check]] -= 
      Round[(vecPosAct[[check]] - boxWidth2)/boxWidth]*
       boxWidth2*1., {check, 1, c, 1}
     ]
    ],
   CompilationTarget -> "C",
   CompilationOptions -> {"InlineExternalDefinitions" -> True}
 ];

It is important that you use "_Real" for your types and not just "Real".

The next issue I think I see is your call to the compiled function:

cIterReal[{{3.2, 4., 9.}, {2.3, 4., 9.}, {9.2, 3.2, 5.0}}, 2.2, 1.1, 3];

Your compiled function expects its first argument to be a Real. but you are passing in:

{{3.2, 4., 9.}, {2.3, 4., 9.}, {9.2, 3.2, 5.0}}

Which is a nest list of reals, but not a real.

I hope that helps.

Have a great week.

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