Message Boards Message Boards

0
|
12082 Views
|
11 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Compile on lists?

Posted 11 years ago

Hi,

Is it possible to instruct Compile to take a list as arguments without having to hardcode each individual component of the list?

For example, suppose I want to take the dot product of a matrix and a vector:

Compile[{a,b,c,d,e,f,g},{{a,b},{c,d}}.{f,g}]

would do the trick, but notice the level of detail that I have to specify. Moreover, what if I do not know the dimension of the list I will feed to this function?

I guess a second-best solution would be to use meta functions inside Compile, something like

myComp[list1,list2]:=Compile[<some function that "separates" the elements from the lists>,<operation>]

Any guesses on what those meta functions should be?

POSTED BY: Miguel Olivo-V
11 Replies
Posted 11 years ago

Thank you, I will try that.

Any idea why this function takes forever to compile? Compilation to WVM takes maybe a second, while compilation to C has been running for 1 hour.

Compile[{{m, _Real, 1}}, 
  Log[Exp[P + \[Beta] F1.m] + Exp[\[Beta] F0.m]], 
  CompilationOptions -> {"InlineExternalDefinitions" -> True}, 
  Parallelization -> True, CompilationTarget -> "C"];

P is a 486-vector, beta is a scalar and F1, F0 are 486-square matrices. All have numeric components.

I'm on a Mac with Yosemite and Xcode, I have GCC and Clang. Clang is the default compiler.

POSTED BY: Miguel Olivo-V

If you compile to WVM you can use DumpSave[]:

SetDirectory[NotebookDirectory[]] ;
F = Compile[{{x}}, x^2 + Sin[x^2]];
DumpSave["F.mx", F] ;
Clear[F];
<< F.mx ;
F[10.]

For C functions, Mathematica creates a library (dll or so) and then link it. The library is saved in a temporarily directory and removed when session ends. It is possible to copy created lib and use it latter. All relevant data needed to link the function:

SetDirectory[NotebookDirectory[]] ;
<< CompiledFunctionTools`;
F = Compile[{{x}},x^2+Sin[x^2],CompilationTarget->"C"];
F[[-1,1]] (* file location *)
F[[-1,2]] (* generic function name*)
F[[-1,3]] (* in arg *)
F[[-1,4]] (* out arg *)

Here is an example:

(* copy lib and get loading data *)
SaveFunc[name_,file_]:=Block[
       {
         data=Last[name]
       },
         CopyFile[
          data[[1]],
          StringJoin[file,".dll"] (* replace with  .so on linux *)
         ];
         {file,data[[2]],data[[3]],data[[4]]}
    ];
(* load  lib *)
LoadFunc[savedata_]:=Block[
       { },
       FindLibrary[Directory[]<>StringJoin["/",savedata[[1]]]];
       LibraryFunctionLoad[
         Directory[]<>StringJoin["/",savedata[[1]]],
         savedata[[2]],
         savedata[[3]],
         savedata[[4]]
       ]
    ];
savedata = SaveFunc[F,"TEST"]
G = LoadFunc[savedata]
F[10.]
G[10.]

From my experience it is possible to use generated libs on other computers (of course you can't use .dll on linux or .so on windows). The only issue may be hardware dependent compilation of the lib, but default compiler flags do not do it.

I.M.

POSTED BY: Ivan Morozov
Posted 11 years ago

Is it possible save the compiled function so that I can use it later without having to recompile it again?

What if the function is compiled to C and I wanted to run it in another computer?

POSTED BY: Miguel Olivo-V

There's an example of a Module inside Compile at

https://reference.wolfram.com/language/Compile/tutorial/Introduction.html#675793002

You generally need to read the tutorials, not just the function documentation.

POSTED BY: Frank Kampas
Posted 11 years ago

Thank you both. I definitely need to read more about compilation.

POSTED BY: Miguel Olivo-V

Hi,

If I do that, it keeps evaluating the cell forever...

Same for me. Miguel, try this:

<< CompiledFunctionTools` ;
P = \[CapitalPi][X];
f2 = Compile[{{m, _Real, 1}}, Log[Exp[P + \[Beta] F1.m] + Exp[\[Beta] F0.m]]]
f3 = Compile[{{m, _Real, 1}}, Log[Exp[P + \[Beta] F1.m] + Exp[\[Beta] F0.m]], CompilationOptions -> {"InlineExternalDefinitions" -> True}]
StringFreeQ[CompilePrint[f2], "MainEvaluate"]
StringFreeQ[CompilePrint[f3], "MainEvaluate"]

Also, take a look at http://mathematica.stackexchange.com/questions/1803/how-to-compile-effectively

I.M.

POSTED BY: Ivan Morozov

You should put the entire function definition inside Compile. You can do that with a Module.

POSTED BY: Frank Kampas
Posted 11 years ago

If I do that, it keeps evaluating the cell forever...

POSTED BY: Miguel Olivo-V

You have functions in your Compile statement which are defined outside it. At the least, you need to wrap the argument of Compile with Evaluate since Compile is HoldAll.

POSTED BY: Frank Kampas
Posted 11 years ago

Thank you, I didn't notice that in the documentation when I was reading it.

However, I get inconsistent results when using the function. See attached notebook. Basically, it gives me an error the first few times that I try to use the compiled function, but after a few calls, it behaves as expected. What could be happening?

Attachments:
POSTED BY: Miguel Olivo-V
f = Compile[{{m, _Real, 2}, {v, _Real, 1}}, m.v]
POSTED BY: Frank Kampas
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