Skip to content

Julia’s Alternative to C-Code Generation for Model-Based Engineering

Jasmine Chokshi
JuliaHub Blog Home Icon Gray

For years, a sort of dogma has prevailed in the world of embedded systems and real-time control: if you're using a high-level language like Python or R for modeling and simulation, you must rewrite your code in C for deployment. But what if there was a better way? What if you could leverage the productivity of high-level languages without sacrificing performance?

Julia is redefining this landscape. As a powerful language for technical computing, it is renowned for its model-based engineering (MBE). And advances in Julia’s compiler technology are eliminating the need for C-code generation, enabling a more seamless and efficient path from modeling to deployment.

The Problem with C-Code Generation

While C-code generation has been a staple of MBE, it's not without its drawbacks:

  • Cost: Code generation tools often come with a hefty price tag.
  • Restrictions: High-level modeling languages frequently impose limitations on expressiveness to ensure compatibility with C-code generation. This can stifle innovation and limit the use of advanced features. Think restrictions on function calls, variable-sized arrays, flexible data structures, and operator overloading.
  • Integration Challenges: Integrating pre-existing libraries or leveraging cutting-edge hardware accelerators can be a nightmare, requiring significant manual effort.

Julia: A New Approach

Julia offers a compelling alternative: direct compilation to architecture-specific binaries via LLVM, bypassing the need for C-code generation altogether. This approach unlocks several key benefits:

  • High-Level Expressiveness: Julia combines the syntax and productivity of a high-level language with performance comparable to low-level languages like C.
  • Rapid Prototyping: Quickly iterate on your designs and models without the overhead of rewriting code in C.
  • Seamless Integration: Leverage Julia's rich ecosystem of libraries for everything from scientific computing to machine learning.
  • Direct Deployment: Compile and deploy your code directly to real-time systems, like embedded devices, without the translation layer of C-code generation.

Key Technical Features Enabling Julia's Success

  • "Trimming" Mode: Julia's compiler includes a "trimming" mode that ensures all emitted code is specialized for high performance. It prunes unreachable code and code associated with the development environment, resulting in smaller, more efficient binaries.
  • Emulation for Cross-Compilation: Julia's compilation workflow can be executed via an emulator like QEMU. This allows you to generate libraries and executables for different target platforms (e.g., compiling on an x86-64 machine for a Raspberry Pi)

Real-World Examples

The power of Julia's direct compilation approach isn't just theoretical. Here are a couple of compelling case studies:

  • Model-Based State Estimation on a Raspberry Pi: A nonlinear state estimator, derived from an equation-based model of a Continuously Stirred Tank Reactor (CSTR) in ModelingToolkit.jl, was compiled and deployed directly to a Raspberry Pi for real-time state estimation. The resulting binary was significantly faster than running the equivalent Julia script.
  • PID Controller Shared Library: A Julia package implementing PID controllers (DiscretePIDs.jl) was compiled into a C-callable shared library. This allows developers to integrate Julia-based PID control logic into existing C-based systems.

Julia's ability to directly compile high-level code into efficient, architecture-specific binaries is a significant step forward for model-based engineering. By bypassing the limitations and complexities of C-code generation, Julia helps engineers develop and deploy complex systems more rapidly and efficiently. It's time to reconsider the old adage and explore the potential of Julia for your next real-time control or embedded systems project.

Read the full research paper:  C-codegen considered unnecessary: go directly to binary, do not pass C. Compilation of Julia code for deployment in model-based engineering



Recent Posts

Learn More

Want to learn more about our capabilities? We are here to help.