Scientific Computing

Matlab / Octave integer representation

For proper integer representation in Matlab / Octave use explicit type to avoid Matlab unwanted casting to “double” for integers.

x = int64(2^63);

Operations involving an explicitly-typed variable will retain that type, assuming implicit casting due to other variables or operations doesn’t occur. Precise string representation of “x” can be done using int2str(), sprintf(), or string():

xc = int2str(x);

xf = sprintf('%d', x);

xs = string(x);

sprintf() gives more control over the string output format, while string() or int2str() are more concise.

ATSC 1.0 MPEG-4 older TVs no video

In 2008 the ATSC ratified the MPEG-4 TV broadcast standard. Numerous ATSC 1.0 TVs were sold before this standard was ratified, and still operate today. TV manufacturers continued to make some non-MPEG-4 TVs for a decade after the standard was ratified. The need to lighthouse ATSC 1.0 broadcasts while implementing ATSC 3.0 broadcasts has led numerous broadcasters to use MPEG-4 encoding for their ATSC 1.0 broadcasts.

Previously MPEG-2 was the only widespread encoding standard for ATSC 1.0 broadcasts. Until recently, a typical ATSC 1.0 broadcast channel layout was to have one 1080i channel and several 480i channels, or 1-2 720p channel(s) with several more 480i channels. The number of channels depends on the broadcaster’s choice of resolution and the number of subchannels they want to broadcast vs. the bandwidth per channel. This database lists the channels available in a given area. Click “Technical Data” to see the resolution and encoding of each channel.

As ATSC 3.0 broadcasts roll out, the number of ATSC 1.0 channels will decrease. A mitigation for broadcasters is to switch to MPEG-4 encoding for the ATSC 1.0 broadcasts, which is more efficient than MPEG-2 and allows packing more channels into the same transmitter bandwidth. This leaves older TVs and receivers with audio-only on MPEG-4 channels. This MPEG-4 list is missing some broadcasters.

A solution for the end user lacking an MPEG-4-capable TV is to buy an ATSC receiver box that supports MPEG-4. These can be obtained for less than $50. The ATSC 3.0 receivers are available for less than $100 if desired to access ATSC 3.0 broadcasts not available even on some new TVs.

Enthusiasts make their “band scan” data available for TV and FM radio typically using a Raspberry Pi to enjoy and share the hobby of broadcast DXing.

Satellite radio outside North America

SDARS satellite radio broadcasts of music or video to mobile receivers has largely been a North American phenomenon. While there are North American specific satellite TV networks like DirectTV and Dish Network, satellite TV has long been a global phenomenon in certain markets.

Automobiles typically have Bluetooth audio, which may one day be used with 5G broadcast instead of individual mobile data streams. This may stymie the growth of SDARS in other continents. Despite a global need for wide-area and rural broadcast radio coverage, SDARS is only widespread in North America. SiriusXM has made massive investment in a “long game” with receiver availability just as mobile internet streaming became widely feasible. Other continents’ markets may be too fragmented despite the large population, with not enough intercity user mobility to make the subscriber base big enough, and the cities dense enough to also need hundreds of terrestrial repeaters.

Notes:

Free 2-D CAD drawing programs

AutoCAD 2-D libre alternatives are available for Linux, macOS and Windows. They generally require retraining for users coming from AutoCAD. Libre 2-D AutoCAD-like choices include FreeCAD, QCAD and LibreCAD.

FreeCAD is 3-D parametric modeling akin to SolidWorks that can import DXF or DWG. QCAD has distinct paid vs. Community Edition features include DWG and DXF read / write. LibreCAD can read / write DXF and read DWG.

The no-cost ODA File Converter converts DXF to / from DWG.

Install Nvidia HPC C, C++, Fortran compilers

The free-to-use Nvidia HPC SDK offers possible speed improvements and CUDA Fortran. A typical reason for using Nvidia HPC SDK is the Cuda GPU features. Nvidia HPC compilers support C11, C++23, and partial Fortran 2008 including submodule and error stop.

Download and install Nvidia HPC SDK. Create a script nvidia.sh:

To use NVIDIA HPC SDK, source the script:

source ~/nvidia.sh

CMake

In CMake, set NVIDIA HPC compiler-specific options in CMakeLists.txt like:

if(CMAKE_Fortran_COMPILER_ID STREQUAL "NVHPC")
  add_compile_options($<$<COMPILE_LANGUAGE:Fortran>:-Mdclchk;-Munixlogical>)
endif()

To use newer languages standard features ensure the underlying GCC toolchain is set to a new-enough compiler as per Nvidia HPC SDK documentation. The compiler path can be determined on RHEL-like Linux distros like:

scl enable gcc-toolset-12 "dirname $(which g++)"

If using a CMake toolchain file, instead of CXXFLAGS environment variable, one can set

set(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN "/opt/rh/gcc-toolset-12/root/usr/")

Visual Studio /utf-8 source files

The MSVC compiler needs the /utf-8 flag when UTF-8 strings are in the source file. If not specified, too many bytes are assigned to each character, leading to incorrect string lengths. This will lead to failures with string width conversions such as WideCharToMultiByte.

Windows Intel oneAPI compiler didn’t need the /utf-8 flag when tested.

In CMake, apply the /utf-8 flag like:

add_compile_options($<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/utf-8>)

In Meson, the /utf-8 flag is applied automatically to C++ source files with MSVC compilers.

Matlab upgrade from Terminal

Matlab version upgrade can be initiated from the system Terminal or from the Matlab Help menu. Running the upgrade or Add-On install requires a graphical connection to the computer (Remote Desktop, VNC, or similar).

Matlab release upgrade

The bell icon on the upper right corner of the Matlab Desktop typically shows when a Matlab update is available. To force checking for install, even if the bell is not showing an update, under the Matlab binary directory look for “MathWorksUpdateInstaller”

