({{, (RefLink[KernelFunction, paclet:ref/KernelFunction])[f] 
represents a function to be evaluated in the Wolfram Engine kernel, even when it is called inside compiled code.}})

OpenersNotesSection(Spacer1)Details

The function f in (RefLink[KernelFunction, paclet:ref/KernelFunction])[f] can be a built-in Wolfram Language function, a (RefLink[Function, paclet:ref/Function]) object or a function with definitions given by the user.

OpenersPrimaryExamplesSection(Spacer1)Examples  (6)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Use the Wolfram Engine evaluator to compute the (RefLink[AiryAi, paclet:ref/AiryAi]) function from compiled code:

cf=FunctionCompile[Function[Typed[arg,"Real64"],Typed[KernelFunction[AiryAi],{"Real64"}->"Real64"][arg]]]

Execute the compiled function:

cf[1.1]

The result compares with the direct call:

AiryAi[1.1]

Call a pure function that executes with the evaluator:

cf=FunctionCompile[Function[Typed[arg,"Real64"],Typed[KernelFunction[#+10&],{"Real64"}->"Real64"][arg]]]

cf[ 10.2]

Define a function in the Wolfram Engine:

f[x_]:=x^2

Call the function from compiled code:

cf=FunctionCompile[Function[Typed[arg,"MachineInteger"],Typed[KernelFunction[f],{"MachineInteger"}->"MachineInteger"][arg]]]

cf[10]

OpenersExampleSection1(Spacer1)Scope  (4)

Use the result of (RefLink[KernelFunction, paclet:ref/KernelFunction]) as an internal function:

cf=FunctionCompile[Function[{Typed[cond,"Boolean"], Typed[arg,"Real64"]},Module[{f1=Typed[KernelFunction[#+10&],{"Real64"}->"Real64"],f2=Typed[KernelFunction[#+10&],{"Real64"}->"Real64"]},
            If[cond, f1[arg],f2[arg]]]]]

cf[ True,10.2]

A (RefLink[KernelFunction, paclet:ref/KernelFunction]) can return a function:

cf=FunctionCompile[Function[Typed[arg,"Real64"],Module[{f1=Typed[KernelFunction[Function[{},#+10&]],{}->{"Real64"}->"Real64"][]},
	   f1[arg]]]]

cf[ 10.2]

Returning a function from (RefLink[KernelFunction, paclet:ref/KernelFunction]) allows operations such as (RefLink[Fit, paclet:ref/Fit]) to be integrated into the compiler:

func=Function[{Typed[data,TypeSpecifier["PackedArray"]["Real64",2]],Typed[arg,"Real64"]},
		Module[{f1=Typed[
		KernelFunction[
			Function@@{x,Fit[#, {1,x},x]}&],
			  {TypeSpecifier["PackedArray"]["Real64",2]}->{"Real64"}->"Real64"][data]},
           f1[ arg]]];
cf=FunctionCompile[func]

cf[ {{0.,1.},{1.,0.},{3.,2.},{5.,4.}}, 1.2]

The function returned from (RefLink[KernelFunction, paclet:ref/KernelFunction]) can itself be dynamically compiled:

func=Function[{Typed[data,TypeSpecifier["PackedArray"]["Real64",2]],Typed[arg,"Real64"]},
		Module[{f1=Typed[
		KernelFunction[
			FunctionCompile[Function@@{Typed[x,"Real64"],Fit[#, {1,x},x]}]&],
			  {TypeSpecifier["PackedArray"]["Real64",2]}->{"Real64"}->"Real64"][data]},
           f1[ arg]]];
cf=FunctionCompile[func]

cf[ {{0.,1.},{1.,0.},{3.,2.},{5.,4.}}, 1.2]

Use (RefLink[KernelFunction, paclet:ref/KernelFunction]) to integrate file operations:

path=CreateTemporary[];
Put[RandomReal[100,100], path];

cf=FunctionCompile[Function[{},
Module[{data},
data=Typed[KernelFunction[Get[path]&],{}->"PackedArray"["Real64",1]][];
Fold[Plus, data]
]]]

cf[]

OpenersExampleSection3(Spacer1)Neat Examples  (1)

Integrate (RefLink[NDSolve, paclet:ref/NDSolve]) into compiled code:

func=Function[{Typed[arg1,"Real64"],Typed[arg2,"Real64"]},
	Module[{f=Typed[
KernelFunction[Function[{init},First[y/.NDSolve[{y'[x]==y[x] Cos[x+y[x]],y[0]==init},y,{x,0,30}]]]],{"Real64"}->{"Real64"}->"Real64"][arg1]},
f[arg2]]];
cf=FunctionCompile[func]

cf[0.2,0.0]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[FunctionCompile, paclet:ref/FunctionCompile])  ▪  (RefLink[CompiledCodeFunction, paclet:ref/CompiledCodeFunction])  ▪  (RefLink[Typed, paclet:ref/Typed])  ▪  (RefLink[ExternalFunction, paclet:ref/ExternalFunction])  ▪  (RefLink[CloudFunction, paclet:ref/CloudFunction])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Code Compilation, paclet:guide/CodeCompilation])}})}})