Scientific Computing

Select compiler versions update-alternatives

Switch between compilers e.g. g++-7 and g++-8 with simple commands. Note: We suggest NOT using sudo, but rather to make the links under ~/.local/bin, which should already be in your PATH (or start using it as in step 1).

(one-time) Setup shell to use ~/.local/bin instead of system-wide /usr. This is generally beneficial in any case.

mkdir ~/.local/bin

Add to ~/.profile:

export PATH="$HOME/.local/bin:$PATH"

(one-time setup) enable switching

update-alternatives --install $HOME/.local/bin/g++ g++ /usr/bin/g++-7 20
update-alternatives --install $HOME/.local/bin/g++ g++ /usr/bin/g++-6 10

and so on for gcc and gfortran

At any time, switch compiler versions:

update-alternatives --config g++
update-alternatives --config gcc
update-alternatives --config gfortran

update-alternatives works with virtually any program including Python.

Compiler version priority order: last number of update-alternatives --install is priority. The highest priority number is used in “automatic” update-alternatives mode.

Troubleshooting: If accidentally reversed the order of the link and target or if used sudo in /usr/bin then may need to reinstall the compiler.

expanduser home directory tilde Fortran

Like Python, Fortran does not understand tilde ~ in commands like open(). Just like in Python and some Matlab functions, an expanduser() procedure is needed. We provide this functionality in fortran-filesystem expanduser().

The shell typically expands ~ itself. It becomes an issue when reading an absolute path involving ~ from say a config file using Fortran and then trying to open that filename read from the config file.

Find Linux package dependencies

Numerous Python packages use PyTest extensively. Upon apt install a package that depends on python-astropy, python-pytest is also installed. The system version of a package is typically several minor versions behind for stable Linux distros like Ubuntu. To find which specific package is responsible for a package being installed, use a command like:

apt-cache rdepends --installed python-pytest

which reveals that

python-py
python-astropy

are requiring this old pytest version.

Disable Gnome Keyring SSH Agent

Ubuntu Gnome Agent remembers SSH private key passwords until you log out. If someone knows an Ubuntu user password, they also have access to any SSH private keys loaded since last logon.

This also fixes error upon trying to use ssh or sshfs:

sign_and_send_pubkey: signing failed: agent refused operation

Permanently disable Gnome Keyring SSH Agent by including this line in /etc/xdg/autostart/gnome-keyring-ssh.desktop

X-GNOME-Autostart-enabled=false

Reboot and test that private key passwords aren’t being remembered.


Alternative method to disable Gnome Keyring SSH Agent: Edit /etc/xdg/autostart/gnome-keyring-ssh.desktop to include the line:

NoDisplay=false

Under Startup Applications → SSH Key Agent (uncheck). Reboot and test that private key passwords aren’t being remembered.


Related: configure SSH agent to remember SSH keys

Installing GPSTk in Anaconda Python

GPSTk Python examples require installing GPSTk for Python. GPSTk is a complicated program that is more difficult to install than typical Python programs.

Install

Unstall minimal prereqs

apt install g++ make cmake swig

Download gpstk source

git clone --depth 1 https://github.com/SGL-UT/GPSTk
cd GPSTk

Build & install

./build.sh -ue

Test

Get the example code

git clone https://github.com/scivision/gpstk-examples-python

GPSTk Example 1

rsync to EXFAT drive

These rsync options are useful to sync with an ExFAT drive:

rsync -vrltD --progress --stats /source/a/ /dest/a
-vrltD
options from -a friendly with EXFAT.

The standard rsync options like:

rsync -a

don’t work with an EXFAT drive because EXFAT doesn’t understand permissions, owners, or groups.

rsync: mkstemp … failed: Function not implemented (38)


Per-file progress:

rsync -av --progress

Overall progress:

rsync -a --info=progress2

Markdown reference for Jekyll websites

Related: Create fast Jekyll GitHub Pages website


