Scientific Computing

Detect if Python running as sudo

On Unix-like systems such as Linux and macOS, Python can detect if a script is being run as sudo / root from the UID, which is by definition zero for root.

On Windows, the ctypes module can be used to check if the current user is an administrator.

import os
import ctypes

def running_as_root() -> bool:
    if os.name == 'nt':
        return ctypes.windll.shell32.IsUserAnAdmin()
    else:
        return os.getuid() == 0

Flip image using command line

Its convenient to flip an image vertically or horizontally from Terminal. In general, modifying an image in any way including cropping, flipping, rotating, etc. is a lossy process if the image compression algorithm used is lossy such as JPEG. Lossless compression formats such as PNG will remain lossless with this operation.

Here are examples using ImageMagick or IrfanView. Assume the input image is named “in.png” and the modified image is written to “out.png”.

ImageMagick

ImageMagick is available on most operating systems via their package managers.

  • Windows: winget install ImageMagick.ImageMagick WinGet auto adds “magick” to PATH after restarting Terminal
  • macOS: brew install imagemagick
  • Linux: apt install imagemagick

The order of the options is very important in ImageMagick.

Vertical flip the image by:

magick in.png -flip out.png

Horizontal flip the image by:

magick in.png -flop out.png

IrfanView

IrfanView has been popular for decades on Windows and WINE as a no-cost (not open source) image viewing and simple modification program that handles many formats. IrfanView can also be used from the command line. Install IrfanView directly, not via the Microsoft App Store:

winget install IrfanSkiljan.IrfanView

The order of the commands is important. These commands do not open the IrfanView GUI–they are “headless” operations. If there are spaces in the file paths, they must be enclosed in quotes like "c:/data pics/in.png"

Vertical flip the image by:

& $Env:ProgramFiles\IrfanView\i_view64 in.png /vflip /convert=out.png

Horizontal flip the image by:

& $Env:ProgramFiles\IrfanView\i_view64 in.png /hflip /convert=out.png

nanosleep() on Windows with Visual Studio

The POSIX functions nanosleep() and clock_gettime() useful in games and other programs needing repeatable time delay and time measurement is not available in the standard C library on Windows with Visual Studio and certain MinGW CPU architectures.

Our CLOCK_MONOTONIC and CLOCK_REALTIME implementation of nanosleep(), clock_gettime() and clock_getres() for Windows uses Waitable Timer Objects.

The clock tick for Windows is 100 ns. The practical achievable timer resolution on Windows is in the millisecond range. This presents a floor on the accuracy achievable for sleep timers. Virtual Machine running of Windows makes the sleep accuracy significantly worse.

In general across general-purpose operating systems, high accuracy sleep timer performance is not guaranteed. For high accuracy sleep timers (i.e., beyond what is needed for gaming and abstract high-level control of hardware interfaces), consider using a real-time operating system (RTOS) or a dedicated microcontroller.

CMake use MUSL slim libc

CMake can use MUSL slim libc natively (e.g. Alpine Linux), in a Docker container, or by cross-compiling. Cross-compiling in CMake generally uses a toolchain file to set critical variables that can’t be detected automatically. For example, using the x86_64 MUSL binaries from musl.cc, extract the tar file say to ~/musl and then create file ~/musl/musl-toolchain.cmake like:

set(CMAKE_SYSTEM_NAME Linux)
# target system
set(CMAKE_SYSTEM_PROCESSOR x86_64)
# target architecture

set(CMAKE_FIND_ROOT_PATH ${CMAKE_CURRENT_LIST_DIR}/x86_64-linux-musl-native)

# These cross-compilers come from the toolchain downloaded from musl.cc
set(_bin ${CMAKE_FIND_ROOT_PATH}/bin)

set(CMAKE_C_COMPILER "${_bin}/x86_64-linux-musl-gcc")
set(CMAKE_CXX_COMPILER "${_bin}/x86_64-linux-musl-g++")
set(CMAKE_Fortran_COMPILER "${_bin}/x86_64-linux-musl-gfortran")

set(_lib ${CMAKE_FIND_ROOT_PATH}/lib)

set(CMAKE_SYSTEM_LIBRARY_PATH ${_lib})
set(CMAKE_SYSTEM_INCLUDE_PATH ${CMAKE_FIND_ROOT_PATH}/include)

set(CMAKE_EXE_LINKER_FLAGS_INIT -Wl,-rpath-link=${_lib},-rpath=${_lib})

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

Use this to cross-compile a CMake project with MUSL like:

cmake -Bbuild ---toolchain ~/musl/musl-toolchain.cmake

cmake --build build

ctest --test-.dir build

The LINKER_FLAGS allow executables to be run from the host system.

Windows 11 manual upgrade

When turning on an old Windows PC that hasn’t been used for a while, the Windows OS may be several versions out of date. Windows may be so old that using Windows Update doesn’t automatically give an option to upgrade Windows, or may offer a non-latest Windows release. Instead of discarding the PC, consider manually upgrading Windows to the latest release via the Windows Upgrade Assistant. Typically the Assistant will have a PC Health Check that it will ask to have run first. The Health Check will tell among other things the current maximum capacity of the laptop battery. The PC can still be used during the upgrade, which doesn’t take too long. It’s important to use a currently supported version of Windows to be better protected from cybersecurity concerns and to avoid unsupported or non-working software configurations.

Earth Abides miniseries radio frequency

ℹ️ Note

This entire article involves spoilers for the original 1949 book and 2024 miniseries adaptation of “Earth Abides” by George R. Stewart.

