Skip to main content
added 425 characters in body
Source Link
Karsten7
  • 27.6k
  • 5
  • 74
  • 135

In general, investigating this features is mostly trial and error, because Compile is a pure kernel function that resists inspection by PrintDefinitions or Trace. Therefore, the only reliable source of information are the one example from the documentation and trying things out. What follows is list of rules extended from my comments.

What can be inlined by "InlineExternalDefinitions" -> True?

In principle, basic own values (see OwnValues) created using Set can potentially be inlined by "InlineExternalDefinitions" -> True.

ext1 = 1;
ext2 = Pi;
ext3 = #;
ext4 = Sin[x];
ext5 = Compile[{x}, Cos[x]];
ext6 = #^2 &;

cf = Compile[{{x, _Real, 0}},
  ext1*ext2 + Nest[ext3 &, x, 10] + ext6[ext4*ext5[x]]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

Needs["CompiledFunctionTools`"];
CompilePrint@cf

Also the DownValues of indexed objects (the DownValues with a single match) can become inlined.

extDV1[1] := 1;
extDV2[1] = 1;
extDV3[y] = x;

cf2 = Compile[{{x, _Real, 0}}, extDV1[1] + extDV2[1] + extDV3[y], 
  CompilationOptions -> {"InlineExternalDefinitions" -> True}]
CompilePrint@cf2

What can't be inlined by "InlineExternalDefinitions" -> True?

Everything else, especially OwnValues created by SetDelayed and DownValues that rely on pattern matching, can't be inlined by "InlineExternalDefinitions" -> True.

ext7 := 1;
cf2cf3 = Compile[{{x, _Real, 0}},
  ext7*x
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf2CompilePrint@cf3


ext8[x_] := x;
cf3cf4 = Compile[{{x, _Real, 0}},
  ext8[x]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf3CompilePrint@cf4

What will be inlined?

Which DownValues will be inlined by "InlineExternalDefinitions" -> True is decided by the algorithm of Compile and is therefore hard to predict. It has even changed between version 9 and 10.

Wolfram Technical Support: "According to the developers, nPara is not inlined because Mathematica is making the conservative assumption that nPara might change in the future."

How to force inlining?

In general, investigating this features is mostly trial and error, because Compile is a pure kernel function that resists inspection by PrintDefinitions or Trace. Therefore, the only reliable source of information are the one example from the documentation and trying things out. What follows is list of rules extended from my comments.

What can be inlined by "InlineExternalDefinitions" -> True?

In principle, basic own values (see OwnValues) created using Set can potentially be inlined by "InlineExternalDefinitions" -> True.

ext1 = 1;
ext2 = Pi;
ext3 = #;
ext4 = Sin[x];
ext5 = Compile[{x}, Cos[x]];
ext6 = #^2 &;

cf = Compile[{{x, _Real, 0}},
  ext1*ext2 + Nest[ext3 &, x, 10] + ext6[ext4*ext5[x]]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

Needs["CompiledFunctionTools`"];
CompilePrint@cf

What can't be inlined by "InlineExternalDefinitions" -> True?

Everything else, especially OwnValues created by SetDelayed and DownValues, can't be inlined by "InlineExternalDefinitions" -> True.

ext7 := 1;
cf2 = Compile[{{x, _Real, 0}},
  ext7*x
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf2


ext8[x_] := x;
cf3 = Compile[{{x, _Real, 0}},
  ext8[x]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf3

What will be inlined?

Which DownValues will be inlined by "InlineExternalDefinitions" -> True is decided by the algorithm of Compile and is therefore hard to predict. It has even changed between version 9 and 10.

Wolfram Technical Support: "According to the developers, nPara is not inlined because Mathematica is making the conservative assumption that nPara might change in the future."

How to force inlining?

In general, investigating this features is mostly trial and error, because Compile is a pure kernel function that resists inspection by PrintDefinitions or Trace. Therefore, the only reliable source of information are the one example from the documentation and trying things out. What follows is list of rules extended from my comments.

What can be inlined by "InlineExternalDefinitions" -> True?

In principle, basic own values (see OwnValues) created using Set can potentially be inlined by "InlineExternalDefinitions" -> True.

ext1 = 1;
ext2 = Pi;
ext3 = #;
ext4 = Sin[x];
ext5 = Compile[{x}, Cos[x]];
ext6 = #^2 &;

cf = Compile[{{x, _Real, 0}},
  ext1*ext2 + Nest[ext3 &, x, 10] + ext6[ext4*ext5[x]]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

Needs["CompiledFunctionTools`"];
CompilePrint@cf

Also the DownValues of indexed objects (the DownValues with a single match) can become inlined.

extDV1[1] := 1;
extDV2[1] = 1;
extDV3[y] = x;

cf2 = Compile[{{x, _Real, 0}}, extDV1[1] + extDV2[1] + extDV3[y], 
  CompilationOptions -> {"InlineExternalDefinitions" -> True}]
CompilePrint@cf2

What can't be inlined by "InlineExternalDefinitions" -> True?

Everything else, especially OwnValues created by SetDelayed and DownValues that rely on pattern matching, can't be inlined by "InlineExternalDefinitions" -> True.

ext7 := 1;
cf3 = Compile[{{x, _Real, 0}},
  ext7*x
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf3


ext8[x_] := x;
cf4 = Compile[{{x, _Real, 0}},
  ext8[x]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf4

What will be inlined?

Which DownValues will be inlined by "InlineExternalDefinitions" -> True is decided by the algorithm of Compile and is therefore hard to predict. It has even changed between version 9 and 10.

Wolfram Technical Support: "According to the developers, nPara is not inlined because Mathematica is making the conservative assumption that nPara might change in the future."

How to force inlining?

Bounty Ended with 500 reputation awarded by xzczd
added 517 characters in body
Source Link
Karsten7
  • 27.6k
  • 5
  • 74
  • 135

In general, investigating this features is mostly trial and error, because Compile is a pure kernel function that resists inspection by PrintDefinitions or Trace. Therefore, the only reliable source of information are the one example from the documentation and trying things out. What follows is list of rules extended from my comments.

What can be inlined by "InlineExternalDefinitions" -> True?

In principle, basic own valuevalues (see OwnValues) created by using Set can potentially be inlined by "InlineExternalDefinitions" -> True.

ext1 = 1;
ext2 = Pi;
ext3 = #;
ext4 = Sin[x];
ext5 = Compile[{x}, Cos[x]];
ext6 = #^2 &;

cf = Compile[{{x, _Real, 0}},
  ext1*ext2 + Nest[ext3 &, x, 10] + ext6[ext4*ext5[x]]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

Needs["CompiledFunctionTools`"];
CompilePrint@cf

What can't be inlined by "InlineExternalDefinitions" -> True?

Everything else, especially OwnValues created by SetDelayed and DownValues, can't be inlined by "InlineExternalDefinitions" -> True.

ext7 := 1;
cf2 = Compile[{{x, _Real, 0}},
  ext7*x
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf2


ext8[x_] := x;
cf3 = Compile[{{x, _Real, 0}},
  ext8[x]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf3

What will be inlined?

Which DownValues will be inlined by "InlineExternalDefinitions" -> True is decided by the algorithm of Compile and is therefore hard to predict. It has even changed between version 9 and 10.

Wolfram Technical Support: "According to the developers, nPara is not inlined because Mathematica is making the conservative assumption that nPara might change in the future."

How to force inlining?

In general, investigating this features is mostly trial and error, because Compile is a pure kernel function that resists inspection by PrintDefinitions or Trace. Therefore, the only reliable source of information are the one example from the documentation and trying things out. What follows is list of rules extended from my comments.

What can be inlined by "InlineExternalDefinitions" -> True?

In principle, basic own value (see OwnValues) created by using Set can potentially be inlined by "InlineExternalDefinitions" -> True.

ext1 = 1;
ext2 = Pi;
ext3 = #;
ext4 = Sin[x];
ext5 = Compile[{x}, Cos[x]];
ext6 = #^2 &;

cf = Compile[{{x, _Real, 0}},
  ext1*ext2 + Nest[ext3 &, x, 10] + ext6[ext4*ext5[x]]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

Needs["CompiledFunctionTools`"];
CompilePrint@cf

What can't be inlined by "InlineExternalDefinitions" -> True?

Everything else, especially OwnValues created by SetDelayed and DownValues, can't be inlined by "InlineExternalDefinitions" -> True.

ext7 := 1;
cf2 = Compile[{{x, _Real, 0}},
  ext7*x
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf2


ext8[x_] := x;
cf3 = Compile[{{x, _Real, 0}},
  ext8[x]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf3

What will be inlined?

How to force inlining?

In general, investigating this features is mostly trial and error, because Compile is a pure kernel function that resists inspection by PrintDefinitions or Trace. Therefore, the only reliable source of information are the one example from the documentation and trying things out. What follows is list of rules extended from my comments.

What can be inlined by "InlineExternalDefinitions" -> True?

In principle, basic own values (see OwnValues) created using Set can potentially be inlined by "InlineExternalDefinitions" -> True.

ext1 = 1;
ext2 = Pi;
ext3 = #;
ext4 = Sin[x];
ext5 = Compile[{x}, Cos[x]];
ext6 = #^2 &;

cf = Compile[{{x, _Real, 0}},
  ext1*ext2 + Nest[ext3 &, x, 10] + ext6[ext4*ext5[x]]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

Needs["CompiledFunctionTools`"];
CompilePrint@cf

What can't be inlined by "InlineExternalDefinitions" -> True?

Everything else, especially OwnValues created by SetDelayed and DownValues, can't be inlined by "InlineExternalDefinitions" -> True.

ext7 := 1;
cf2 = Compile[{{x, _Real, 0}},
  ext7*x
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf2


ext8[x_] := x;
cf3 = Compile[{{x, _Real, 0}},
  ext8[x]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf3

What will be inlined?

Which DownValues will be inlined by "InlineExternalDefinitions" -> True is decided by the algorithm of Compile and is therefore hard to predict. It has even changed between version 9 and 10.

Wolfram Technical Support: "According to the developers, nPara is not inlined because Mathematica is making the conservative assumption that nPara might change in the future."

How to force inlining?

I donated a small introduction
Source Link
halirutan
  • 113.4k
  • 7
  • 266
  • 479

In general, investigating this features is mostly trial and error, because Compile is a pure kernel function that resists inspection by PrintDefinitions or Trace. Therefore, the only reliable source of information are the one example from the documentation and trying things out. What follows is list of rules extended from my comments.

What can be inlined by "InlineExternalDefinitions" -> True?

In principle every kind of ownvalue, basic own value (see OwnValues) created by using Set can potentially be inlined by "InlineExternalDefinitions" -> True.

ext1 = 1;
ext2 = Pi;
ext3 = #;
ext4 = Sin[x];
ext5 = Compile[{x}, Cos[x]];
ext6 = #^2 &;

cf = Compile[{{x, _Real, 0}},
  ext1*ext2 + Nest[ext3 &, x, 10] + ext6[ext4*ext5[x]]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

Needs["CompiledFunctionTools`"];
CompilePrint@cf

What can't be inlined by "InlineExternalDefinitions" -> True?

Everything else, includingespecially OwnValues created by SetDelayed and DownValues, can't be inlined by "InlineExternalDefinitions" -> True.

ext7 := 1;
cf2 = Compile[{{x, _Real, 0}},
  ext7*x
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf2


ext8[x_] := x;
cf3 = Compile[{{x, _Real, 0}},
  ext8[x]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf3

What will be inlined?

How to force inlining?

...

What can be inlined by "InlineExternalDefinitions" -> True?

In principle every kind of ownvalue (OwnValues) created by using Set can potentially be inlined by "InlineExternalDefinitions" -> True.

ext1 = 1;
ext2 = Pi;
ext3 = #;
ext4 = Sin[x];
ext5 = Compile[{x}, Cos[x]];
ext6 = #^2 &;

cf = Compile[{{x, _Real, 0}},
  ext1*ext2 + Nest[ext3 &, x, 10] + ext6[ext4*ext5[x]]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

Needs["CompiledFunctionTools`"];
CompilePrint@cf

What can't be inlined by "InlineExternalDefinitions" -> True?

Everything else, including OwnValues created by SetDelayed and DownValues, can't be inlined by "InlineExternalDefinitions" -> True.

ext7 := 1;
cf2 = Compile[{{x, _Real, 0}},
  ext7*x
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf2


ext8[x_] := x;
cf3 = Compile[{{x, _Real, 0}},
  ext8[x]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf3

What will be inlined?

How to force inlining?

In general, investigating this features is mostly trial and error, because Compile is a pure kernel function that resists inspection by PrintDefinitions or Trace. Therefore, the only reliable source of information are the one example from the documentation and trying things out. What follows is list of rules extended from my comments.

What can be inlined by "InlineExternalDefinitions" -> True?

In principle, basic own value (see OwnValues) created by using Set can potentially be inlined by "InlineExternalDefinitions" -> True.

ext1 = 1;
ext2 = Pi;
ext3 = #;
ext4 = Sin[x];
ext5 = Compile[{x}, Cos[x]];
ext6 = #^2 &;

cf = Compile[{{x, _Real, 0}},
  ext1*ext2 + Nest[ext3 &, x, 10] + ext6[ext4*ext5[x]]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

Needs["CompiledFunctionTools`"];
CompilePrint@cf

What can't be inlined by "InlineExternalDefinitions" -> True?

Everything else, especially OwnValues created by SetDelayed and DownValues, can't be inlined by "InlineExternalDefinitions" -> True.

ext7 := 1;
cf2 = Compile[{{x, _Real, 0}},
  ext7*x
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf2


ext8[x_] := x;
cf3 = Compile[{{x, _Real, 0}},
  ext8[x]
  , CompilationOptions -> {"InlineExternalDefinitions" -> True}
  ]

CompilePrint@cf3

What will be inlined?

How to force inlining?

Source Link
Karsten7
  • 27.6k
  • 5
  • 74
  • 135
Loading