Scientific Computing

Troubleshooting DNS problems

If one suspects a website has been compromised, don’t use a standard web browser to access the site as there could be zero-day malware on the site. Consider Terminal programs that don’t have JavaScript enabled like curl or lynx if necessary to browse the site, preferably from a VM or other isolated computing resource. These programs are also not immune from security vulnerabilities.

DNSViz helps visualize the DNS chain. Keep in mind DNS and nameserver updates can take minutes to hours to propagate.

macOS:

dscacheutil -q host -a name host.invalid

Linux / macOS / WSL:

dig +trace host.invalid

If the DNS entries seem valid, consider that the web hosting server (that sends the HTML files to browser) may be compromised.

Compile Matlab .m code executable

Matlab Compiler compiles existing .m Matlab script to run as an executable on another PC without Matlab. Matlab Compiler Runtime MCR is used on computers that don’t have Matlab to run the compiled Matlab code.

Caveats:

  • Matlab Compiler does not in general speedup Matlab code execution
  • in general, compiled binaries might be disassembled to reverse-engineer the underlying code
  • MCR version on each computer running the executable must match the Matlab version of the compiling Matlab, and the compiling computer must have the same operating system as the MCR running computers.

Compiling computer: ensure Matlab Compiler is installed:

assert(license('test', 'compiler') == 1)

Example program “mymcc.m”:

function Y = mymcc()

X = 0:0.01:2*3.14;
Y = sin(X);
plot(X,Y)
title('Test of MCR')
xlabel('x')
ylabel('y')
disp('I ran an MCR program!')

end

Compile “.m” file in Matlab:

mcc -m -v mymcc.m

Run compiled Matlab program:

./run_mymcc.sh mymcc

I ran an MCR program!

and show a Matlab plot window showing a sine wave. Close the plot window to end the execution of your program.


Notes:

Reference

GNU Octave does not currently have the ability to compile “.m” files. Octave mkoctfile is to distribute C / C++ code that calls Octave functions–and ABI-compatible Octave must be installed on the user computers

Using Intel oneAPI and MKL with CMake

There can be substantial speed boosts from Intel compilers with Intel CPUs. Intel oneAPI gives advanced debuggers and performance measurements. Intel oneMKL can give a significant speed boost to ABI-compatible compilers for certain math operations.

For Windows, use the oneAPI Command Prompt. Otherwise, specify environment variables CC, CXX, FC to indicate desired compilers via script:

Build with CMake:

cmake -B build

cmake --build build

Example CMakeLists.txt

To see the compiler commands CMake is issuing, use

cmake --build build -v

Refer to Intel Link Advisor for supported compiler / operating system / MKL combinations.


Get runtime confirmation that MKL is being used via MKL_VERBOSE.

  • Linux:

    MKL_VERBOSE=1 ./mytest
  • Windows

    set MKL_VERBOSE=1
    mytest.exe

That gives verbose text output upon use of MKL functions. That runtime option does slow down MKL performance, so normally we don’t use it.

GUI viewers for HDF5 / NetCDF4 data

HDF5 is a popular data container format, a filesystem within a file. Many programs supporting HDF5 like Matlab can read and plot data. It is useful to have a standalone simple data browser like HDFview.

HDFview from the HDF Group can read HDF5, NetCDF4, and FITS. HDFview enables editing (writing) as well as reading HDF5. One can simply download the HDFview binaries, or use package managers:

  • Linux: apt install hdfview
  • macOS: brew install hdfview

ViTables is a Python-based HDF5 GUI.


The Java-based PanoplyJ is available for macOS, Linux and Windows.

CMake compiler flag precedence

To ensure the user set compile flags take precedence by appearing later in the command line vs. CMake’s automatic flags, use add_compile_options to set language-specific or project-wide compiler options. target_compile_options is similar, but applies to only one target.

The generator expression $<$<COMPILE_LANGUAGE:C>:...> is used to only add the flag for the C language. This is important as many compiler options are language-specific, particularly for C / C++ and Fortran projects.

If one truly needs to wipe out all CMake automatic flags, try settingCMAKE_FLAGS variable to an empty string.

Git default text editor