fullfile(matlabroot, "bin", computer("arch"))

Matlab Add-Ons

Matlab Add-On Explorer is normally launched from the Matlab Desktop. The Add-On Explorer requires a graphical connection. To launch Add-On explorer from the Matlab binary directory look for executable “AddOnInstaller”

Troubleshooting

If the graphical connection has a problem like:

terminating with uncaught exception of type (anonymous namespace)::DisplayError: No display available.

try for diagnosis:

w = matlab.internal.webwindow('www.mathworks.com');

w.bringToFront();

The first command (object instantiation) should not error. The method “bringToFront” should bring up a window if the connection / system allows.


If on Linux with a graphical connection and errors result, try renaming libcrypto.so.1.1 like:

r=$(matlab -batch "disp(fullfile(matlabroot, 'bin', computer('arch')))" | tail -n1)

mv libcrypto.so.1.1 libcrypto.so.1.1.bak

Related: Matlab install on Linux

Datetime vectors in Matlab / Octave

Generating a range of datetime data is a common data analysis and simulation task across programming languages. Matlab and GNU Octave can also generate datetime vectors.

Matlab datetime deprecates datenum. Generate a sequence of datetime like:

t0 = datetime('2020-01-05 12:30:00');
t1 = datetime('2020-01-06 18:15:10');
dt = hours(5.5);

t = t0:dt:t1;

disp(t)

GNU Octave can use many datetime features via the tablicious package.

pkg install -forge tablicious

Load in Octave prompt:

pkg load tablicious

Then use the same Matlab code above.

CMake version recommendations and install

CMake ≥ 3.24 is recommended for general users. Downloading the latest release of CMake is usually easy. Admin / sudo is not required.

  • Linux: snap install cmake
  • macOS: brew install cmake
  • Windows: winget install Kitware.CMake
  • PyPI CMake package: python -m pip install cmake

For platforms where CMake binaries aren’t easily available, build CMake using scripts/build_cmake.cmake.

To see the merge requests for a certain release, use a URL like: https://gitlab.kitware.com/cmake/cmake/-/merge_requests?milestone_title=3.32.0&scope=all&state=all


CMake 3.32 enables CMAKE_LINK_WARNING_AS_ERROR boolean option sets most compilers to error if a compile warning occurs, which is generally a good setting for CI systems. target_link_libraries() can use a LINKER: prefix to abstract options. CTest –interactive-debug-mode is particularly useful on Windows to enable debug dumps and error popup windows.


CMake 3.31 warns if cmake_minimum_required() is < 3.10. TLS ≥ 1.2 is required by default for internet operations e.g. file(DOWNLOAD), ExternalProject, FetchContent, and similar. file(ARCHIVE_CREATE) gained a long-needed WORKING_DIRECTORY parameter that is essentially necessary to avoid machine-specific paths being embedded in the archive. CMAKE_LINK_LIBRARIES_STRATEGY allows specifying a strategy for ordering target direct link dependencies.


CMake 3.30 adds C++26 support. CMAKE_TLS_VERIFY environment variable was added to set TLS verification (true, false). CMake 3.31 defaults CMAKE_TLS_VERIFY to on, where previously it was off.


CMake 3.29 adds cmake_language(EXIT code) to exit CMake script mode with a specific return code. This is useful when using CMake as a platform-agnostic scripting language instead of shell script.

Environment variable CMAKE_INSTALL_PREFIX is used to set the default install prefix across projects–it can be overridden as typical by cmake -DCMAKE_INSTALL_PREFIX= option.

Target property TEST_LAUNCHER allows specifying a test launcher. For MPI program this allows deduplicating or making more programmatic test runner scripts.

Linker information variables including CMAKE__COMPILER_LINKER_ID have been added to allow programmatic logic like setting target_link_options() based on the particular linker.

ctest --parallel without a number or 0 will use unbounded test run parallelism.


CMake 3.28 changes PATH behavior for Windows find_{library,path,file}() to no longer search PATH. This may break some projects that rely on PATH for finding libraries. The MSYS2-distributed CMake is patched to include PATH like earlier CMake, which can be confusing for CI etc. not using MSYS CMake with that patch. Windows CI/user may need to specify CMAKE_PREFIX_PATH like

cmake -DCMAKE_PREFIX_PATH=$Env:SYSTEMDRIVE/msys64/ucrt64/lib -B build

Support for C++20 modules is considerably improved and most users will want at least CMake 3.28 to make C++ modules usable. Generator expressions $<IF> $<AND> $<OR> now short circuit. Test properties now have a DIRECTORY parameter, useful for setting test parameters from the project’s top level CMakeLists.txt. CMake 3.28.4 fixed a long-standing bug in Ninja Fortran targets that use include statements.


CMake 3.27 emits warning for cmake_minimum_required(VERSION) < 3.5. CTest test properties TIMEOUT_SIGNAL_NAME and TIMEOUT_SIGNAL_GRACE_PERIOD specify a POSIX signal to send to a timed out test process. Interactive CMake debugger added by cmake --debugger is used with an IDE such as Visual Studio. CMake script command cmake_file_api() allows querying CMake File API from within CMake. NOTE: Fortran + Ninja was broken for OBJECT libraries in CMake 3.27.0..3.27.8 and fixed in 3.27.9.


Older CMake changelog

No strict aliasing C / C++

Optimizing compilers may enable strict aliasing. For a wide variety of existing projects, strict aliasing provides additional optimization. For some projects, such as MUMPS, memory leaks have been observed that are resolved by disabling strict aliasing using GCC flag “-fno-strict-aliasing”.

Compilers with ability to switch on / off strict aliasing include:

libuv recommends -fno-strict-aliasing due to type punning.

References: