## Question on FunctionCompile

10

1

So far, I have mixed feelings about the new compiler in Mathematica 12. Sometimes my results are a little bit faster, sometimes they are considerable slower than those obtained with the old compiler with the option CompilationTarget->"C", and anyway it is much more typing. Now I met a simple example that I cannot get compiled.

With the old compiler in Mathematica the following works excellent:

fuc=Compile[{{lst, _Real, 1}}, Module[{s=0.},
Do[s=s+z, {z, lst}]; s], CompilationTarget->"C"]


It returns a compiled function that is many hundreds times faster than the uncompiled function.

With the new compiler, I expected the following to work:

fun=FunctionCompile @
Function[Typed[lst,TypeSpecifier["PackedArray"]["Real64", 1]],
Module[{s=0.}, Do[s=s+z, {z, lst}]; s]]


The result is a failure with a (for me) not very helpful message: Function NativeCreateIterator: no satisfiable candidate of the type {Integer64, PackedArray[Real64, 1:Integer64], Integer64}->Handle[MIteratorWork[v13092, v13092, v13092, Integer64]] was found in the alternative list <<324>>, and tag TypeError.

I tried to improve my attempt by indicating the type of s and trying to indicate the type of z, without success.

I cannot imagine that the new compiler cannot compile the list form of Do. Any hint on what am I doing wrong and how should it be done is highly appreciated.

2Looks like a I guess it is an error in the internal type coercion. This is not overly surprising as this technology is pretty new. Best course of action would be to tell Wolfram Support about it. For the moment, fun = FunctionCompile@Function[ Typed[lst, TypeSpecifier["PackedArray"]["Real64", 1]], Module[{s = 0.}, Do[s += lst[[i]], {i, 1, Length[lst]}]; s ] ] will do... – Henrik Schumacher – 2019-05-05T15:49:15.183

Still, fuc will beat fun by over an order of magnitude and Total will be another order of magnitude faster. Either I did not get how to use it or FunctionCompile is really no reason to be excited. – Henrik Schumacher – 2019-05-05T15:54:03.573

1

my experience regarding speed is the same. Check out, https://mathematica.stackexchange.com/questions/195435/new-compiler-technology-in-v12

– Asim – 2019-05-05T18:27:31.670

6

The CreateIterator error suggests that only integer iterators are available. (I do not know whether they are the only ones or not; nor do I know whether to consider this a bug. But it is worth reporting and see what WRI says.)

fun = FunctionCompile@
Function[Typed[lst, TypeSpecifier["PackedArray"]["Real64", 1]],
Module[{s = 0.}, Do[s = s + lst[[z]], {z, Length@lst}]; s]]


[I just noticed @Henrik posted the same thing in a comment while I was typing this up.]

3Following your and @Henrik 's suggestion I reported both my observation that the new compiler cannot compile the list form of Do, and the speed difference between fuc and fun to WRI. They filed reports about both points, and remarked that FunctionCompile at the moment is experimental, so it might undergo substantial expansions and improvements in future versions of Mathematica. The old Compile[]` functionality will remain in the Wolfram Language for the foreseeable future. – Fred Simons – 2019-05-07T07:37:03.467

1Thank you, Fred, for reporting! – Henrik Schumacher – 2019-05-17T07:04:24.740