Scientific Computing

CTest parallel run by default

CMake environment variable CTEST_PARALLEL_LEVEL controls default test parallellism to save test run wallclock time. CTEST_PARALLEL_LEVEL=0 uses unbounded test parallelism. If the computer runs out of memory or has conflicts with parallel tests, use fixtures and resource locks to control test run parallelism on a per-test basis.

CTest parallel somewhat randomizes the order of the tests. ctest –schedule-random randomizes the order of tests even for serial test runs.

This example run on a 4-core machine shows that no extra command line parameters are needed to use CTEST_PARALLEL_LEVEL:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)

project(par LANGUAGES NONE)

enable_testing()

message(STATUS "ENV{CTEST_PARALLEL_LEVEL}: $ENV{CTEST_PARALLEL_LEVEL}")

foreach(t RANGE 8)
  add_test(NAME sleep${t} COMMAND ${CMAKE_COMMAND} -E sleep 1)
endforeach()
export CTEST_PARALLEL_LEVEL=0

ctest --test-dir build

or

ctest --test-dir build --parallel
Internal ctest changing into directory: C:/temp/build
Test project C:/temp/build
    Start 2: sleep1
    Start 5: sleep4
    Start 3: sleep2
    Start 7: sleep6
1/8 Test #5: sleep4 ...........................   Passed    1.05 sec
    Start 6: sleep5
2/8 Test #7: sleep6 ...........................   Passed    1.04 sec
    Start 1: sleep0
3/8 Test #2: sleep1 ...........................   Passed    1.06 sec
    Start 8: sleep7
4/8 Test #3: sleep2 ...........................   Passed    1.06 sec
    Start 4: sleep3
5/8 Test #1: sleep0 ...........................   Passed    1.06 sec
6/8 Test #6: sleep5 ...........................   Passed    1.06 sec
7/8 Test #8: sleep7 ...........................   Passed    1.05 sec
8/8 Test #4: sleep3 ...........................   Passed    1.05 sec

100% tests passed, 0 tests failed out of 8

Total Test time (real) =   2.14 sec

X11 on macOS

X11 can be accessed using XQuartz as available via Homebrew.

brew install libx11 xquartz

To finish the one-time setup of XQuartz, logout/login or reboot. Check that X11 server is available by

echo $DISPLAY

which should show a temporary directory ending like “org.xquartz:0”.

Demo and troubleshooting

See that X11 is working with the classic Xeyes test GUI.

xeyes

A useful image browsing program on Linux, macOS etc. is feh:

brew install feh

Troubleshooting: XQuartz GitHub Issues

Specifically, be sure XQuartz is enabled in Login Items to fix problems like the XQuartz window won’t open.

XQuartz will open automatically when any X11 program is started.


Related: PulseAudio on macOS

macOS power-off USB sleep

By default, macOS keeps USB ports constantly providing power while in sleep mode, which can run down the laptop battery if a USB device such as a docking station is plugged in without a power adapter. pmset macOS command utility can set USB ports to power-off when the lid is closed. This works by putting the laptop into hibernate mode, which makes the laptop wake up a few seconds slower than the standard sleep.

Get current power settings:

pmset -g

Note the “hibernatemode” value.

  • 3: sleep mode with power on to USB ports.
  • 25: DeepSleep hibernate, which powers off USB ports.
pmset -a hibernatemode 25

Test this by pushing the power button or closing the laptop lid. Wait a minute to see if the USB device loses power. A USB power analyzer (volt meter) can be used to check the USB port power as well.

Cleanup unused files in Linux

Keep at least 10% of drive space to avoid:

  • SSD wear
  • HDD fragmentation

Determine free space on Linux / macOS / Windows Subsystem for Linux with “ncdu”. ncdu uses Ncurses terminal graphics to quickly show the biggest files in the Linux filesystem tree. ncdu is very handy to find large files or directories that may be unneeded.

df -h

gives a drive-level summary of disk usage.

Package managers cache installed files in case of need to reinstall, but the packages can be redownloaded if needed to save disk space by clearing the cache. Clear the package cache–for APT (common in Debian-based systems):

apt autoclean

or for DNF (Fedora, RHEL, CentOS):

dnf clean dbcache

Remove unwanted packages

