Aussie AI

Basics of Portable Coding

  • Book Excerpt from "Generative AI in C++"
  • by David Spuler, Ph.D.

Basics of Portable Coding

The basic approach to writing portable code is:

    1. Write generic code portably, and

    2. Write platform-specific code where needed.

Write portable code: Most of your AI C++ application should be written in portable C++. The majority of the C++ programming language is well-standardized, and a lot of code can be written that simply compiles on both, and has the same functionality. You just have to avoid the portability pitfalls.

Platform-specific coding: Most C++ programmers are familiar with using #if or #ifdef preprocessor directives to handle different platforms, and the various flavors of this are discussed further below. The newer C++ equivalent is “if constexpr” statements for compile-time processing. Small or sometimes large sections of C++ code will need to be written differently on each platform. Likely major areas that will be non-portable include:

  • Hardware acceleration (GPU interfaces)
  • Intrinsic functions (CPU acceleration)
  • FP16/BF16 floating-point types
  • User interfaces (Windows vs Mac vs X Windows)
  • Android vs iOS (not just the GUI)
  • Multi-threading (Linux vs Windows threads)
  • Text file differences (You've heard of \r, right?)
  • File system issues (Directory hierarchies, permissions, etc.)
  • “Endian” issues in integer representations.

Consider your code choices carefully. Some other areas where you can create portability pain for yourself include:

  • Third-party libraries (i.e. if not widely used like STL or Boost).
  • Newer C++ standard language features (e.g. C++23 features won't be widely supported yet).

Backend vs GUI Portability. Most of the discussion in this chapter focuses on the portability of C++ coding on the backend, where the AI engine is running. But the user doesn't give a hoot about that stuff, and only cares about their user interface. Which brings us back to iOS versus Android, or Windows versus Mac.

Yeah, I know, you're a professional C++ programmer sitting there with two screens as big as a mammoth's ears. But your users are on these tiny little things that fit in their purse.

Most of the user interface issues are the same for AI applications as they are for non-AI applications. The methods to detect the type of the end user's device are the same in AI programs as they are for all types of programs, so we won't be delving into them here.

 

Next:

Up: Table of Contents

Buy: Generative AI in C++: Coding Transformers and LLMs

Generative AI in C++ The new AI programming book by Aussie AI co-founders:
  • AI coding in C++
  • Transformer engine speedups
  • LLM models
  • Phone and desktop AI
  • Code examples
  • Research citations

Get your copy from Amazon: Generative AI in C++