Introduction
1. Installing QMCkl
The latest version of QMCkl can be downloaded here, and the source code is accessible on the GitHub repository.
1.1. Installing from the released tarball (for end users)
QMCkl is built with GNU Autotools, so the usual
configure ; make ; make check ; make install scheme applies.
The C compiler can be specified with the CC variable and the Fortran
compiler with the FC variable; compiler options are given in CFLAGS and
FCFLAGS.
1.2. Installing from the source repository (for developers)
To compile from the source repository, additional dependencies are required to generate the source files:
- Emacs >= 26
- Autotools
- Python3
When the repository is downloaded, neither the configure script nor the
Makefile exists yet, and ./autogen.sh has to be run first.
2. Using QMCkl
Two files are installed in ${prefix}/include. The qmckl.h header has to
be included in C codes that use QMCkl functions:
#include "qmckl.h"
The qmckl_f.F90 file declares interfaces to the same functions. It has to be
compiled together with the Fortran codes, which then load the module with
use qmckl
3. Developing in QMCkl
3.1. Literate programming
In a traditional source code, most of the lines of source files of a program are code, scripts, Makefiles, and only a few lines are comments explaining parts of the code that are non-trivial to understand. The documentation of the program is usually written in a separate directory, and is often outdated compared to the code.
Literate programming is a different approach to programming, where the program is considered as a publishable-quality document. Most of the lines of the source files are text, mathematical formulas, tables, figures, etc, and the lines of code are just the translation in a computer language of the ideas and algorithms expressed in the text. More importantly, the "document" is structured like a text document with sections, subsections, a bibliography, a table of contents etc, and the place where pieces of code appear are the places where they should belong for the reader to understand the logic of the program, not the places where the compiler expects to find them. Both the publishable-quality document and the binary executable are produced from the same source files.
Literate programming is particularly well adapted in this context, as the central part of this project is the documentation of an API. The implementation of the algorithms is just an expression of the algorithms in a language that can be compiled, so that the correctness of the algorithms can be tested.
We have chosen to write the source files in
org-mode format.
Conversions of the org-mode files produce the HTML and text documentation.
The source code is extracted from the same org-mode files by invoking the
Emacs text editor from the command-line in the Makefile, and the produced
files are then compiled. Within the Emacs text editor, the source code
blocks can also be executed interactively, in the same spirit as Jupyter
notebooks.
Note that Emacs is not needed for end users because the distributed tarball contains the generated source code.
3.2. Source code editing
For a tutorial on literate programming with org-mode, follow this link.
Any text editor can be used to edit org-mode files. Emacs is recommended for a better user experience. For users who dislike Emacs, it is good to know that Emacs can behave like Vim when switched into ``Evil'' mode.
The tools/init.el file provides a minimal Emacs configuration for vim
users; copy it to ~/.emacs.d/init.el.
For users who prefer Jupyter notebooks, the tools/nb_to_org.sh script
converts a notebook into an org-mode file.
Note that pandoc can be used to convert multiple markdown formats into org-mode.
3.3. Choice of the programming language
Most of the codes of the TREX CoE are written in Fortran, with some scripts in Bash and Python. Outside the CoE, Fortran is still important in QMC codes (Casino, Amolqc), and the community also uses C and C++ (QMCPack, QWalk), with Julia and Rust gaining in popularity. We want QMCkl to be compatible with all of these languages, so the QMCkl API has to be compatible with C, since a library with a C-compatible API can be used from every other language.
High-performance versions of QMCkl, with the same API, can be rewritten by HPC experts. These optimized libraries will be tuned for specific architectures, among which we can cite x86 based processors and GPU accelerators. Nowadays, the software tools that take advantage of low-level features (intrinsics, prefetching, aligned or pinned memory allocation, …) are aimed at C++ developers. Optimized implementations will probably be written in C++, but as the API is C-compatible this poses no problem for linking the library into other languages.
Fortran is one of the most common languages in the community, and is simple enough to make the algorithms readable both by experts in QMC and by experts in HPC. We therefore use Fortran in this pedagogical implementation of QMCkl to express the QMC algorithms. For internal functions related to system programming, however, C is more natural than Fortran.
As QMCkl appears as a C library, each Fortran function has an
iso_c_binding interface that makes it callable from C, and it is this C
interface that is exposed to the user. Fortran users therefore never call
the Fortran routines directly: they call the C binding function, which
still requires iso_c_binding:
The name of the Fortran source files ends with _f.F90, so that the
Makefile handles them and their object files (*.o) do not collide
with those of the C sources. The functions implemented in Fortran carry
the same names as their C declarations, and are called from C through
bind(C). A C function that needs a Fortran-specific interface,
with pointers instead of values or with strings, takes the _f suffix
and stays in the private headers, as qmckl_probe_f does for
qmckl_probe.
For more guidelines on using Fortran to generate a C interface, see this link.
3.4. Coding rules
The authors should follow the recommendations of the C99 SEI+CERT C Coding Standard.
Compliance can be checked with cppcheck; make cppcheck runs it on the
tangled sources in src/ and writes its output to cppcheck.out:
3.5. Design of the library
The proposed API should allow the library to deal with memory transfers between CPU and accelerators, and to use different levels of floating-point precision. We chose a multi-layered design with low-level and high-level functions (see below).
3.6. Naming conventions
To avoid namespace collisions, we use qmckl_ as a prefix for all exported
functions and variables. All exported header files have a file name
prefixed with qmckl_.
If the name of the org-mode file is xxx.org, the name of the produced C
file is xxx.c, the produced headers are xxx_type.h and xxx_func.h,
and the produced Fortran file is xxx_f.F90.
In the names of the variables and functions, only the singular form is allowed.
3.7. Application programming interface
In the C language, the number of bits used by the integer types can change
from one architecture to another one. To circumvent this problem, we choose
to use the integer types defined in <stdint.h>, where the number of bits
used for the integers is fixed.
To ensure that the library will be easily usable in any other language than C, we restrict the data types in the interfaces to the following:
- 32-bit and 64-bit integers, scalars and arrays (
int32_tandint64_t) - 32-bit and 64-bit floats, scalars and arrays (
floatanddouble) - pointers, always cast into 64-bit integers, even on legacy 32-bit architectures
- ASCII strings, represented as pointers to character arrays terminated by
a
'\0'character (C convention) - complex numbers, represented by an array of 2 floats
- booleans, passed as the C99
boolof<stdbool.h>, which the Fortran side declares aslogical(c_bool) - floating point variables are
doubleby default, unless explicitly mentioned otherwise - integers used for counting are always
int64_t
To facilitate the use from other languages than C, we will provide bindings in other repositories.
3.8. Global state
Global variables should be avoided in the library, because a single
program may need to use multiple instances of the library. Instead, the
library builds a pointer to a context variable with the
qmckl_context_create function. The context holds the global state of
the library, and is the first argument of many QMCkl functions.
The internal structure of the context is not specified, to leave a maximum
of freedom to the different implementations. Modifying the state is done by
setters and getters, prefixed by qmckl_set_ and qmckl_get_.
3.9. Headers
A single qmckl.h header to be distributed by the library
is built by concatenating some of the produced header files.
To facilitate building the qmckl.h file, we separate types from
function declarations in headers. Types should be defined in header
files suffixed by _type.h, and functions in files suffixed by
_func.h.
As these files will be concatenated in a single file, they should
not be guarded by #ifndef *_H, and they should not include other
produced headers.
Some particular types that are not exported need to be known by the
context, and there are some functions to update instances of these
types contained inside the context. For example, a
qmckl_numprec_struct is present in the context, and the function
qmckl_set_numprec_range takes a context as a parameter and sets a
value in the qmckl_numprec_struct contained in the context.
Because of these intricate dependencies, a private header is
created, containing the qmckl_numprec_struct. This header is
included in the private header file which defines the type of the
context. Header files for private types are suffixed by _private_type.h
and header files for private functions are suffixed by _private_func.h.
Fortran interfaces are written in the *_fh_func.F90 file, and the type
definitions in the *_fh_type.F90 file.
| File | Scope | Description |
|---|---|---|
*_type.h |
Public | Type definitions |
*_func.h |
Public | Function definitions |
*_private_type.h |
Private | Type definitions |
*_private_func.h |
Private | Function definitions |
*_fh_type.F90 |
Public | Fortran type definitions |
*_fh_func.F90 |
Public | Fortran function definitions |
3.10. Low-level functions
Low-level functions are the leaves of the function call tree: they don't call any other QMCkl function.
These functions are pure: their output depends only on their arguments, and they do not modify the state of the library. They are not allowed to allocate or deallocate memory; if they need temporary memory, it is provided by the caller.
3.11. High-level functions
High-level functions are at the top of the function call tree. They choose which lower-level function to call depending on the required precision, and do the corresponding type conversions. They are also responsible for allocating temporary storage, to simplify the use of accelerators.
3.12. Numerical precision
The minimal number of bits of precision required for a function should be
given as an input of low-level computational functions. This input sets the
thresholds used to avoid computing unnecessary noise, and high-level
functions read the precision from the context variable.
In order to automatize numerical accuracy tests, QMCkl uses Verificarlo and its CI functionality. You can read Verificarlo CI's documentation at the following link. Reading it is advised to understand the remainder of this section.
To enable support for Verificarlo CI tests when building the library, use the following configure command:
Note that this requires an installation of Verificarlo with Fortran
support. The C sources are instrumented by verificarlo-c and the Fortran
sources by verificarlo-f. Enabling the support defines the VFC_CI
preprocessor variable, whose use is explained below.
As explained in the documentation, Verificarlo CI uses a probes system to
export variables from test programs to the tools themselves. To make tests
easier to use, QMCkl has its own interface to the probes system. These
functions are always defined, but behave differently depending on the
VFC_CI preprocessor variable. There are 3 functions at your disposal.
With the CI enabled, they register the probe with Verificarlo (vfc_probe,
vfc_probe_check or vfc_probe_check_relative). Otherwise, they either do
nothing or perform a check on the tested value and return its result as a
boolean that you are free to use or ignore.
Here are these 3 functions:
qmckl_probe: place a normal probe without any check. WhenVFC_CIis disabled, it does nothing and returnsfalse.qmckl_probe_check: place a probe with an absolute check. WhenVFC_CIis disabled, it performs the check |val - ref| < accuracy target, and returnstrueif the check fails,falseotherwise.qmckl_probe_check_relative: place a probe with a relative check. WhenVFC_CIis disabled, it performs the check |val - ref| / |ref| < accuracy target, and returnstrueif the check fails,falseotherwise.
The functions, their Fortran interfaces and the automatic creation and dump of the probes are documented on the Verificarlo CI page.
Finally, if you need to add a QMCkl kernel to the CI tests or modify an existing one, pay attention to the following points:
- add the new kernel to the
vfc_tests_config.jsonfile, which controls the backends and repetitions for each executable. More details can be found in thevfc_cidocumentation. - to call the probe functions from Fortran, import the
qmckl_verificarlo_fmodule. - if your tests include asserts that rely on accurate FP values, wrap them
inside a
#ifndef VFC_CIstatement, as the asserts would otherwise risk failing when executed with the different Verificarlo backends.
3.13. Algorithms
Reducing the scaling of an algorithm usually implies also reducing its arithmetic complexity (number of flops per byte). Therefore, for small sizes and algorithms are better adapted than linear scaling algorithms. As QMCkl is a general purpose library, several algorithms should be implemented and adapted to different problem sizes.