CMake detect CPU model

The Intel compilers have an undocumented -march=native or /arch:native flag parameter. Currently, this undocumented “native” flag parameter enables only Pentium 4 optimizations. Using the CPU generation noted in the “arch” documentation results in many CPU-specific flags being added.

CMake detect CPU

CMake can detect the host CPU arch to use with Intel compilers. For example using -march=icelake-client adds flags as revealed by -watch adds flags including:

-D__SSE3__
-D__SSSE3__
-D__SSE4_1__
-D__SSE4_2__
-D__POPCNT__
-D__PCLMUL__
-D__AES__
-D__AVX__
-D__F16C__
-D__AVX_I__
-D__RDRND__
-D__FMA__
-D__FP_FAST_FMA
-D__FP_FAST_FMAF
-D__BMI__
-D__LZCNT__
-D__AVX2__
-D__AVX512F__
-D__AVX512DQ__
-D__ADX__
-D__RDSEED__
-D__AVX512IFMA__
-D__AVX512CD__
-D__AVX512BW__
-D__AVX512VL__
-D__AVX512VBMI__
-D__AVX512VPOPCNTDQ__
-D__AVX512BITALG__
-D__AVX512VBMI2__
-D__GFNI__
-D__VAES__
-D__VPCLMUL__
-D__AVX512VNNI__
-D__CLWB__
-D__RDPID__
-D__icelake_client
-D__icelake_client__
-D__tune_icelake_client__

Fortran 2018 coarray quick start

Fortran coarrays are an abstract, higher-level construct than using MPI directly. Coarrays support multiple parallel processing libraries including MPI. The simpler Coarray syntax can make debugging easier in general. Gfortran, NAG, Intel and Cray are among the compilers supporting Fortran 2018 coarray features so useful in high-performance computing.

Install

Coarray Fortran as enabled in Fortran 2008 / Fortran 2018 standards is available from multiple compilers. Compilers with built-in coarray support (not needing external libraries) include:

  • Intel ifort
  • NAG nagfor
  • Cray ftn

The OpenCoarrays library is popular for enabling coarray features in Gfortran

Gfortran

Install Fortran OpenCoarrays library for Gfortran by:

Ubuntu / Debian / Raspberry Pi (packages names may vary by Linux distro version)

apt install gfortran libcoarrays-dev libcoarrays-openmpi-dev

Linux / MacOS: using Homebrew

brew install gcc opencoarrays

When manually compiling, add -fcoarray=lib and -lcaf_mpi. For example:

gfortran -fcoarray=lib myfun.f90 -lcaf_mpi

When manually running use cafrun like:

cafrun -np 4 ./myprog

Intel Fortran

ifort Fortran 2018 standard support includes coarrays.

ifort -coarray

enables the coarray features.

CMake

OpenCoarrays includes CMake scripts. CMake itself can recognize generic coarray support with FindCoarray.cmake

An example top-level CMakeLists.txt using Fortran coarray contains:

find_package(Coarray REQUIRED)

add_executable(coarray_pi pi.f90)
target_compile_options(coarray_pi PRIVATE ${Coarray_COMPILE_OPTIONS})
target_link_libraries(coarray_pi ${Coarray_LIBRARIES})

add_test(NAME CoarrayPi
COMMAND ${Coarray_EXECUTABLE} ${Coarray_NUMPROC_FLAG} ${Coarray_MAX_NUMPROCS} ./coarray_pi)

Examples

Fortran coarray examples includes CMakeLists.txt for Fortran coarrays.

Notes

Install Intel oneAPI C++ and Fortran compiler

Intel oneAPI is a cross-platform toolset that covers several languages including C, C++, Fortran and Python. Currently, Intel is providing oneAPI including the Fortran compiler at no cost, and no login is required to download oneAPI. The no-cost oneAPI access is distinct from Intel Parallel Studio that requires a paid license, except for instructional use or to develop open source projects.

Install

Refer to the oneAPI install guide to download the oneAPI Fortran compiler for Linux or Windows.

  1. (Windows only) Install latest Visual Studio
  2. install GDB from oneAPI Base Toolkit
  3. Install the remaining packages below with oneAPI HPC toolkit

The oneAPI package options we installed included:

  • GDB
  • Intel MPI library
  • Intel C++ compiler
  • Intel Fortran compiler
  • Intel Math Kernel Library

Usage

As with Intel Parallel Studio, there are slightly distinct usage patterns to access the Intel oneAPI compilers on Windows vs. Linux. NOTE: it is NOT recommended by Intel to use ifx.exe or icx.exe directly. You should be using ifort icl icc icpc with Intel oneAPI as with Intel Parallel Studio.

Windows

On Windows a Start menu shortcut for a oneAPI command prompt is installed. CMake et al should just find the Intel compiler when in the oneAPI command prompt. Otherwise run setvars.bat as per oneAPI documentation.

Linux

each time you wish to use Intel oneAPI, do

. /opt/intel/inteloneapi/setvars.sh

Avoid general use of GCC march native flag

The GCC -march=native flag can cause unexpected and difficult to diagnose error messages on certain CPU arches, including high end Intel Xeon CPUs. The compiler error messages generally don’t give any clues as to what the real problem may be. While there can be a small to significant performance benefit to using the GCC flags enabling special features in a CPU arch, we feel it’s important to provide software that “just works” by default, even if not completely optimized for a given CPU. The end users that care most about performance probably have already learned the appropriate flags for their CPU, and are willing to fiddle with the CPU arch flags to get the program running at optimum.

