I want to speed up evaluation time of a long expression (a function of a single parameter t
). To do so, I thought about using Compile
but I then run into warnings about numerical errors (although the function itself is absolutely fine). Here is an example for an expression in question:
expr=Uncompress["1:eJztnL9KA0EQh0+TiGVAQS3t7W0TrASLGIRgIygkuBA9zEXsLOzSaeVb+AJWgmhhYaE+hGBhZyPE2ySYZG8vGwNr7s93xcElt3OTuZ3vNw6DqwduuZZ1HMeTpy3hNWuz8irnn3bEUdXrXs77p/J+U7jH+3Ux418I+XH3O7mwVD/1zAvb/jGwUN5acs+qjb4D0k7D6R1mg/JCZAKeNB4q9+7VzWGhlhmyIC6Dz/bkZUlo3ZI/1JMmNlxPMaVxRn4i5A09k55c3uwazpoWf8rQZII+DPgVfAed+zNDv/x87Xru4vmloMSusdtev937Wira+4lKqJ3t0FBnbYVaZ3isrdUJv7oy2ltq4OXrnIms38p+fX9defw4eQrs19bb3fJmazF9+3UUUsW3Zpvq8n+hqM//yj/GM5+bSjx1EFVi5PQOIAAElE07JQiMVPmQ79q/FYPxye0/Fhdh9yOmEfObPEJMbcYTMQUCsYPAJGLa/3PauM4otLqXPfSKzHaU3Ivu1jP2NfKOniL0NehraOMNhZO5XynFKMWAQMogYLmvoVRtxuKCvgZ5FMs8QkwRUyCQcghMsa8x8cSH5hlMfDDxQWcEjqeW4xRzFHNAIOUQYOKDPCKPEFPEFAgAgfh2Rpj4UBcz8WFJ2ijFouU3FKYUsxlPSjEgEDsIMPFBHpFHiCliCgSAQHz7GhNPfPA/PkIXM/FhSRwp5qLlNxynmLMZT4o5IBA7CDDxQR6RR4gpYgoEgEBiOiMhOWO2w8QHfQ36GlA4QfuVUoxSDAikDAJMfJBH5BFiipgCASCQmL7G+I2M0IYIEx9MfFgSR4q5aPkNxynmbMaTYg4IxA4CTHyQR+QRYoqYAgEgkJjOSEjOmO0w8UFfg74GFE7QfqUUoxQDAimDABMf5BF5hJgipkAACES1r/EDoJXfZA=="];
compiledExpr = With[{e = expr}, Compile[{{t, _Real}}, Re[e], RuntimeOptions -> "Quality"]];
compiledExpr[2.]
CompiledFunction::cfn: Numerical error encountered at instruction 29; proceeding with uncompiled evaluation.
(* {-19.6371, -13.3496, 13.3496, 19.6371} *)
If one then looks at
Needs["CompiledFunctionTools`"];
CompilePrint[compiledExpr]
it is possible to observe the instruction in question. However, I don't know how to extract the relevant information about all those common expressions inside the CompiledFunction
that would allow me to debug what is going on without writing and tracing all instructions one after another by hand. Perhaps it is also just sufficient to provide other options to Compile
that already solve the problem.
There are somehow related questions (61596), (13689) but they seem to both tackle an issue with infinite expressions during evaluation, which for me should not be the case (the erroneous instruction in my case is a Sqrt
). Most relevant appears to be (92440) but in order to make an attempt to use the conclusion there, I need to figure out how to better understand what happens inside my CompiledFuntion.
I wonder
- how to debug such situations generally to figure out what exactly is going wrong and
- how to fix this specific error
- (optional) if there is any other means than
Compile
which enables faster evaluation ofexpr
for givent
and would really appreciate assistance.
CompilationOptions -> {"ExpressionOptimization" -> False}
seems to remove the warnings. $\endgroup$CompiledFunction
one after another $\endgroup$