The 2024 MGM miniseries adaptation of George R. Stewart’s novel “Earth Abides” has several distinctions from the original study facilitated by 2020’s technology not available in the 1949 book. This technology enables a more comfortable, modern-like existence while still facing many of the same challenges as the book.

A key plot element involves the 2 meter amateur radio band VHF radio frequency 147.225 MHz. However, most of the radios used and antenna notebook designs are specifically for HF. The antenna ultimately erected is consistent with a VHF antenna. The radio powered on is an HF radio, with a possible old signal generator capable of generating a VHF signal being shown. I suppose the lack of modern radios having direct key entry was a factor in the movie production. In my opinion, showing an actual VHF radio being tuned in say 5 kHz steps would have been more realistic and not at all detracting from the story. While millennials and younger generations are decreasingly familiar with tuning a radio dial, it is still not a foreign concept to them and easy to film and understand for world audiences.

Using 50% spot probability and ideal site locations not locally obstructed by terrain or buildings as indicated on the TV show, it is possible with a typical inexpensive VHF mobile transceiver to communicate with adequate FM SNR as shown on the show. It is especially possible during periods of tropospheric ducting experienced in this region that communications would be possible between two base stations.

Earth Abides radio path profile

Path profile for the Earth Abides radio frequency communication.

Intel oneMKL ScaLAPACK examples

Intel oneAPI / oneMKL includes Scalapack. MKL Scalapack library & include files are found under the MKLROOT environment variable. Scalapack MKL is used in CMake via MKLConfig.cmake (found under $ENV{MKLROOT}/lib/cmake/MKLConfig.cmake) like:

set(ENABLE_BLACS true)
set(ENABLE_SCALAPACK true)

set(MKL_INTERFACE "lp64")
# "lp64" is the 32-bit interface, "ilp64" is the 64-bit interface

set(MKL_SYCL_MPI false)
set(MKL_SYCL_LINK false)
# for Intel oneAPI 2025.2, we don't need SYCL

find_package(MKL CONFIG REQUIRED)

add_executable(mytest main.f90)
target_link_libraries(mytest PRIVATE MKL::MKL_SCALAPACK)

The MKL_SYCL* variables control whether SYCL is enabled for Intel oneAPI 2025.2 and later, which usually we don’t want.

The exact link flags and compile flags (that are automatically handled by MKLConfig.cmake) for a particular system are obtained from Intel Link Line Advisor.

Intel MKL Scalapack example project

Netlib Scalapack examples can also be used with non-MKL Scalapack.

GitHub Actions Windows MSYS2 MS-MPI

On Windows the most commonly used MPI libraries are MS-MPI and Intel MPI. MSYS2 is a popular choice to use GCC, Clang and many other developer tools from the Windows Command Prompt, PowerShell or the MSYS Terminal itself.

Continuous Integration for Windows MPI applications on GitHub Actions is accomplished as in fortran-mpi-examples

Set inter-step environment variables using GitHub Actions environment files.

GNU Octave without readline

GNU Octave and other interactive terminal program use GNU Readline for command line editing. If the Readline library is incompatible with GNU Octave, it may become unusable by not being able to type in GUI or non-GUI modes. A workaround for non-GUI mode only is to disable interactive console input like:

octave --no-line-editing

For example Readline 8.3 has a known bug with a patch already released.

SSH-agent for Windows, macOS, Linux

SSH-agent remembers SSH Public Key authentication, which can be time-limited by the user. This avoids the user having to type the password for each SSH connection, especially relevant to using Git over SSH. Windows has SSH built-in from Microsoft including SSH-agent. WSL also can use SSH-agent within each of the WSL images. SSH-agent works well with Git over SSH.

To use SSH-agent, add SSH keys like:

ssh-add ~/.ssh/mykey

This will persist the key in the SSH-agent until the SSH-agent is stopped, the user logs out,or the key is removed.

Remove all SSH-agent keys from RAM (if desired):

ssh-add -D

List all SSH-agent keys loaded:

ssh-add -L

Time limit for SSH-agent keys

Except for Windows, a time limit can be set for how long the key is remembered:

ssh-add -t 30m ~/.ssh/mykey
-t 30m
remember authentication for a period of time (here, 30 minutes)

Currently, the OpenSSH implemenation of SSH-agent on Windows does NOT support the -t option. If the -t option is used on Windows, it will fail like:

Could not add identity <key>: agent refused operation


Each operating system has a distinct method of enabling SSH-agent.

Windows SSH-agent

SSH-agent can be enabled from PowerShell. Note that the OpenSSH Client and OpenSSH server must both be installed.

Check if Windows SSH-Agent is running:

Get-Service ssh-agent

Start SSH Agent:

Set-Service -StartupType Automatic -Name ssh-agent

Start-Service ssh-agent

if status of Windows SSH-Agent in Powershell is “Running” then SSH-agent should be working.

Get-Service ssh-agent

If you still have trouble, try setting the permissions for the $HOME/.ssh directory more conservatively with this Powershell script.

Linux SSH-agent

For Linux, including Windows Subsystem for Linux:

Add to ~/.profile:

if [ -z "$(pgrep ssh-agent)" ]; then
   rm -rf ${TMPDIR}/ssh-*
   eval $(ssh-agent -s) > /dev/null
else
   export SSH_AGENT_PID=$(pgrep ssh-agent)
   export SSH_AUTH_SOCK=$(find ${TMPDIR}/ssh-* -name agent.*)
fi

macOS SSH-agent

On macOS, SSH-agent is enabled by default.


SSH agents can have vulnerabilities, as noted for Windows and Linux.


Related: Disable Gnome Keyring SSH Agent

reference