Scientific Computing

CTCSS / DCS coded squelch selection

In general for narrowband FM two-way PTT radio systems including amateur radio, FRS, GMRS, MURS and CB radio, using CTCSS / DCS is a means to allow a group or subgroup to communicate while not bothering others on a different squelch code. Coded squelch has the side effects of slightly reducing communication range and adding a slight delay to unmuting audio at the beginning of a transmission while the receivers decode the squelch code.

Coded squelch does NOT make communications private, as anyone without coded squelch can hear the audio–the subaudible tone is added to the voice audio, but does not scramble the audio.

Unless there is a specific need, users should generally use CTCSS to avoid unmuting delays and range loss that are noticeably worse with DCS. DCS is for crowded areas where range loss isn’t important, and where the user wants to avoid CTCSS interference from other users. By default use CTCSS unless you are really having a problem with not enough tones to pick from. DCS is a last resort.

Historically, lower frequency CTCSS tones had a longer unmute delay, but modern radios minimize this impact. Avoid tones that are near harmonics of the AC mains frequency in the area (50 Hz or 60 Hz typically).

In particular, CB Radio use among family groups and convoys is dramatically improved by using CTCSS to avoid interference from other users and “skip” on the 27 MHz band from users hundreds or thousands of miles away. CTCSS also avoids nuisance opening of squelch from power lines, computers, etc.

Particular to CB radio, the receiver may have an RF gain control that can be used to reduce the sensitivity of the receiver to avoid interference from strong signals. With CTCSS, one might leave the RF Gain on “auto” or maximum (“off” on Anytone family of radios) and use auto noise squelch “ASQ” to maximum communication range while protected from noise by coded squelch.

Sub-$100 CB radios that have CTCSS include the Radioddity CB-500 and the Radioddity CS-27. Anyone buying CB radio for family, group, or convoy use should strongly consider a CB radio with CTCSS.

Reference: very detailed history of subaudible squelch by Repeater Builder.

Matlab buildfile MEX

Matlab buildtool has become a capable build system for tasks including MEX targets and tests with incremental progress, which avoids the need rerun already completed tasks. A standalone buildtool example illustrates the basic use of Matlab buildtool.

Run the build and test tasks:

buildtool mex

buildtool test

See several additional examples of more advanced Mex and Matlab Engine tasks. Matlab-stdlib is another substantial example of buildfile.m usage.

Quote CMake JSON arguments

The CMake string(JSON) subcommands should have the “json-string” input variable quoted to avoid CMake interpreting any semicolon in the JSON string as a list separator. This avoids CMake string(JSON ...) failures when the JSON string contains semicolons.

string sub-command JSON failed parsing json string:
  Syntax error: value, object or array expected.

Example

Suppose a JSON string contains one or more values with semicolons in any value. In that case, the JSON string should be quoted to avoid the CMake string(JSON ...) failure.

set(json_string "{ \"key1\": 432, \"key2\": \"I like to write; my blog is about tech. \" }")

# Fails with a syntax error
string(JSON a GET ${json_string} key1)

# works as expeccted
string(JSON a GET "${json_string}" key1)

Related: CMake JSON array iteration

GitHub API with CMake

CMake can use the GitHub REST API to fetch the latest release download URL from GitHub in the form of JSON data. The API is useful to download the latest release of a GitHub project using CMake. The API used in this example specifically ignore pre-releases and draft releases. The example CMake code shows how to parse the JSON data to get the download URL.

CMake file(DOWNLOAD …) has HTTPHEADER option that can be repeated to add multiple headers to the HTTP request. In GitHubRelease.cmake linked above, a specific version of the GitHub API and format is used to get the latest release download URL. Authentication parameters can also be passed if API limits are exceeded. Don’t save/commit API credentials to a public repository!

file(DOWNLOAD ...
HTTPHEADER "Accept: application/vnd.github+json"
HTTPHEADER "X-GitHub-Api-Version: 2022-11-28"
)

