4
$\begingroup$

I am using OptionsPattern and OptionValue to define some switches for my custom function. Here is a minimal example:

t[vars_, expr_, 
  OptionsPattern[{ Experimental`OptimizeExpression, 
    Compile}]] :=
 (
  Compile[vars, expr, 
   CompilationOptions -> OptionValue[CompilationOptions],
   CompilationTarget -> OptionValue[CompilationTarget],
   Parallelization -> OptionValue[Parallelization],
   RuntimeAttributes -> OptionValue[RuntimeAttributes],
   RuntimeOptions -> OptionValue[RuntimeOptions]
   ]
  )
t[{x, y}, Sin[x*y]]

This produces the errors:

"Options expected (instead of \
OptionValue[{Experimental`OptimizeExpression,Compile},{},\
CompilationOptions]) beyond position 2 in \
Method->{OptionValue[{Experimental`OptimizeExpression,Compile},{},\
CompilationOptions]}. An option must be a rule or a list of rules."
"Options expected (instead of \
OptionValue[{Experimental`OptimizeExpression,Compile},{},\
RuntimeOptions]) beyond position 2 in \
Method->{OptionValue[{Experimental`OptimizeExpression,Compile},{},\
RuntimeOptions]}. An option must be a rule or a list of rules"
"OptionValue[{Experimental`OptimizeExpression,Compile},{},\
RuntimeAttributes] is not a known runtime attribute and will be \
ignored"

and fails to execute with default values passed to the options. OptionValue appears to fail to grab the correct value. (Moreover, Method appears to be being assigned a value even though Compile has no such option, but this could just be internal definitions to Compile)

Assigning explicitly something like

t[{x, y}, Sin[x*y], 
 CompilationOptions -> {"ExpressionOptimization" -> True}]

produces similar errors. Why isn't the correct OptionValues being parsed?

$\endgroup$

1 Answer 1

6
$\begingroup$

Compile holds its arguments, and doesn't recognize the unevaluated Rule[_, _OptionValue] as an option. Use With to inject the options into Compile:

t[vars_, expr_, OptionsPattern[{Experimental`OptimizeExpression, Compile}]] := With[
    {
        opts = {
            CompilationOptions -> OptionValue[CompilationOptions],
            CompilationTarget -> OptionValue[CompilationTarget],
            Parallelization -> OptionValue[Parallelization],
            RuntimeAttributes -> OptionValue[RuntimeAttributes],
            RuntimeOptions -> OptionValue[RuntimeOptions]
        }
    },
    Compile[vars, expr, opts]
]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.