Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wolfram mathematica Mathematica——如何编译BitShiftRight(或Left)?_Wolfram Mathematica_Mathematical Optimization_Mathematica 8 - Fatal编程技术网

Wolfram mathematica Mathematica——如何编译BitShiftRight(或Left)?

Wolfram mathematica Mathematica——如何编译BitShiftRight(或Left)?,wolfram-mathematica,mathematical-optimization,mathematica-8,Wolfram Mathematica,Mathematical Optimization,Mathematica 8,我想编译Mathematica模块,因为我追求速度 testC = Compile[{{inputInt, _Integer}}, Module[{outputInt, bitShift = 5}, outputInt = BitShiftRight[inputInt, bitShift] ] , CompilationTarget -> "C", RuntimeOptions -> "Speed" , Compil

我想编译Mathematica模块,因为我追求速度

    testC = Compile[{{inputInt, _Integer}},
      Module[{outputInt, bitShift = 5},
      outputInt = BitShiftRight[inputInt, bitShift]
      ]
      , CompilationTarget -> "C", RuntimeOptions -> "Speed"
      , CompilationOptions -> {"ExpressionOptimization" -> True, 
       "InlineCompiledFunctions" -> True, 
       "InlineExternalDefinitions" -> True}
    ];
我的实函数较长,但结构非常简单,使用列表,并且只包含以下函数:Total、Table、BitAnd和If。所有的编译和运行时选项在我的实函数中都很有用(可能),而不是这一行提取

我已经准备好了

SetSystemOptions[“CompileOptions”->“CompileReporterExternal”->True]

确保我能看到发生了什么,以及

CompilePrint[testC]

给出以下结果

    1 argument
    3 Integer registers
Underflow checking off
Overflow checking off
Integer overflow checking off
RuntimeAttributes -> {}

    I0 = A1
    I1 = 5
    Result = I2

    1   I2 = MainEvaluate[ Hold[BitShiftRight][ I0, I1]]
    2   Return
正如该线程所预期/担心的那样,BitShiftRight是不可编译的,调用MainEvaluate会严重拖累我的函数。我非常惊讶,这种非常低级的通用函数是不可编译的,而BitAnd、BitNot、BitOr、BitXor是可编译的!
有人知道(快速)解决方法吗?我可以使用MathLink调用C语言函数,但我的目标是在Operate[]中使用此函数,并将其部署为独立的cdf文件。我明白在这种情况下我不能使用MathLink,对吗?顺便问一下,是否有一些易于处理的解决方法?

如果除以32,编译器可能会将其重写为移位。您还应该尝试在Mathematica中直接除以32,确保数据已打包并保持打包状态(Developer`ToPackedArray[])。来回发送数据的开销可能不值得使用C语言进行计算。

只是为了确认(并稍加修改),如果使用商,它将编译sans外部求值器调用。在这个简单的例子中,合适的行是outputInt=Quotient[inputInt,2^bitShift]]。如果你的移位长度是一个固定的常数,那么通电应该在编译时完成(可能你们都知道…。@Daniel:你的解决方案解决了我的pb问题。谢谢!然后编译的代码是
I3=商[I0,I2]
。如果我只是除以2^5,我需要对结果进行四舍五入[]以将其转换为整数。然后编译代码是
R1=receive[R0]
R0=I0
R0=R0*R1
I3=Round[R0]
(越长,效率越低)。