Reported to WRI [CASE:3406803]
Why does generating an array in "C"-compiled function uses 50% more memory than doing same thing in "WVM"-compiled function?
Does this depend on OS or used C compiler?
Simple example:
ClearAll[zeroArrayWVM, zeroArrayC]
zeroArrayWVM =
Compile[{{i, _Integer}}, Table[0, {i}], CompilationTarget -> "WVM"];
zeroArrayC =
Compile[{{i, _Integer}}, Table[0, {i}], CompilationTarget -> "C"];
TableForm[
Table[
{#1, #2, N[#2/#1]} &[
zeroArrayWVM[i] // MaxMemoryUsed,
zeroArrayC[i] // MaxMemoryUsed
],
{i, 10^Range[8]}
],
TableHeadings -> {None, {"WVM", "C", "C/WVM"}}
]
\begin{array}{lll} \text{WVM} & \text{C} & \text{C/WVM} \\ 320 & 512 & 1.6 \\ 1824 & 2768 & 1.51754 \\ 16544 & 24848 & 1.50193 \\ 160240 & 240392 & 1.5002 \\ 1600240 & 2400392 & 1.50002 \\ 16000240 & 24000392 & 1.5 \\ 160000240 & 240000392 & 1.5 \\ 1600000240 & 2400000392 & 1.5 \\ \end{array}
This results were obtained on Linux x86_64 with gcc 4.8.3, if that is relevant. They are the same in Mathematica versions 9.0, 10.0, 10.1 and 10.2.
Answer
As suggested in comment of Simon Woods and comment of halirutan, returning an array from "WVM"-CompiledFunction and from LibraryFunction creates one additional copy of result, while returning from "C"-CompiledFunction creates two additional copies of result.
We can test it by comparing total memory usage of calling functions generating array of zeros with size of this array. We create "C"-CompiledFunction: zeroArrayC, directly. We get "WVM"-CompiledFunction: zeroArrayWVM, by taking all elements of "C"-CompiledFunction except last one. We get LibraryFunction: zeroArrayCLib by taking last element of "C"-CompiledFunction.
zeroArrayC = Compile[{{i, _Integer}}, Table[0, {i}], CompilationTarget -> "C"];
zeroArrayWVM = Most@zeroArrayC;
zeroArrayCLib = Last@zeroArrayC;
TableForm[
Table[
Prepend[
N[(MaxMemoryUsed@#@i& /@ {zeroArrayWVM, zeroArrayC, zeroArrayCLib})/#],
#
]&@ByteCount@ConstantArray[0, i]
,
{i, 2^(5 Range@5)}
],
TableHeadings -> {
None,
{"ByteCount", "WVM/ByteCount", "C/ByteCount", "CLib/ByteCount"}
}
]
\begin{array}{rlll} \text{ByteCount} & \text{WVM/ByteCount} & \text{C/ByteCount} & \text{CLib/ByteCount} \\ 360 & 1.86667 & 2.88889 & 0.844444 \\ 8304 & 2.23892 & 3.36224 & 2.20713 \\ 262288 & 1.99982 & 2.99985 & 1.96465 \\ 8388752 & 1.99999 & 3. & 1.96874 \\ 268435600 & 2. & 3. & 1.96875 \\ \end{array}
If array is created while evaluating function, but is not returned, then no additional copies of array are created:
zeroArrayFirstC = Compile[{{i, _Integer}}, First@Table[0, {i}], CompilationTarget -> "C"];
zeroArrayFirstWVM = Most@zeroArrayFirstC;
zeroArrayFirstCLib = Last@zeroArrayFirstC;
TableForm[
Table[
Prepend[
N[(MaxMemoryUsed@#@i & /@ {zeroArrayFirstWVM, zeroArrayFirstC, zeroArrayFirstCLib})/#],
#
]&@ByteCount@ConstantArray[0, i]
,
{i, 2^(5 Range@5)}
],
TableHeadings -> {
None,
{"ByteCount", "WVM/ByteCount", "C/ByteCount", "CLib/ByteCount"}
}
]
\begin{array}{rlll} \text{ByteCount} & \text{WVM/ByteCount} & \text{C/ByteCount} & \text{CLib/ByteCount} \\ 360 & 0.866667 & 1.15556 & 0.844444 \\ 8304 & 1.11657 & 1.12909 & 1.11561 \\ 262288 & 0.999817 & 1.00021 & 0.999786 \\ 8388752 & 0.999994 & 1.00001 & 0.999993 \\ 268435600 & 1. & 1. & 1. \\ \end{array}
Comments
Post a Comment