Skip to content

DirectSourceGeneration

Joao Rui Leal edited this page Feb 19, 2022 · 1 revision

Direct source generation is only advised for advanced use cases. For most cases, if you are using Linux, there are utility classes that will automatically perform most steps (see LibGeneration).

Sources for execution

C language

The C source generator aims at producing not only efficient source code but also a source code that leads to faster compilations. This is achieved by reducing the total number of temporary variables, including variable reuse, and having very short variable names.

A generic program used to generate C source code can be written as:

#include <iosfwd>
#include <vector>
#include <cppad/cg.hpp>

using namespace CppAD;
using namespace CppAD::cg;

int main() {
    // use a special object for source code generation
    typedef CG<double> CGD;
    typedef AD<CGD> ADCG;

    /***************************************************************************
     *                               the model
     **************************************************************************/

    // independent variable vector
    CppAD::vector<ADCG> x(2);
    x[0] = 2.;
    x[1] = 3.;
    Independent(x);

    // dependent variable vector 
    CppAD::vector<ADCG> y(1);

    // the model
    ADCG a = x[0] / 1. + x[1] * x[1];
    y[0] = a / 2;

    ADFun<CGD> fun(x, y); // the model tape

    /***************************************************************************
     *                        Generate the C source code
     **************************************************************************/

    /**
     * start the special steps for source code generation for a Jacobian
     */
    CodeHandler<double> handler;

    CppAD::vector<CGD> indVars(2);
    handler.makeVariables(indVars);

    CppAD::vector<CGD> jac = fun.SparseJacobian(indVars);

    LanguageC<double> langC("double");
    LangCDefaultVariableNameGenerator<double> nameGen;

    std::ostringstream code;
    handler.generateCode(code, langC, jac, nameGen);
    std::cout << code.str();
}

The output of this function is:

   y[1] = 0.5 * x[1] + 0.5 * x[1];
   // dependent variables without operations
   y[0] = 0.5;

Make sure to check out the Linux page which is also applicable to macOS.

Sources for documentation

CppADCodegen, in addition to the highly optimized C sources, is also capable of generating documentation oriented sources. These include:

  • LATEX sources, which can be used to compile PDF files,
  • HTML documents with presentation MathML markup and Javascript, and
  • Dot-files to generate a graph of operations.

HTML/MathML/Javascript

CppADCogden can create an HTML page that allows you to navigate your model. Since models can be extremely large (millions of lines of code), this is best suited for small models.

#include <iosfwd>
#include <cppad/cg.hpp>
#include <cppad/cg/lang/mathml/mathml.hpp>

using namespace CppAD;
using namespace CppAD::cg;