MATLAB GCC version compatibility fix

Matlab requires compatible compilers to run compiled code and even to run itself. Trying to use Matlab on a non-supported just-released OS version can sometimes encounter difficulty. libc, libstdc++, or libgfortran incompatible with Matlab and cause failure to run MEX code. The workaround below assumes Linux.

Example error messages:

MATLAB/R*/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.29’ not found (myfun.mexa64)

Workaround by having the system libraries take priority by using environment variable LD_PRELOAD.

Find the system libstdc++:

find /usr -name libstdc++.so.6

Suppose “/usr/lib64/libstdc++.so.6”.

LD_PRELOAD=/usr/lib64/libstdc++.so.6 matlab

C / C++ include inline code file

In certain cases, such as defining multiple classes or templates in a single header or source file in C or C++, it may be useful to include inline source code files to reduce code duplication. This can be achieved by using the #include directive with a file containing the inline code. This technique is distinct from the use of header files, which are typically used to declare functions, classes, and other entities that are defined in a separate source file. This technique is also distinct from the use of the inline specifier on functions.

A traditional file suffix for include code files is .inc or .inl, but any suffix can be used. Build systems detect changes to these included inline code files and rebuild the source file if necessary. For example, CMake detects include dependencies (header, inline code) based on its own source parser, or some modern compilers manage dependencies themselves.

Makefiles with CMake uses the compiler itself or depend.make in each target build directory to track dependencies.

Ninja (with CMake or other build system such as Meson) specifies include dependencies via depfiles per source file, which may be observed for debugging with option ninja -d keepdepfile

Example

Here we assume a CMake project with source file main.cpp and inline source file myconst.inl.

main.cpp:

#include <iostream>
#include "myconst.inl"

int main() {
    std::cout << "The value of MY_CONST is: " << MY_CONST << "\n";
    return 0;
}

myconst.inl:

constexpr int MY_CONST 42
cmake_minimum_required(VERSION 3.20)

project(InlineDemo LANGUAGES C CXX)

add_executable(demo main.cpp)

Observe that CMake will rebuild main.cpp if myconst.inl changes.

cmake -Bbuild
cmake --build build

touch myconst.inl

cmake --build build

Fortran include statement

The Fortran include statement inserts source code from the specified file into the Fortran source code file at the location of the include statement. The include file can contain any valid Fortran syntax, including procedures, modules, variable definitions, operations, etc. This concept is similar to the C/C++ #include preprocessor directive that can also be used for inlining code, but Fortran include does not require a preprocessor. include statements are frequently used to reuse code like defining constants or Fortran 77 common blocks. Generated code from build systems like CMake and Meson can be consumed with include statements. The file suffix “.inc” is often used, but is arbitrary.

One example of a Fortran-only project extensively using CMake-generated Fortran source with include is h5fortran to allow polymorphic (type and rank) HDF5 I/O in Fortran. The source code deduplication thus achieved is significant and the code is easier to maintain.

Build systems scan Fortran source files for dependencies to detect the include statements and track the included files. Makefiles with CMake uses the compiler itself or depend.make in each target build directory to track dependencies. Ninja (with CMake or other build system such as Meson) specifies include dependencies via depfiles per source file, which may be observed for debugging with option ninja -d keepdepfile

In the example below, the dependency of main.f90 on const.inc is tracked by:

  • Ninja: depfile “build/CMakeFiles/main.dir/main.f90-pp.f90.d”
  • Make: “build/CMakeFiles/main.dir/depend.make”

A minimal example of using the Fortran include statement is shown below.

main.f90:

program main

include 'const.inc'

print '(A, I0)', 'The value of my_const is: ', my_const

end program

const.inc:

integer, parameter :: my_const = 42

CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)

project(include_example LANGUAGES Fortran)

add_executable(main main.f90)

Observe that CMake will rebuild main.f90 if const.inc changes.

