Skip to content

Backend C

Fred Rothganger edited this page Jun 3, 2024 · 9 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 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
    • Option 2c: Direct build of MinGW-w64
      • As of 6/20/2022, the most recent available build is GCC 8. This is too out-of-date to use. Please avoid this option until a more current version is built.
      • Download the installer. This little utility helps you pick an appropriate version for your system.
      • When you run the installer, pick pthreads and SEH.
      • All the installer does is grab the right zip file. Expand it in your Program Files directory.
      • Add the directory where g++.exe resides to your system path.

Metadata tags

These tags appear under "backend.c":

  • cli -- Flag to activate or suppress 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. Only variables that also have the tag "param" are enabled. However, setting cli on an individual variable overrides this. CLI must be enabled on the top-level model for the feature to operate. It can then be enabled/disabled in individual sub-parts and variables.
  • debug -- Builds code in debug mode rather than release mode.
  • 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.

For tuning fixed-point code:

median=value -- Gives the median (not mean!) value. 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