4
$\begingroup$

I have read the generously comprehensive answers by @leonidshifrin and @xzczd to the question How to compile effectively but more than seven years have now passed, and whilst I am sure much remains the same, it seems unlikely that everything is unchanged; it would be helpful to know the current state of the art - and I also have some questions that were not considered in the previous answers.

Context I wish to generate C code to be compiled and called on a website; webmathematica is not a financial option. I have an existing package containing numerous functions, global variables and constants. The global variables typically contain large intermediate results but also large "initial value" lists that are computationally expensive to create but reusable and so are typically generated only once for many function calls. Functions may use several of these large global variables and restructuring everything to pass and return all necessary variables would be time consuming, error prone and - to me - unnecessary duplication of effort

I have looked at and tested as OK the trivial examples for CCodeGenerate, and the existing package of compiled functions compiles and runs correctly (without calls to MainEvaluate).

Environment Windows 10 64-bit, Mathematica 11.0.1., Visual Studio 2017

The questions are

  1. Is there now any way - direct or by workaround - to include and use global variables and constants in generated code? (Note: for constants there is a workaround: one can always define functions e.g. cMin = Compile[{{x}}, Evaluate[Pi/5. + $MinMachineNumber]] and call with arbitrary x)
  2. If not, how might one approach the idea of "patching in" appropriate definitions etc. to the output code by string manipulation?
  3. What is the practical difference between the output of CCodeGenerate and SymbolicCGenerate?
  4. Once code is generated, how does one actually compile the code (I am using Visual Studio) - particularly, what Mathematica paths must be known to the compiler (e.g. for for headers such as "WolframRTL.h")?
  5. Is there a MWE example of c code generation and demo of calling simple functions from the output of the C compiler?
  6. Are there any other important changes to compilation recommendations since the quoted answer that users should be aware of?
$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.