Scientific Computing

nmap in Cygwin - seamlessly

WSL2 works with Nmap directly and is generally recommended instead of Cygwin. For those who do need Cygwin, it’s possible to use Nmap from Cygwin, silently calling Windows Nmap.

Install by download nmap. Install nmap “self-installer” .exe. When asked, also install Npcap. Cygwin: add to ~/.bash_profile the following. Note the \ and \( are vital for Cygwin shell to interpret the command correctly.

alias nmap="/cygdrive/c/Program\ Files\ \(x86\)/Nmap/nmap.exe"

Open a new Cygwin window to start using nmap

Test nmap in Cygwin:

nmap 8.8.8.8

results in

Starting Nmap ( https://nmap.org )
Nmap scan report for 8.8.8.8
Host is up (0.0092s latency).
Not shown: 998 filtered ports
PORT STATE SERVICE
53/tcp open domain
443/tcp open https

Nmap done: 1 IP address (1 host up) scanned in 7.41 seconds

Troubleshooting:

  • errors about interface → try running Cygwin as Administrator (right click on Cygwin icon).
  • find interface names available to nmap
nmap --iflist

Notes:

  • to find servers with a particular port open on a subnet, try my Python findssh program that scans for servers without nmap.
  • If you don’t install Npcap when asked in the nmap installer, nmap does not work. Nmap claims no host existed at a known working IP address.

Windows Git Credential Manager

Git over SSH can use SSH-agent to remember SSH keys for Linux and Windows. For those not using Git over SSH but tiring of typing the Git password, on Windows the Git Credential Manager can fix that situation. The install is small and quick, and to use Git Credential Manager, be sure via

git remote -v

that the connection is https:// and not ssh:// where appropriate. Upon trying to git push or git pull, the Git Credential Manager will appear. It works with 2-factor Authentication as well, storing in Windows Credential Manager. The keys can be deleted from Windows Credential Manager if needed, and / or the Personal Access Token automatically generated can be deleted from GitHub.

Notes

If using automatic Git push SSH that would need to be disabled to allow git push to use https so that Git Credential Manager is invoked.

Future

Git Credential Manager Core 2.x is the next generation OS-agnostic Git credential manager that also works on more than just GitHub (e.g. also works on Bitbucket). At the moment, we experienced a bug that prevented using the next generation GCM.

Paraview Python plot frozen with Render

Opening Paraview Python “pvpython” terminal is accomplished by:

  • Linux: pvpython should be on $PATH.
  • macOS: Look under /Applications/ParaView*.app/Contents/bin/pvpython
  • Windows: Look under Start Menu > ParaView > pvpython

Paraview’s Python interface scripts that use Render() create a vestigial feature that is frozen. To make the plots interactive, use Interact() instead.

Python Paraview example:

from paraview.simple import *

s = Sphere()
Show()
Interact()

Using CMAKE_ROLE to detect CMake run mode

The operating mode of CMake can be detected via CMAKE_ROLE. This variable must be read with:

get_property(cmake_role GLOBAL PROPERTY CMAKE_ROLE)

Here, the variables cmake_role is created and can then be used in if() statements as needed.

CMake Ninja generator preprocesses Fortran

CMake with the Ninja and Ninja Multi-Config generators virtually always work well together, including with Fortran projects. To generate the dependency graph, CMake directs the Fortran compiler to preprocess each Fortran code file when using the Ninja generator. In rare cases, some legacy Fortran large code files can generate unexpected and spurious syntax errors when using CMake + Ninja.

The workaround seems to be to use the GNU Make backend, we have found that works for the rare case this became an issue.

Android switch to 3-button navigation

Android task switching swipe navigation can be switched to 3-button navigation. Some workflows are much easier via the traditional 3-button navigation used by Android since Android 3.x / 4.x. Also when wearing gloves, using 3-button navigation can work better.

Switch an Android device to traditional 3-button navigation (back, home, switch or triangle, circle square) by going to Settings → System → Gestures → System navigation and select “3-button navigation”.

CMake Fortran design patterns

CMake is excellent for building very complex Fortran projects across operating systems and computing platforms from embedded systems to HPC. Here are a few common Fortran CMake patterns.

specify linker options for all targets using add_link_options()

add_link_options(-myflag)

or for a particular target with target_link_options

For hand-written Makefile the order of libraries matters completely. CMake attempts to determine the graph for library files, starting from the user specified order. CMake will try a finite multiplicity to resolve the graph, but in tough cases of library interdependency library ordering may need to be influenced by add_dependencies() or even object libraries.

“Undefined reference to” occurs at linking of main executable: be sure the library is actually linked to the user library with

cmake --build build -v

Fortran module include: be sure the *.mod file directory is included, particularly to the main executable by setting the target properties like:

include(GNUInstallDirs)

target_include_directories(mymod INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR})

set_property(TARGET mymod PROPERTY Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR})

this can be done for numerous targets in a loop instead of repeatedly specifying by a foreach() loop:

foreach(t foo bar biz baz)
  target_include_directories(${t} INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR})

  set_property(TARGET ${t} PROPERTY Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR})
endforeach()

To ensure the build system is completely updated, do

cmake --build build --clean-first

Specify C and CXX for CMake projects with MSVC

A quirk with Visual Studio and CMake for C++ projects is that the C language must also be specified in CMakeLists.txt like:

project(foo LANGUAGES C CXX)

Otherwise, weird linker errors can occur, even on a pure C++ project with no C files.

CMake FindHDF5 improvements

An improved FindHDF5.cmake in h5fortran handles modern HDF5 versions including on Windows.

The important improvements are:

  1. check that HDF5 compiler wrapper is able to compile and link a simple HDF5 program
  2. have more default directories to search for the HDF5 libraries
  3. work with HDF5 across versions
  4. work with HDF5-MPI

Before this, CMake would get confused by the Anaconda Python HDF5 compiler wrapper. This also makes Intel compiler on Windows work with HDF5.

Benefits of CMake Object Libraries

CMake Object Libraries can be used to keep build directories less cluttered and speed up the build process. The traditional workflow for Makefile was to create lots of object files (targets) with the relevant compile options, definitions and link flags. Then the final executable or library targets would be linked from all the object files.

CMake add_library() defaults to creating static libraries. The following stanza creates “libfoo.a” or similar:

add_library(foo src.f90)

With numerous targets, there are a lot of “lib*.a” that may be cluttering the build directory. Also, there is a finite time consumed in creating all those archive .a files from the object files. For projects that use Visual Studio, using Object Libraries can help avoid complications with exporting symbols for MSVC.

CMake object libraries: a cleaner option for most cases where the “lib*.a” artifact is not needed is to create CMake Object Libraries instead:

add_library(foo OBJECT src1.f90 src2.f90)
add_library(bar OBJECT src3.c src4.c)

add_executable(baz main.cpp $<TARGET_OBJECTS:foo> $<TARGET_OBJECTS:bar>)

instead of having files under build/ like “libfoo.a, libbar.a” there will be only “baz.exe”. The object files will be tucked away under build/CMakeFiles/foo/*.o and build/CMakeFiles/bar/*.o. The generator expressions expand to all the object files of “foo” and “bar” like a legacy Makefile.

The INTERFACE target properties of object libraries propagate up to targets linking them as usual. You may need to modify the PUBLIC / PRIVATE / INTERFACE of object libraries or add them to the “target_link_libraries()” of the top-level targets when going from static or shared libraries to object libraries.

Caveats: object libraries with duplicate symbols can cause conflicts that need to be manually resolved. When linking non-object libraries, the linker typically just uses the first instance seen of a symbol.