cmake -Bbuild
cmake --build build

touch const.inc

cmake --build build

Python libstdc++ conflict LD_PRELOAD

If importing Python modules or trying to run an Anaconda Python program like Spyder gives CXXABI errors, it can be due to a conflict between the system libstdc++ and the Anaconda libstdc++. Assuming Anaconda / Miniconda Python on Linux, try specifying the libstdc++ library in the conda environment by LD_PRELOAD.

Find the system libstdc++:

find /usr -name libstdc++.so.6

Suppose “/usr/lib64/libstdc++.so.6”. Set LD_PRELOAD environment variable in the conda environment:

conda env config vars set LD_PRELOAD=/usr/lib64/libstdc++.so.6

conda activate

Then retry the command / program.

Overview of Free C++ and Fortran Compiler Families

These modern, currently-supported compiler families are free-to-use for C, C++ and Fortran.

Compiler C C++ Fortran
GNU gcc: C23 g++: C++26 gfortran: F2018
Intel oneAPI icx: C23 icpx: C++23 ifx: F2023
LLVM clang: C17 clang++: C++26 flang: F2018
AOCC clang: C17 clang++: C++17 flang: F2008
NVIDIA HPC SDK nvc: C11 nvc++: C++23 nvfortran: F2003
IBM OpenXL xlc: C17 xlc++: C++26 xlf: F2018

GCC

GCC has broad support of modern standards on a very wide range of computing platforms. GCC is competitive in build time and runtime with vendor-specialized compilers. These may offer vendor-specific capabilities not available in general compilers like GCC.

Intel

Intel oneAPI compilers are free to use for any user, supporting Linux and Windows computers with x86-64 CPU including from AMD, Intel, etc. Intel oneAPI components like MKL, IPP, and TBB are available at no cost. Intel MPI Library implements the MPICH specification for massively parallel computation across discrete computing nodes.

LLVM

LLVM Clang C and C++ compilers join with the Flang Fortran compiler using modern C++ internals and robust industry support. LLVM is known for performance, correctness, and prompt implementation of new language standards. LLVM has powerful associated tooling like clang-format, clang-tidy, and sanitizers. It is generally important to ensure that a project builds with both LLVM and GCC for better portability.

AMD

AMD AOCC LLVM compiler is tuned for AMD CPUs. AOCC works on non-AMD CPUs but is generally only useful for those with HPC/AI workloads on AMD CPUs, as it typically uses LLVM releases that are a few versions behind the latest.

AMD GPUs are the focus of the ROCm software stack, which includes the HIP C++ language and ROCm Fortran compiler. Currently the ROCm Fortran Next Gen compiler is in early development and is intended for advanced users who need to use AMD GPUs with Fortran or C/C++ code.

NVIDIA

NVIDIA HPC SDK is free to use and works on a variety of desktop CPUs including x86-64, OpenPOWER, and ARM. A key feature of the NVIDIA compilers is intrinsic support for CUDA Fortran, enabling offloading computationally intensive Fortran code to NVIDIA GPUs. NVIDIA HPC SDK includes specialized tools for NVIDIA Nsight profiling, debugging, and optimizing HPC applications on NVIDIA platforms.

IBM OpenXL

Unlike the other compilers mentioned above, IBM OpenXL LLVM-based compilers are specifically designed for POWER CPUs, such as ppc64le. Consequently, IBM OpenXL compilers do not work with typical x86-based computers.

The IBM OpenXL Fortran compiler features extensive optimization capabilities specifically for POWER CPU architecture. It supports OpenMP for parallel processing and can auto-vectorize code for POWER vector units (VSX, VMX). The compiler includes built-in support for IBM MASS (Mathematical Acceleration Subsystem) libraries and optimization reports to help developers tune code performance. OpenXL compilers include hardware-specific optimizations for POWER CPUs and support for IBM-specific operating systems like AIX.