TeX Live documentation can consume a lot of disk space. To cleanup the documentation, consider removing packages matching texlive-*doc. This also removes texlive-full but with no detriment to TeX Live working.

Synaptic list of files to remove for texlive-doc to save disk space

Packages removed for texlive-doc to save over 1 GB of disk space.


Related:

CMake FindOpenSSL hints

For all CMake find_*() commands including FindOpenSSL, the package path can be hinted by setting an appropriate environment variable or CMake variable. This examples supposes a Homebrew package manager has installed OpenSSL 1.1, which the user wishes to use in a CMake project. To hint the package path when configuring a CMake project, either specify OpenSSL_ROOT by environment variable:

export OpenSSL_ROOT=$(brew --prefix openssl@1.1)

or directly in the CMake configure command:

cmake -B build -DOpenSSL_ROOT=$(brew --prefix openssl@1.1)

The example CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)

project(f LANGUAGES NONE)

find_package(OpenSSL REQUIRED)

Use the –debug-find CMake option to see the paths CMake is searching.

To disable various search paths, consider the following CMake variables. These are normally only used for debugging or special cases.

set(CMAKE_FIND_USE_CMAKE_PATH false)
set(CMAKE_FIND_USE_CMAKE_SYSTEM_PATH false)
set(CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH false)
set(CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH false)

The OpenSSL world is gradually transitioning from OpenSSL 1.1 to 3, and Homebrew uses subdirectory to isolate the OpenSSL installs. CMake does not recursively search as that would in general not have a stopping condition and at least significantly slow down the search performance.

GitHub Actions Apple Silicon CPU

GitHub Actions macOS runners can use Apple Silicon CPU, which is what most Apple users have. Some build issues including the linker have historically had Apple Silicon-specific issues. Generally it’s good to test on the same CPU architecture as the target platform.

We sometimes find it necessary to select the Xcode version compatible with Homebrew GCC if build errors occur that are not present on a physical Apple Silicon laptop.

jobs:

  mac:
    runs-on: macos-14

    strategy:
      matrix:
        cxx: [g++-13, clang++]

    env:
      HOMEBREW_NO_AUTO_CLEANUP: 1
      CXX: ${{ matrix.cxx }}

    steps:
    - uses: actions/checkout

    - name: Ninja install
      run: brew install ninja

    - run: sudo xcode-select --switch /Applications/Xcode_15.1.app

    - run: cmake --workflow --preset debug

    - run: cmake --workflow --preset release

In this example Ninja enables quick testing of builds in Debug and Release mode, which is important to catch bugs.

macOS WiFi BSSID scan

The undocumented, discontinued macOS command-line utility airport– not to be confused with the Airport Utility app–gave detailed information about the current WiFi connection and nearby WiFi APs. This utility was located at /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport.

Since discontining airport, current BSSID requires using CoreWLAN framework as demonstrated in Python scan-wifi-python.

Apple provides a list of device WiFi support.

Matlab on macOS doesn't source ~/.zshrc

On macOS, Matlab does not source ~/.zshrc. This issue has existed at least since macOS started using ZSH as the default shell.

To workaround this issue, particularly when programs from package managers like Homebrew are needed, add a setup script to the Matlab project containing like:

if ~ismac
  return
end

% Add Homebrew to the PATH
[ret, homebrew_prefix] = system('brew --prefix');
if ret == 0
  p = fullfile(strip(homebrew_prefix), "bin");
  if isfolder(p)
    setenv('PATH', append(p, pathsep, getenv('PATH')))
  end
end

Then programs installed by Homebrew like CMake, GCC, etc. will be on Path environment variable in Matlab.

Note that the Matlab commands below do not help:

!source ~/.zshrc

system("source ~/.zshrc")

PowerShell tilde expansion

PowerShell tilde expansion was dropped in 7.4.0. Automatic variable $home remains available across operating systems.

ls $home

PowerShell tilde expansion was fraught with difficulties that led PowerShell maintainers to at least temporarily drop tilde expansion in PowerShell 7.4.0.

Note that automatic variables are just inside PowerShell itself–they are not environment variables. Thus, automatic PowerShell variables are generally not visible to other programs or scripts unless additional steps are taken to expose them, perhaps as a command line argument or environment variable.