int main(void) {
    // use a special object for source code generation
    using CGD = CG<double>;
    using ADCG = AD<CGD>;

    // independent variable vector
    CppAD::vector<ADCG> x(2);
    x[0] = 2.;
    x[1] = 3.;
    Independent(x);

    // dependent variable vector 
    CppAD::vector<ADCG> y(8);

    /***************************************************************************
     *                               the model
     **************************************************************************/
    ADCG a = x[0] / 1. + x[1] * x[1];
    ADCG b = a / 2e-6;
    y[0] = b + 1 / (sign(b)*5 * a);
    y[1] = x[1];
    y[2] = CondExpLt(ADCG(1.0), x[0], x[1], b);
    y[3] = CondExpLe(x[0], ADCG(2.0), x[1], b);
    y[4] = CondExpEq(x[0], x[1], x[1], b);
    ADCG c = CondExpGe(ADCG(3.0), x[0], a, b);
    y[5] = CondExpGt(ADCG(4.0), x[0], ADCG(5.0), c);
    y[6] = 5 * pow(4, x[0]);
    y[7] = 3;

    ADFun<CGD> fun(x, y); // the model tape
    /***************************************************************************
     *                    Generate the HTML/MathML source code
     **************************************************************************/
    CodeHandler<double> handler;

    CppAD::vector<CGD> indVars(2);
    handler.makeVariables(indVars);

    CppAD::vector<CGD> vals = fun.Forward(0, indVars);

    LanguageMathML<double> langMathML;
    LangMathMLDefaultVariableNameGenerator<double> nameGen;

    langMathML.setSaveVariableRelations(true);

    // add some additional code to select variables
    langMathML.setStyle(langMathML.getStyle() + "\n.selectedProp{background-color: #ccc;}"
                                                "\n.faded{\n"
                                                "    opacity: 0.2;\n"
                                                "    filter: alpha(opacity=20); /* For IE8 and earlier */\n"
                                                "}\n"
                                                "\n.faded2{\n"
                                                "    opacity: 0.5;\n"
                                                "    filter: alpha(opacity=50); /* For IE8 and earlier */\n"
                                                "}");

    // use block display
    langMathML.setEquationMarkup("<math display=\"block\" class=\"equation\">", "</math>");

    // use MathJax (and align to the left)
    langMathML.setHeadExtraMarkup("<script type=\"text/x-mathjax-config\">\n"
                                  //"MathJax.Hub.Config({    MMLorHTML: { prefer: { Firefox: \"MML\" } }  });\n" // use this to define a prefered browser renderer
                                  "MathJax.Hub.Config({\n"
                                  "    jax: [\"input/TeX\",\"output/HTML-CSS\"],\n"
                                  "    displayAlign: \"left\"\n"
                                  "});\n"
                                  "</script>\n"
                                  "<script type=\"text/javascript\" src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"></script>");

    std::ifstream jsFile;
    jsFile.open("variableSelection.js");

    std::stringstream strStream;
    strStream << jsFile.rdbuf();

    langMathML.setJavascript(strStream.str());

    // create the HMTL file
    std::ofstream htmlFile;
    htmlFile.open("algorithm.html");

    handler.setReuseVariableIDs(false);
    handler.generateCode(htmlFile, langMathML, vals, nameGen);

    htmlFile.close();
}

This program creates an HTML file for the model that can be used to navigate and inspect your model. The javascript file variableSelection.js is available in the test folder.

Here is a screenshot of the result (the variable x0 was selected):

mathml.png

Latex

CppADCodeGen can generate Latex files that can be used to create PDF files. These PDF files are better suited for larger models than MathML.

Here is an example:

#include <iosfwd>
#include <cppad/cg.hpp>
#include <cppad/cg/lang/latex/latex.hpp>

using namespace CppAD;
using namespace CppAD::cg;

int main(void) {
    // use a special object for source code generation
    using CGD = CG<double>;
    using ADCG = AD<CGD>;

    /***************************************************************************
     *                               the model
     **************************************************************************/

    // independent variable vector
    CppAD::vector<ADCG> x(2);
    x[0] = 2.;
    x[1] = 3.;
    Independent(x);

    // dependent variable vector 
    CppAD::vector<ADCG> y(1);

    // the model
    ADCG a = x[0] / 1. + x[1] * x[1];
    y[0] = a / 2;

    ADFun<CGD> fun(x, y); // the model tape

    /***************************************************************************
     *                       Generate the Latex source code
     **************************************************************************/

    /**
     * start the special steps for source code generation
     */
    CodeHandler<double> handler;

    CppAD::vector<CGD> xv(x.size());
    handler.makeVariables(xv);

    CppAD::vector<CGD> vals = fun.Forward(0, xv);

    LanguageLatex<double> langLatex;
    LangLatexDefaultVariableNameGenerator<double> nameGen;

    std::ofstream texfile;
    texfile.open("algorithm.tex");

    handler.generateCode(texfile, langLatex, vals, nameGen);

    texfile.close();

    /***************************************************************************
     *                               Compile a PDF file
     **************************************************************************/
#ifdef PDFLATEX_COMPILER
    std::string dir = system::getWorkingDirectory();

    system::callExecutable(PDFLATEX_COMPILER, {"-halt-on-error", "-shell-escape",
                                               system::createPath({dir, "resources"}, "latex_template.tex")});
#endif
}

This program expects PDFLATEX_COMPILER to be set to a Latex compiler (e.g., /usr/bin/pdflatex) and a template file (see latex_template.tex) to be provided. It then creates a PDF file.