We generally have no problems using the GCC -mtune=native flag.

GCC CPU flags

GCC will show a neat list of which flags are enabled and disabled like:

gfortran -Q --help=target foo.f90

Or to just show the raw flags being used

gfortran -v foo.f90

Reference

X11 desktop GUI apps for Windows Subsystem for Linux

Linux X11 GUI programs often work fine on Windows Subsystem for Linux. This procedure has been used for years on WSL1. WSL2 may need additional setup due to the more advanced network configuration of WSL2.

Setup

Install a Windows X11 server to enable graphical Linux applications on Windows as follows:

Install VcXsrv and add to WSL ~/.bashrc

DISPLAY=:0.0
export DISPLAY

Usage

To run any X11 Linux GUI app in WSL, start VcXsrv first from Windows. You don’t have to do this before starting WSL.

Notes

  • on WSL1, do NOT open a port in your firewall for X11 if asked by Windows.
  • old Xming X11 server

Fortran MPI with MSYS2 MinGW on Windows

OpenMPI is not currently available for native Windows. While Cygwin and WSL do have working OpenMPI, it is also possible to use MPICH via Microsoft MS-MPI as described in this article.

setup MPI on Windows MSYS2

This procedure gives MPI via MSYS2 GCC / Gfortran compilers on Windows.

  1. Download and install Microsoft MS-MPI

  2. To make mpiexec.exe available, add to user PATH: C:\Program Files\Microsoft MPI\Bin – this is needed even when using MSYS2.

  3. Install MSYS2 MS-MPI library, from the MSYS2 Terminal

    pacman -S mingw-w64-x86_64-msmpi
    
  4. compile, creating “mpi.mod” for Fortran use. Do NOT link with ar or MPI may only run with a single image.

    cd C:/msys64/mingw64/
    
    gfortran include/mpi.f90 -c -fno-range-check -fallow-invalid-boz
    

Notes

If you don’t compile the mpi.f90 as above, errors may result like:

use mpi

Fatal Error: Cannot open module file 'mpi.mod' for reading at (1): No such file or directory

MS-MPI without MSYS2

If not using MSYS2, it’s still possible use MS-MPI. We will put the resulting artifacts under c:/lib/mpi

mkdir c:/lib/mpi/

Copy-Item -Path "C:/Program Files (x86)/Microsoft SDKs/MPI/Include" -Destination "c:/lib/mpi/" -Recurse

cd c:/lib/mpi

you may need to edit “include/mpi.f90” to have INCLUDE 'mpifptr.h'

gfortran include/mpi.f90 -c -fno-range-check -fallow-invalid-boz

Do NOT link with ar or MPI may only run with a single image.

Ensure Git contribution are tracked over time

The GitHub contribution calendar is one of the key ways a future client, employer or colleague gets a quick glance at your individual Git contribution level. Consider including private contributions on your profile. GitHub includes more than just Git commits to master branch as depicted in the GitHub contribution rose:

GitHub contribution rose

Whether hiring is waxing or waning in your field it’s important to retain the value of software work over time by tracking Git commit and other contributions. Whether you’ve built up your development career from boot camp or college, from devops or in pursuit of science data analysis, one never knows how their career might shift. Software is a common core skill necessary for many fields, and it’s important to show the distinction of your software work over time. Help do so by ensuring your Git commits are counted over time.

Git commits with coauthor / multiple authors

Git does not have a integral mechanism to have multiple authors per Git commit. A Git coauthor notation convention has become accepted by some major services.

  • Github: supported
  • GitLab: supported
  • Bitbucket: not supported

Syntax

The Git coauthor convention is placing plaintext in the commit message body. To track commits on GitHub or other services, the email address cited must match a registered email with that service. The email can be a working public email or the “fake” noreply email provided by the Git service. Multiple coauthors each use the same syntax on the same Git commit.

added foo function to bar.py

Co-authored-by: David <snake@users.noreply.github.com>

Coauthor tracking

On GitHub, a coauthor commit looks like:

GitHub coauthor detail

Currently, the coauthored commit doesn’t count on the user’s GitHub contribution calendar. I suppose the coauthor commits don’t count toward the contribution calendar to avoid stuffing / inflating contribution count of users.

GitHub contribution calendar

The coauthor commits do show up in GitHub search under “Commits”.

Caveats

As with regular Git commits, there is no authentication to avoid someone masquerading as someone else with Git coauthor commits. Git coauthor commits cannot be GPG signed for each coauthor, only the primary Git committer can GPG sign as usual.

Matlab remote desktop rendering

Matlab’s rendering engine can break under some remote desktop use cases, including X11 forwarding. There can also be issues when using Matlab plots locally if a problem arises with a GPU graphics driver.

Diagnose

Matlab function rendererinfo() provides extensive details about the rendering backend. It can be invoked from the Matlab GUI

rendererinfo(gca)

or from the system Terminal

matlab -batch "rendererinfo(gca)"

Workaround

Although slow, software-based and feature-limited, the painters algorithm can be reliable if something outside Matlab is hindering graphics (local or remote). Tell

figure
set(gcf,'renderermode','manual','renderer','painters')

confirm the Painters renderer is being used by:

rendererinfo(gca)

which should result in

GraphicsRenderer: ‘MathWorks Painters’

The Mathworks has detailed rendering troubleshooting.