Scientific Computing

Git contributor feature branch workflow

Major Git projects commonly have a contributor workflow where other contributors fork a primary Git repository, make changes, and then contribute back to the primary project. This article describe the contributor workflow. The maintainer workflow is in a separate article.

The contributor forks the primary project Git repo. On the local copy of the fork, create a feature branch:

git switch -c add-feature1

Once the new work is complete, make the branch consistent with the primary repo by pulling in the latest changes from the primary repo:

git switch main
# whatever the primary or development branch of the primary repo is

git remote add upstream https://github.invalid/primary/repo_url

git fetch upstream

git rebase upstream/main

Update the local branch to remote main

git switch add-feature1

git rebase main

Test the final updated code. Create the Pull Request / Merge Request for the maintainer to evaluate the changes and possibly put them into the primary project.

MPI vader unlink error

OpenMPI 4.x uses temporary files for UNIX sockets, which are limited to 100 character path length. This can become an issue on macOS, which by default sets POSIX environment variable TMPDIR to a pseudorandom path under “/var” about 50 character long. We have experienced this as a race condition that becomes more likely as the number of MPI workers is eight or more.

A workaround is to set an alias for mpiexec or within CMake test properties etc. such that mpiexec gets a sufficiently short working path so that UNIX sockets don’t fail due to excessive path length.

CMake example

Fortran contiguous variables

Fortran 2018 contiguous arrays are discussed in pp.139-145 of “Modern Fortran Explained: Incorporating Fortran 2018”. In general, operating on contiguous arrays is faster than non-contiguous arrays.

Contiguous variables happen by default, so unless using pointers or array striding, the variable is likely to be contiguous for simple Fortran code. Check contiguous status of a variable with is_contiguous intrinsic function.

A non-contiguous array actual argument into a contiguous subroutine dummy argument is made contiguous by copy-in, copy-out. This copy-in copy-out as needed is part of the Fortran 2008 and Fortran 2018 standard. GCC ≥ 9, Intel oneAPI, IBM Open XL Fortran, etc. work to Fortran 2008+ standard for contiguous dummy argument copy-in, copy-out for non-contiguous actual argument.

References: Fortran 2008 Contiguity

CMake with clang-tidy

CMake can use clang-tidy as a build option for a CMake project by adding a few parameters to CMakeLists.txt. It’s also possible to use clang-tidy without modifying CMakeLists.txt. Generate the file compile_commands.json used by clang-tidy. You don’t have to build the project as we are interested in checking and possibly fixing the C++ source code.

To install clang-tidy:

  • macOS: brew install llvm, then edit ~/.zshrc, adding the line alias clang-tidy=$(brew --prefix llvm)/bin/clang-tidy or similar
  • Linux: apt install clang-tidy or similar
cmake -Bbuild -DCMAKE_EXPORT_COMPILE_COMMANDS=on

Run clang-tidy. Specify options or use a file .clang-tidy, which is usually in the top directory of the project.

clang-tidy -p build/ <glob or files>

To fix the errors, add clang-tidy option --fix.

CMake sleep

CMake doesn’t have a “sleep” command, but sleep in floating point seconds is accomplished by:

execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 0.5)

This uses CMake command tool to sleep in a platform-indpeendent manner.

cmake -E sleep 0.5

The minimum practical time is about 0.1 seconds. Below that, the overhead of starting/stopping CMake starts to dominate elapsed time.

CMake profile configure

CMake can output JSON in google-trace format that can help understand what tasks a CMake project configuration is taking time on.

cmake -B build --profiling-output=perf.json --profiling-format=google-trace

View profiler data: type in web browser (Chrome or Edge) address bar “about:tracing” and Load “perf.json”.

Other google-trace viewers can also be used.

For example, in a minimal C++ project on a Windows computer:

cmake_minimum_required(VERSION 3.18)
project(b LANGUAGES CXX)

Approximate percent of CMake configure time spent:

  • system ID: 1%
  • Generator identify: 2%
  • Compiler identification: 50%
  • Compiler capabilities: 47%

CMake get current time

CMake can retrieve the current time in local (default), UTC, or other timezone via string(TIMESTAMP). Elapsed time can also readily be computed or compared using timestamp format %s

Currently, CMake math command cannot handle floating point numbers, only integers.

string(TIMESTAMP t)
message(STATUS "local time: ${t}")

string(TIMESTAMP t0 "%s")
execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 1.0)
string(TIMESTAMP t1 "%s")

math(EXPR elapsed "${t1} - ${t0}")

message(STATUS "Elapsed time: ${elapsed} seconds")

Git GUI install

Git history browsing can be easier with a GUI, which are available for most platforms. Git installs can come with a GUI by default. However, many package managers make the GUI a separate install to save considerable time and space on cloud servers, virtual machines and similar. Here are a few example commands to install gitk, a basic Git history browser:

  • macOS: brew install git-gui
  • Linux: apt install gitk or dnf install gitk or similar
  • Windows: winget install Git.Git that comes with “gitk”

For those using GitHub, GitHub Desktop is a popular Git GUI.