Skip to content

Backend C

Fred Rothganger edited this page Jun 23, 2025 · 23 revisions

This backend generates C++ code. The simulation compiles and runs as a self-contained program. All you need is g++, the GCC compiler for c++. If you are running on Mac or Linux, simply ensure that the compiler package is installed on your system (almost universally the case for Linux).

If you are running on Windows, you can use either the Visual Studio compiler (cl.exe) or a Windows port of g++.

  • Option 1: CL
    • Install Microsoft Visual Studio. Take the Community edition free download. Do not take Visual Studio Code, as this will not provide the c++ compiler. When the installer launches, choose the "Desktop development with C++" workflow.
    • Find the exact location of cl.exe. It will have a path similar to {Visual Studio root}\VC\Tools\MSVC\{version}\bin\Hostx64\x64\cl.exe. The path illustrated here is for the most common case, a 64-bit compiler hosted on a 64-bit system. Copy the full path string from the file browser address bar.
    • In the N2A application, go to Settings > Backend C > localhost
    • Paste the path. Ensure the "cl.exe" appears at the end.
  • Option 2: port of g++
    • Option 2a: MSYS2
      • Follow the installation instructions. This includes using pacman to install mingw64.
      • Add the directory where g++.exe resides to your system path.
        • The directory will be something like: {install dir}/mingw64/bin
        • You can change the system path in Control Panel > System > Advanced settings > Environment Variables button.
        • Add it to PATH in your User variables.
      • Also add {install dir}/usr/bin to the system path after the g++ dir.
      • In the N2A application, the path should be left at the default of "g++".
    • Option 2b: Cygwin
      • Download and run the installer
      • During installation, be sure to pick a gcc-g++ package
      • Setup is similar to MSYS2

Other options may work as well. The ones listed above are cheap and reasonably good. CL tends to compile the fastest and produce the fastest code.

Metadata tags

These tags appear on the top level model under "backend.c":

  • cli -- Flag to enable command-line interface. In CLI mode, extra C code is emitted to read command-line parameters and assign their values to variables in the model. When "cli" is set, any variables that also have the tag "param" are enabled.
  • debug -- Builds code in debug mode rather than release mode.
  • jni -- Tells exporter to include Java Native Interface support code.
  • keepExportJob -- For any kind of C export, don't delete the temporary job directory created to construct the export files. This allows you to view them to get more information or to diagnose problems. In particular, it is possible to view model.cc, where all the generated code goes.
  • profile=methods -- Enables profiling hooks. Value is a comma-separated list. Supported methods are:
    • gprof -- The default if no method is specified.
    • kokkos
  • sharp -- Tells exporter to include wrapper code for C#.
  • tls -- Enable thread-local storage for the simulation object. This allows multiple simulations to run in the same process. Please avoid using this feature, since it is cleaner and more efficient to run a separate process for each simulation.
  • type={int,float,double} -- Indicates basic numeric type for variables. int generates fixed-point code, and involves additional tags.

These tags appear on parts under "backend.c":

  • cli -- Overrides setting of "cli" for this part and all its children. If a child also sets "cli", it further overrides this one.

These tags appear on variables under "backend.c":

  • cli -- Enables/disables command-line processing for this specific variable. Overrides any settings from containing parts. The tag "param" is not needed in this case.
  • vector -- When exporting a shared library, this creates an IO vector associated with the variable. The vector consists of an element for each instance in the population, and allows you to get/set the variable value by index. The exported library exposes the function IOvectorCreate() for retrieving IO vectors by path, which includes part names and indices of each parent leading to the population in question.

For tuning fixed-point code:

  • median=value -- Gives the median (not mean!) value. It is an assertion that half the time the actual value is above this and half the time it is below. This allows the fixed-point engine to select a scaling that minimizes loss of representation at both ends of the range. This can appear as a tag directly under a variable (no backend.c), or as a named parameter in certain functions (pow(), exp(), tan(), input(), matrix()).
Clone this wiki locally