Scientific Computing

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")

Install AMD AOCC C C++ Fortran compiler

The no-cost AMD AOCC compiler is tuned for AMD CPUs, akin to how Intel oneAPI is tuned for Intel CPUs. Install the “.rpm” file after downloading on a RHEL-based Linux like:

dnf install ./aocc-compiler*.x86_64.rpm

Create a source script “~/aocc.sh” like:

root=/opt/AMD/aocc-compiler-4.2.0/

[[ ! -d ${root} ]] && echo "ERROR: ${root} not found" && exit 1

source ${root}/setenv_AOCC.sh

export CC=clang CXX=clang++ FC=flang

export MPI_ROOT=

flang --version

To use the AOCC compiler:

source ~/aocc.sh

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.

Aspell don't backup

Aspell creates backup files with a .bak extension by default. To turn off the backup files configure Aspell to not create them. Often there is not a not a user configuration file “aspell.conf” present. Even if there is a config file present, it can be overridden by environment variable ASPELL_CONF:

export ASPELL_CONF="dont-backup;"

or do similarly through Control Panel in Windows.

Confirm the setting has taken effect by:

aspell dump config | more

and look for the line backup false.


Related: Aspell dictionary location

Clear temporary scratch files on HPC

Unix-like HPC systems often have shared temporary scratch directories mapped by environment variable $TMPDIR to a directory like “/scratch” or “/tmp”. $TMPDIR may be used for temporary files during build or computation. $TMPDIR is often shared among all users with no expectation of preservation or backup. If user files are left in $TMPDIR, the HPC system may email a periodic alert to the user.

If the user determines that $TMPDIR files aren’t needed after the HPC batch job completes, one can clear $TMPDIR files with a command near the end of the batch job script. Carefully consider whether this is appropriate for the specific use case, as the scratch files will be permanently deleted.

rm -r -i $TMPDIR 2>/dev/null

Verify that deletes only the user’s files, as each user’s files have write permissions only for their own files. Once this is established, to use this command in batch scripts replace the “-i” with “-f” to make it non-interactive.

CMake TARGET_RUNTIME_DLL_DIRS for CTest

Building Windows shared libraries in general creates DLLs whose directory must be on environment variable PATH when the executable target is run. Windows error code -1073741515 corresponding to hex error code 0xc0000135 emits when the necessary DLLs are not in the program’s working directory or on Path environment variable. This will make CTest tests fail with error code 135.

The CMake generator expression TARGET_RUNTIME_DLL_DIRS along with test property ENVIRONMENT_MODIFICATION can be used to set the Path environment variable for the test, gathering all the directories of the DLLs CMake knows the target needs.

set_property(TEST adder PROPERTY ENVIRONMENT_MODIFICATION PATH=path_list_append:$<TARGET_RUNTIME_DLL_DIRS:main>)

in this minimal example CMakeLists.txt uses the properties above to work correctly.

Limit code language standard

C++17 and C++20 standard code is used throughout projects of all sizes, perhaps with limited-feature fallback to older language standards. Some standards certifications require a specific language standard. High reliability and safety-critical projects may require specific language standards. Examples include FACE and MISRA C++.

To enforce a specific language standard be limited, consider in a header used throughout the project as follows. This example limits the language standard to C++14 or earlier by halt the build if a higher standard is detected:

#if __cplusplus >= 201703L
#error "C++14 or earlier required"
#endif

For C code say no higher than C99, consider in a header used throughout the project, which will halt the build if a higher standard is detected:

#if __STDC_VERSION__ >= 201112L
#error "C99 or earlier required"
#endif

Related: MVSC __cplusplus macro flag

CMake ignore Anaconda libraries and compilers

Anaconda Python conda activate puts Conda directories first on environment variable PATH. This leads CMake to prefer finding Anaconda binaries (find_library, find_program, …) and Anaconda GCC compilers (if installed) over later directories on system PATH. Anaconda libraries and compilers are generally incompatible with the system or desired compiler. For certain libraries like HDF5, Anaconda is particularly problematic at interfering with CMake.

Detect Anaconda environment by existence of environment variable CONDA_PREFIX.

Fix by putting in CMakeLists.txt like the following.

NOTE: CMAKE_IGNORE_PREFIX_PATH does not take effect if set within Find*.cmake.

cmake_minimum_required(VERSION ...)

# ignore Anaconda compilers, which are typically not compatible with the system
if(DEFINED ENV{CONDA_PREFIX})
  set(CMAKE_IGNORE_PATH $ENV{CONDA_PREFIX}/bin)
endif()

project(example LANGUAGES C)

# Optional next two lines if needing Python in CMake project
unset(CMAKE_IGNORE_PATH)
find_package(Python ...)
# end optional lines

# exclude Anaconda directories from search
if(DEFINED ENV{CONDA_PREFIX})
  list(APPEND CMAKE_IGNORE_PREFIX_PATH $ENV{CONDA_PREFIX})
  list(APPEND CMAKE_IGNORE_PATH $ENV{CONDA_PREFIX}/bin)
  # need CMAKE_IGNORE_PATH to ensure system env var PATH
  # doesn't interfere despite CMAKE_IGNORE_PREFIX_PATH
endif()

To totally omit environment variable PATH from CMake find_* use CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH:

set(CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH false)

However, this can be too aggressive i.e. it might miss other programs on PATH actually wanted.

Windows host cross-build for Linux target

Visual Studio supports cross-builds on Windows host for Linux targets. This requires either a remote Linux machine connection, or using WSL on the local computer.

A more robust solution without additional setup on developer computers is CI/CD such as GitHub Actions or many other online and offline choices such as Jenkins. When the developer Git pushes, the CD job provides binaries across operating systems.

An example of GitHub Actions CD is the Ninja project. They provide old (CentOS 7) Linux binaries, macOS and Windows. This could easily be extended to ARM etc.