Git uses the EDITOR environment variable to determine which text editor to use for commit messages. Since the commit message editing is typically small and simple, it may be desired to set a distinct text editor just for Git. This is done via Git global config:

git config --global core.editor "nano"

CMake environment variable names with special characters

Most environment variable have alphanumeric names and don’t need any special consideration to access. On Windows, some important programs still use the “Program Files (x86)” directory, denoted by environment variable “ProgramFiles(x86)”.

cmake_minimum_required(VERSION 3.10)

set(px $ENV{ProgramFiles\(x86\)})

message(STATUS "${px}")

Most other code languages don’t have any particular issues using environment variables named with special characters.

All of the following print like:

C:\Program Files (x86)

Matlab:

getenv("ProgramFiles(x86)")

Python:

python -c "import os; print(os.environ['ProgramFiles(x86)'])"

C++:

#include <cstdlib>
#include <iostream>

int main()
{
    std::cout << std::getenv("ProgramFiles(x86)") << "\n";
    return EXIT_SUCCESS;
}

Fortran:

program env

implicit none

integer :: i
character(100) :: path

call get_environment_variable('ProgramFiles(x86)', path, status=i)
if (i/=0) error stop "env var ProgramFiles(x86) not found"

print '(a)', path

end program env

Matplotlib datetime tick labels

Matplotlib plots with datetime axes can benefit from rotating axes tick labels or concise tick labels to avoid overlapping text.

Example code used in this post with synthetic data:

from matplotlib.pyplot import Figure
import matplotlib.dates as mdates
from datetime import datetime, timedelta

def datetime_range(start: datetime, end: datetime, step: timedelta) -> list[datetime]:
    """like range() for datetime"""
    return [start + i * step for i in range((end - start) // step)]

t = datetime_range(datetime(2021, 1, 1), datetime(2021, 1, 2), timedelta(minutes=30))
y = range(len(t))

Rotate datetime tick labels

If rotating tick labels, the overall axes typically need to be positioned to allow for the rotated labels, otherwise the tick labels can be cut off the figure edges. The axes position is updated automatically with constrained_layout option of figure().

import matplotlib.pyplot as plt

fg = plt.figure(layout='constrained')
ax = fg.gca()

ax.plot(t, y)
ax.set_xlabel('time')
ax.set_ylabel('y')
ax.tick_params(axis="x", labelrotation=30)  # arbitrary rotation amount

plt.show()

Matplotlib date formatting

Matplotlib datetime axes have numerous formatting options. Here we show the ConciseFormatter, which may avoid the need to rotate tick labels.

import matplotlib.pyplot as plt

fg = plt.figure(layout='constrained')
ax = fg.gca()

ax.plot(t, y)
ax.set_xlabel('time')
ax.set_ylabel('y')

ax.xaxis.set_major_formatter(
    mdates.ConciseDateFormatter(ax.xaxis.get_major_locator()))

plt.show()

Diagnose silent quit program DLL conflict Windows

If a DLL conflicts with a programs needed DLLs, the program may quit with a specific message, or it may silently exit. The return code may correspond to segfault or other error. To help see if a DLL conflict is occurring, use gdb to run the program. This works even for general programs that weren’t built on the system. We suggest obtaining GDB via MSYS2. If there is a problem with a DLL, GDB will often print the name of the DLL. If the DLL is in an unexpected location, this may indicate a directory that should not be in environment variable Path.

Start GDB Fortran debugger: assuming executable myprog

gdb myprog.exe

Run program from (gdb) prompt:

r

CMake CppCheck static code checks

CMake has built-in support for C/C++ static code analysis tools such as CppCheck. Apply CppCheck to CMake targets with CMakeLists.txt by setting CMAKE_CXX_CPPCHECK.

File “cppcheck.supp” contains suppressions for false positives. NOTE: CMake runs cppcheck from an arbitrary directory, so per-file suppressions in the file don’t work as usual. To suppress a warning for a specific file, use the --suppress option to cppcheck in CMakeLists.txt like:

set_property(TARGET MyTarget PROPERTY CXX_CPPCHECK "${CMAKE_CXX_CPPCHECK};--suppress=containerOutOfBounds")

Don’t just blindly modify code based on CppCheck output. Like any code analysis tool there are false positives and false negatives.