C23 attribute specifiers

C23 specification added C++-like attribute specifiers to C. C attribute specifiers add metadata to declarations and definitions. The metadata can be used by the compiler and by the developer to help understand the code intent.

Commonly used attributes such as [[maybe_unused]] and [[fallthrough]] suppress compiler warnings.

Compilers including GCC ≥ 11 have __has_c_attribute() to check if an attribute is supported by the compiler–even if the command line specified standard is older.

maybe_unused attribute

To enable code to fallback to no attribute with older compilers, use logic in header file like:

#if !defined(__has_c_attribute)
#  define __has_c_attribute(x) 0
#endif

#if __has_c_attribute(maybe_unused)
#  define MAYBE_UNUSED [[maybe_unused]]
#else
#  define MAYBE_UNUSED
#endif

then in the definition:

int windows_fun(int x, MAYBE_UNUSED int y) {

#ifdef _WIN32
  return y;
#else
  return x;
#endif
}

On non-Windows systems, the compiler would have issued a warning about y being an unused argument. The [[maybe_unused]] attribute suppresses that warning.

fallthrough attribute

The [[fallthrough]] attribute is used to indicate that a fall-through in a switch statement is intentional. This attribute can be used to suppress warnings about missing break statements in a switch block.

In declaration (header) file do like:

#ifndef __has_c_attribute
#define __has_c_attribute(x) 0
#endif
#if __has_c_attribute(fallthrough)
// GCC >= 11
#define FALLTHROUGH [[fallthrough]]
#elif defined(__GNUC__) && __GNUC__ >= 7 || defined(__clang__) && __clang_major__ >= 12
#define FALLTHROUGH __attribute__((fallthrough))
#else
#define FALLTHROUGH
#endif

then in the definition:

switch (x) {
  case 1:
    x++;
    FALLTHROUGH;
  case 2:
    x--;
}

C++ attribute specifiers

C++ attribute specifiers add metadata to declarations and definitions. The metadata can be used by the compiler and by the developer to help understand the code intent.

Commonly used attributes such as [[maybe_unused]] and [[fallthrough]] suppress compiler warnings. [[likely]] and [[unlikely]] may be used by the compiler to optimize compilation, and also provide human code readers insight into the intent of the algorithm.

GCC ≥ 7 has __has_cpp_attribute() to check if an attribute is supported by the compiler–even if the command line specified standard is older.

maybe_unused attribute

To enable code to fallback to no attribute with older compilers, use logic in header file like:

#if !defined(__has_cpp_attribute)
#  define __has_cpp_attribute(x) 0
#endif

#if __has_cpp_attribute(maybe_unused)
#  define MAYBE_UNUSED [[maybe_unused]]
#else
#  define MAYBE_UNUSED
#endif

then in the definition:

int windows_fun(int x, MAYBE_UNUSED int y) {

#ifdef _WIN32
  return y;
#else
  return x;
#endif
}

On non-Windows systems, the compiler would have issued a warning about y being an unused argument. The [[maybe_unused]] attribute suppresses that warning.

fallthrough attribute

The [[fallthrough]] attribute is used to indicate that a fall-through in a switch statement is intentional. This attribute can be used to suppress warnings about missing break statements in a switch block.

In declaration (header) file do like:

#ifndef __has_cpp_attribute
#define __has_cpp_attribute(x) 0
#endif
#if __has_cpp_attribute(fallthrough)
// GCC >= 7
#define FALLTHROUGH [[fallthrough]]
#else
#define FALLTHROUGH
#endif

then in the definition:

switch (x) {
  case 1:
    x++;
    FALLTHROUGH;
  case 2:
    x--;
}

Python tempfile on Windows

Python tempfile is generally robust.

TemporaryDirectory(ignore_cleanup_errors=True) fixes the Windows corner case on exiting a tempfile context manager such as cloning a Git repo into the TemporaryDirectory.

import tempfile

with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
    # create subdirectories, make files, git clone, etc.
    # the context manager attempts to recursively delete "tmpdir" on exit

If there is a PermissionError, the temporary directory remains until the operating system cleans up the temporary directory.

Python tempfile.NamedTemporaryFile defaults for Python ≥ 3.12 handle file cleanup on Windows.

Pacman parallel download level option

Pacman can download packages in parallel to speed up the process. The number of parallel download threads can be controlled in the “/etc/pacman.conf” file. This can be useful on slower internet connections to install packages without disrupting other network activities.

To set the number of parallel download threads, edit “/etc/pacman.conf” file. Find the “ParallelDownloads” option in the file. If it is not present, add it under [options] section. Set the number of download threads. The ordering of options doesn’t matter. For example, to use 3 parallel download threads:

[options]
ParallelDownloads = 3

For very slow networks, consider ParallelDownloads = 1 and pacman --disable-download-timeout to avoid timeouts.

C++ Forward Progress Guarantee talks

Olivier Giroux chairs the ISO C++ subgroup for Concurrency and Parallelism. Olivier continues to present very useful interactive talks on C++ concurrency and parallelism. This talk is about the C++ Forward Progress Guarantee in light of concurrency and parallelism.

C++ Proposal P2809R3 is to allow trivial infinite loops as defined behavior in C++.

The definition Olivier uses for concurrency at 16:50 in the video above is:

Concurrent tasks eventually observe each other’s effects.

Previous talks on this subject by Olivier follow:

GnssLogger app save file location

The Android GnssLogger app logs data in the user-selected formats, including RINEX. GnssLogger can run for hours or days, assuming the device has enough storage. Do test runs to be sure unnecessary other data files aren’t also stored as they can be much larger than the RINEX data.

Android Raw GNSS measurements are available on numerous listed devices.

When clicking “save and send” to end a logging session, the app asks where to save in the cloud. Simply canceling the upload retains the data in the “Downloads” folder on the device. This can be useful when the device has a low-bandwidth or expensive data connection, and the data can be uploaded later or copied to a computer via USB.

Fortran alias procedure name

In addition to generic and polymorphic procedures, Fortran also allows aliasing procedure names. This is useful to provide a shorter or more convenient name for a procedure.

It’s easiest to understand from the example below.

program main

interface easy
procedure :: long_and_descriptive_procedure_name
end interface

call easy()

contains

subroutine long_and_descriptive_procedure_name
print '(a)', "Hello from long name"
end subroutine long_and_descriptive_procedure_name

end program

Install Matlab Engine API in Python

Matlab Engine API allows calling Matlab functions from Python code.

These commands are executed from Terminal, not from Matlab. Go to the Matlab Engine directory to setup Matlab Engine, where “python” starts the desired Python executable.

r=$(matlab -batch "disp(matlabroot)" | tail -n1)

python -m pip install -e ${r} --user

Root / admin permissions are NOT needed.


A simple example of using Matlab from Python:

import matlab.engine
eng = matlab.engine.start_matlab('-nojvm')
y = eng.asin(1.)

eng.quit()

The Matlab Engine should take about 1 second for Matlab Engine to start when called from Python. For Matlab functions requiring JVM remove the “-nojvm” option.

Many Matlab numeric classes (single, double, logical) can be converted to Python types like:

numpy.asarray(x)

Python floats pass into Matlab Engine by including a period . after the number.

  • asin(1) fails
  • asin(1.) works

Python can pass N-dimensional arrays to Matlab.

Matlab Engine provides asynchronous call with background=True


References:

Related:

PDCurses build with CMake

PDCurses is a long-standing Curses terminal graphics library for which we provide CMake build script that works across operating systems including Windows, Linux, macOS and DOS. X11, SDL2 and Windows console backends are supported.

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