For one of my programs I need to preallocate a multidimensional list/array. For performance reasons I want to do this within Compile
and compile my program to C-code.
Now I made the following observation for the following minimal working example:
cf = Compile[{}, Block[{list},
list = Table[{0., 1.}, {1}, {1}];
list[[1, 1, 2]]], CompilationTarget :> "MVM"];
with CompilationTarget :> "MVM"
this function works just fine. When I call cf[]
I obtain the expected result 1.
. Now if I change CompilationTarget->"C"
, I receive the following error message when I define the function cf
in MMA
CCompilerDriver`CreateLibrary::cmperr: Compile error: C:\Users\AppData\Roaming\Mathematica\ApplicationData\CCompilerDriver\BuildFolder\compiledFunction100.c(152) : error C2440: 'function' : cannot convert from 'mreal' to 'MTensor *' >>
Compile::nogen: A library could not be generated from the compiled function. >>
Note that for both CompilationTarget
s CompilePrint
shows that the code just compiles nicely.
If I keep the code to be compiled to C
but change the dimensionality of the list
the error message disappears
(*Compiled function*)
cf = Compile[{}, Block[{list},
list = Table[{0., 1.}, {1}];
list[[1, 1]]], CompilationTarget :> "C"];
Any clue how this behaviour comes about? I guess the C-compiler cannot handle these kind of Table
s where I give a List
of rank 1 as a first argument and only accepts arguments of rank 0. However, since the error message only appears when I define the function but not when I call it, is it safe to use anyway? (since CompilePrint
shows no call to MainEvaluate
).
cf= With[{list =Table[{0.,1.},{1},{1}]}, Compile[{}, list[[1,1,2]],CompilationTarget:>"C"]]
$\endgroup$ – Rolf Mertig Nov 23 '18 at 10:03list
, i.e. when going down to the single element e.g. bylist[[1,1,2]]
. Withlist[[1,1]]
everything works just fine I agree. $\endgroup$ – Display Name Nov 23 '18 at 14:07cf[]
:cf = Compile[{}, Block[{list}, list = Table[{0, 1}, {1}, {1}]; list[[1, 1, 2]]], CompilationTarget :> "C"]
$\endgroup$ – Michael E2 Nov 23 '18 at 14:12