Having a good website template helps your website performance substantially (> 10%) by speeding up the site and making it more mobile-friendly. Mobile-friendly websites have been important (including for low-bandwidth users on any device) and feasible since the late 1990s with CSS. As Google has moved to a mobile-first web crawler, the important of being mobile-friendly is of critical importance for virtually all websites. Using appropriate layout elements in the page help your visitors and search engines understand the content better, boosting engagement rates and SERPP (search engine results page position).

The Wordpress Markdown syntax reference applies equally well to Jekyll and other website renderers that accept Markdown input. Almost 100% of website presenation can be done with Markdown.

Only use HTML directly to draw text arrows ↔, give detailed control of a few percent of images, and highlight text (e.g. red warning text).

Liquid generates almost all figures, table of contents, and image galleries.

HTML5 math character: Math characters directly in HTML5 Not all of these work in Jekyll Markdown, test them in the local preview first.

Speed of Matlab vs Python vs Julia vs IDL

The Benchmarks Game uses deep expert optimizations to exploit every advantage of each language. The benchmarks I’ve adapted from the Julia micro-benchmarks are done in the way a general scientist or engineer competent in the language, but not an advanced expert in the language would write them. Emphasis is on keeping the benchmarks written with priority on simplicity and length, where programmer time is far more important than CPU time. Jules Kouatchou runs benchmarks on massive clusters comparing Julia, Python, Fortran, etc. A prime purpose of these benchmarks is given ease of programming for a canonical task (say Mandelbrot), which languages have distinct runtime performance benefits.

Julia’s growing advantage is the performance of compiled languages with the relative ease of a scripted language. The majority of analysts scripting in engineering and science are working in Python, with Matlab in second place. The stable Julia 1.0 release finally brings the promise of API stability that was an adoption blocker in earlier Julia releases. Julia allows abstract expression of formulas, ideas, and arrays in ways not feasible in other major analysis applications. This allows advanced analysts unique, performant capabilities with Julia. Since Julia is readily called from Python, Julia work can be exploited from more popular packages.

Python often is “close enough” in performance to compiled languages like Fortran and C, by virtue of numeric libraries Numpy, Numba and the like. For particular tasks, Tensorflow, OpenCV, and directly loading Fortran libraries with f2py or ctypes minimizes Python’s performance penalty. This was not the case when Julia was conceived in 2009 and first released in 2012. Thanks to Anaconda, Intel MKL and PyCUDA, momentum and performance are solidly behind Python for scientific and engineering computing for the next several years at least.

Cython has Python-like syntax that is compiled to .c code that is much larger than the original Python code and isn’t very readable. However, substantial speed increases can result. Don’t convert the entire program to Cython! Just the slow functions.

PyPy does sophisticated analysis of Python code and can also offer massive speedups, without changes to existing code.


We have created a multi-code language benchmark suite. Fortran is comparable to Python with MKL, Matlab, Julia. With single-precision float, Python Cuda can be 1000+ times faster than Python, Matlab, Julia, and Fortran. However, the usual “price” of GPUs is the slow I/O. If large arrays need to be moved constantly on and off the GPU, special strategies may be necessary to get a speed advantage. For iterative algorithms, it’s worthwhile to use Numba or Cython with Python, to get Fortran-like speeds from Python, comparable with Matlab at the given test.

L3 Harris Geospatial IDL is used mostly by astronomers. IDL can be replaced by GDL, the free open-source IDL-compatible program. A better choice would be to move from IDL/GDL to Python or Julia in many cases.

Pi Machin benchmark


Related: Anaconda Accelerate: GPU from Python / Numba

Open source video / audio codecs

Open source codecs are important to everyone as license fees erode the ability to provide free content without some form of subscription or sponsorship, which can lead to filtering of content. Here are a couple updates for the end of 2018.

In the early 2000’s, Speex by Xiph.org (now making Opus) was great for dialup modem streaming of voice content. The Opus codec effectively replaces Speex and the latest Opus release further enhances performance.

For FFmpeg compiled with libopus support, encoding Opus audio with FFmpeg is like:

ffmpeg -i voice.wav -ac 1 -cutoff 4000 -b:a 6000 -ar 8000 -vbr off -frame_duration 60 -application voip voice.opus

YouTube supports AV1.