### Build and install a complete library set Source: https://libxsmm.readthedocs.io/ Build and install both static and shared libraries by running the install target twice with appropriate flags. ```bash make PREFIX=/path/to/libxsmm-install STATIC=0 install make PREFIX=/path/to/libxsmm-install install ``` -------------------------------- ### Build SPECFEM example for default CPU host Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Compiles the SPECFEM example for a default CPU host. ```bash cd sample/specfem make ``` -------------------------------- ### Build and run Hello LIBXSMM with Bazelisk Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Builds and runs the Hello LIBXSMM example using Bazelisk, which boot-straps Bazel. The build command compiles the example and the resulting binary is executed. ```bash cd /path/to/libxsmm/samples/hello bazelisk build //... ./bazel-bin/hello ``` -------------------------------- ### Install LIBXSMM into workstation sub-directory Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Installs libraries into a sub-directory named workstation/. ```bash make MNK="5 25" ALPHA=1 BETA=0 PRECISION=1 OPT=3 AVX=1 \ OFFLOAD=1 KNC=1 \ PREFIX=workstation/ install-minimal ``` -------------------------------- ### Set environment variables and run torch_example.py Source: https://libxsmm.readthedocs.io/libxsmm_samples Commands to set LD_LIBRARY_PATH, OMP_NUM_THREADS, and KMP_AFFINITY, then run the PyTorch example. The example compares nn.Conv1d with the optimized Conv1dOpti layer. ```bash export LD_LIBRARY_PATH={LIBXSMM_ROOT/lib} # Set LD_LIBRARY_PATH export OMP_NUM_THREADS=28 # Set number of threads export KMP_AFFINITY=compact,1,0,granularity=fine # Set KMP affinity python torch_example.py # Run the pytorch example ``` -------------------------------- ### Install LIBXSMM with default options Source: https://libxsmm.readthedocs.io/en/latest Build and install a complete set of libraries where the generated interface matches both static and shared libraries. This is the recommended command for a general package. ```makefile make PREFIX=/path/to/libxsmm-install STATIC=0 install make PREFIX=/path/to/libxsmm-install install ``` -------------------------------- ### Install LIBXSMM to a specific location Source: https://libxsmm.readthedocs.io/ Install the library to a custom directory using the PREFIX variable. ```bash make MNK="1 2 3 4 5" PREFIX=/path/to/libxsmm-install install ``` -------------------------------- ### Run Conv1dOpti example Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Environment configuration and execution commands for running the torch_example.py script. ```bash export LD_LIBRARY_PATH={LIBXSMM_ROOT/lib} # Set LD_LIBRARY_PATH export OMP_NUM_THREADS=28 # Set number of threads export KMP_AFFINITY=compact,1,0,granularity=fine # Set KMP affinity python torch_example.py # Run the pytorch example ``` -------------------------------- ### Run multi-threaded specfem example on Haswell Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Run the multi-threaded example from the sample/specfem directory. The command changes to that directory, builds with make OPT=3, and runs with OMP_NUM_THREADS=24. ```bash cd sample/specfem make OPT=3; OMP_NUM_THREADS=24 ./specfem.sh ``` -------------------------------- ### Install LIBXSMM with PREFIX Source: https://libxsmm.readthedocs.io/en/latest Install LIBXSMM to a specific location using PREFIX. The PREFIX and DESTDIR variables are equivalent and can be relative or absolute paths. ```makefile make MNK="1 2 3 4 5" PREFIX=/path/to/libxsmm-install install ``` -------------------------------- ### Run multi-threaded specfem example on Knights Corner Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Run the multi-threaded example from the sample/specfem directory. The command changes to that directory, builds with make FCFLAGS="-O3 -fopenmp -warn" OPT=3 KNC=1, and runs with ./specfem.sh -mic. ```bash cd sample/specfem make FCFLAGS="-O3 -fopenmp -warn" OPT=3 KNC=1; ./specfem.sh -mic ``` -------------------------------- ### Build LIBXSMM on FreeBSD with gmake Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_compat Build LIBXSMM under FreeBSD using gmake. For libxsmmext, install OpenMP first with 'sudo pkg install openmp'. Tests may require a LAPACK/BLAS installation unless BLAS=0 is given. ```bash bash gmake ``` -------------------------------- ### Build SPECFEM example for Knights Corner (KNC) Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Compiles the SPECFEM example for Knights Corner. ```bash cd sample/specfem make KNC=1 ``` -------------------------------- ### Build and run Hello LIBXSMM with GNU Make Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Builds and runs the Hello LIBXSMM example using GNU Make. First builds the library, then the sample, and finally runs the resulting executable. ```bash cd /path/to/libxsmm make cd /path/to/libxsmm/samples/hello make ./hello ``` -------------------------------- ### Build SPECFEM for Knights Corner in two steps Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Two-step build for Knights Corner: first build the library, then the SPECFEM example. ```bash make MNK="5 25" ALPHA=1 BETA=0 PRECISION=1 OPT=3 AVX=1 OFFLOAD=1 KNC=1 make OPT=3 specfem_mic ``` -------------------------------- ### Generator driver examples Source: https://libxsmm.readthedocs.io/libxsmm_be Three example invocations of the generator driver program, each with different arguments for dense, dense_asm, and sparse kernels. ```bash bin/libxsmm_gemm_generator dense foo.c foo 16 16 16 32 32 32 1 1 1 1 hsw nopf DP ``` ```bash bin/libxsmm_gemm_generator dense_asm foo.c foo 16 16 16 32 32 32 1 1 1 1 knl AL2_BL2viaC DP ``` ```bash bin/libxsmm_gemm_generator sparse foo.c foo 16 16 16 32 0 32 1 1 1 1 hsw nopf DP bar.csc ``` -------------------------------- ### Build SPECFEM example for Knights Corner via two steps Source: https://libxsmm.readthedocs.io/libxsmm_samples Two-step build for Knights Corner: first build LIBXSMM with OFFLOAD=1 KNC=1, then build the specfem_mic target. ```bash make MNK="5 25" ALPHA=1 BETA=0 PRECISION=1 OPT=3 AVX=1 OFFLOAD=1 KNC=1 make OPT=3 specfem_mic ``` -------------------------------- ### Run single-threaded specfem example on Sandy Bridge Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Run the single-threaded example from the sample/specfem directory. The command changes to that directory, builds with make, and runs with OMP_NUM_THREADS=1. ```bash cd sample/specfem make; OMP_NUM_THREADS=1 ./specfem.sh ``` -------------------------------- ### Install MinGW dependencies with pacman Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_compat Installs required packages for building LIBXSMM in the native MinGW environment using MSYS2. The command installs make, python, diffutils, GCC, GCC Fortran, and OpenBLAS. ```bash pacman -S msys/make msys/python msys/diffutils \ mingw64/mingw-w64-x86_64-gcc mingw64/mingw-w64-x86_64-gcc-fortran \ mingw64/mingw-w64-x86_64-openblas ``` -------------------------------- ### Install Conv1dOpti extension Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Commands to install the optimized 1D dilated convolutional layer package within an environment containing PyTorch. ```bash conda activate environment # Activate anaconda or virtual environment containing PyTorch cd Conv1dOpti-extension/ python setup.py install # Install package cd .. ``` -------------------------------- ### Install Conv1dOpti extension Source: https://libxsmm.readthedocs.io/libxsmm_samples Commands to activate an environment and install the Conv1dOpti package. Requires PyTorch installed in an anaconda or virtual environment and GCC version 8.3.0 or higher. ```bash conda activate environment # Activate anaconda or virtual environment containing PyTorch cd Conv1dOpti-extension/ python setup.py install # Install package cd .. ``` -------------------------------- ### Hello LIBXSMM: C++ matrix multiplication kernel Source: https://libxsmm.readthedocs.io/en/latest C++ example that generates and dispatches a matrix multiplication kernel using the libxsmm_mmfunction template. Build with 'make STATIC=0', compile with 'g++ -I/path/to/libxsmm/include hello.cpp -L/path/to/libxsmm/lib -lxsmm -lblas -o hello', and run with 'LD_LIBRARY_PATH=/path/to/libxsmm/lib LIBXSMM_VERBOSE=2 ./hello'. The kernel computes C += Ai * Bi for a batch of matrices. ```cpp #include #include int main(int argc, char* argv[]) { typedef double T; int batchsize = 1000, m = 13, n = 5, k = 7; std::vector a(batchsize * m * k), b(batchsize * k * n), c(m * n, 0); /* C/C++ and Fortran interfaces are available */ typedef libxsmm_mmfunction kernel_type; /* generates and dispatches a matrix multiplication kernel (C++ functor) */ kernel_type kernel(LIBXSMM_GEMM_FLAG_NONE, m, n, k, 1.0 /*alpha*/, 1.0 /*beta*/); assert(kernel); for (int i = 0; i < batchsize; ++i) { /* initialize input */ for (int ki = 0; ki < k; ++ki) { for (int j = 0; j < m; ++j) a[i * j * ki] = static_cast(1) / ((i + j + ki) % 25); for (int j = 0; j < n; ++j) b[i * j * ki] = static_cast(7) / ((i + j + ki) % 75); } } /* kernel multiplies and accumulates matrices: C += Ai * Bi */ for (int i = 0; i < batchsize; ++i) kernel(&a[i * m * k], &b[i * k * n], &c[0]); } ``` -------------------------------- ### SMM Benchmark Sample Output Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Example output from running the specialized benchmark script with argument 0, showing performance metrics for the batched (A,B,C) streaming case. The output includes pseudo-performance in FLOPS/cycle, performance in GFLOPS/s, memory bandwidth, and duration. ```bash $ ./specialized.sh 0 m=32 n=32 k=32 size=87381 memory=2048.0 MB (DP) Batched (A,B,C)... pseudo-perf.: 10.7 FLOPS/cycle performance: 23.9 GFLOPS/s bandwidth: 11.1 GB/s duration: 239 ms Finished ``` -------------------------------- ### Example of verbose test output Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_valid Shows the output format when LIBXSMM_VERBOSE is set to 4 or higher, displaying the command used for a test. ```text [...] LIBXSMM_TARGET: hsw Registry and code: 13 MB + 8 KB (meltw=1) Command: ./eltwise_binary_simple 1 0 F32 F32 F32 F32 10 10 10 10 [...] ``` -------------------------------- ### Run SpecFEM single-threaded (Haswell) Source: https://libxsmm.readthedocs.io/libxsmm_samples Runs the SpecFEM example with a single thread after building it. The output shows timing results for different loop implementations. ```bash cd sample/specfem make; OMP_NUM_THREADS=1 ./specfem.sh ``` ```text =============================================================== average over 15 repetitions timing with Deville loops = 0.1028 timing with unrolled loops = 0.1385 / speedup = -34.73 % timing with LIBXSMM dispatch = 0.1408 / speedup = -37.02 % timing with LIBXSMM prefetch = 0.1327 / speedup = -29.07 % timing with LIBXSMM static = 0.1151 / speedup = -11.93 % =============================================================== ``` -------------------------------- ### Initialize and Finalize LIBXSMM Library Source: https://libxsmm.readthedocs.io/en/latest Call libxsmm_init() to pay setup cost at a specific point and avoid lazy initialization overhead. Optionally call libxsmm_finalize() to de-initialize the library and free internal memory; the library also deallocates resources at program exit. ```c /** Initialize the library; pay for setup cost at a specific point. */ void libxsmm_init(void); /** De-initialize the library and free internal memory (optional). */ void libxsmm_finalize(void); ``` -------------------------------- ### Set PKG_CONFIG_PATH and query libxsmm link flags Source: https://libxsmm.readthedocs.io/en/latest Use pkg-config to ease linking with LIBXSMM. Set PKG_CONFIG_PATH to the lib directory and run pkg-config libxsmm --libs to get the necessary link flags. ```bash export PKG_CONFIG_PATH=/path/to/libxsmm/lib pkg-config libxsmm --libs ``` -------------------------------- ### Static Specialization with M, N, K Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_tune Builds LIBXSMM with statically specialized matrix multiplication kernels for the given M, N, and K values. The example generates a set of (M,N,K) triplets in a loop-nest relationship. ```make make M="2 4" N="1" K="$(echo $(seq 2 5))" ``` -------------------------------- ### Run SpecFEM multi-threaded on Knights Corner (KNC=1) Source: https://libxsmm.readthedocs.io/libxsmm_samples Runs the SpecFEM example on Knights Corner with 236 threads using the -mic flag. The output shows OpenMP thread count and timing results. ```bash cd sample/specfem make FCFLAGS="-O3 -fopenmp -warn" OPT=3 KNC=1; ./specfem.sh -mic ``` ```text OpenMP information: number of threads = 236 [...] =============================================================== average over 15 repetitions timing with Deville loops = 0.0164 timing with unrolled loops = 0.6982 / speedup = -4162.10 % timing with LIBXSMM dispatch = 0.0170 / speedup = -3.89 % timing with LIBXSMM static = 0.0149 / speedup = 9.22 % =============================================================== ``` -------------------------------- ### Fortran GEMM calls (libxsmm_?gemm and libxsmm_gemm) Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_mm Fortran examples for dense matrix multiplication. The first uses the type-prefixed interface (single/double precision), the second uses the generic interface. Both pass m, n, and k by value. ```fortran ! Dense matrix multiplication (single/double-precision). CALL libxsmm_?gemm(m=m, n=n, k=k, a=a, b=b, c=c) ! Dense matrix multiplication (generic interface). CALL libxsmm_gemm(m=m, n=n, k=k, a=a, b=b, c=c) ``` -------------------------------- ### Parallel execution with tool_pexec.sh Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_scripts Use this command to execute commands read from standard input concurrently. The example pipes a sequence of echo commands to tool_pexec.sh. The level of parallelism is automatic but can be adjusted. Logfiles can be written per command using -o, and environment variables with PEXEC_ prefix can control settings. ```bash seq 100 | xargs -I{} echo "echo \"{}\"" \ | tool_pexec.sh ``` -------------------------------- ### Run C sample with wrapper script Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Executes the C sample via the wrapper script with out-of-place transpose kind and matrix size 20000x20000. The output shows the matrix dimensions, leading dimensions, size in MB, bandwidth, and duration. ```bash $ ./transpose.sh o 20000 m=20000 n=20000 ldi=20000 ldo=20000 size=3052MB (double, out-of-place) bandwidth: 18.8 GB/s duration: 159 ms ``` -------------------------------- ### Build LIBXSMM with default settings (make STATIC=0 and make) Source: https://libxsmm.readthedocs.io/en/latest Generates the interface and builds the static library. By default, STATIC=1 is activated; use STATIC=0 to build shared libraries. Run either or both commands. ```bash make STATIC=0 make ``` -------------------------------- ### Build and compile libxsmm and samples Source: https://libxsmm.readthedocs.io/libxsmm_samples Commands to compile the LIBXSMM library and the magazine samples. Run from the project root, then inside the samples/magazine directory. ```shell cd libxsmm; make cd samples/magazine; make ``` -------------------------------- ### Run basic tests Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_valid Executes the basic test suite using make. ```bash make tests ``` -------------------------------- ### Verbosity Level Functions Source: https://libxsmm.readthedocs.io/libxsmm_aux Functions to get and set the verbosity level. ```APIDOC ## libxsmm_get_verbosity ### Description Returns the current verbosity level. ### Syntax ```c int libxsmm_get_verbosity(void); ``` ### Parameters None. ### Returns - **int** - The current verbosity level. ``` ```APIDOC ## libxsmm_set_verbosity ### Description Sets the verbosity level. The environment variable LIBXSMM_VERBOSE corresponds to this function. ### Syntax ```c void libxsmm_set_verbosity(int level); ``` ### Parameters - **level** (int) - Required - The verbosity level to set. ``` -------------------------------- ### Clean OpenTuner database Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Removes the OpenTuner database file to start a fresh tuning experiment. ```bash rm -rf opentuner.db ``` -------------------------------- ### libxsmm_ptr_dmm and libxsmm_ptr_smm: C-address of kernel handles Source: https://libxsmm.readthedocs.io/en/latest/fortran/sourcefile/libxsmm.f.html Functions to get C pointer to the handle of LIBXSMM_DMMFUNCTION and LIBXSMM_SMMFUNCTION arrays. ```Fortran FUNCTION libxsmm_ptr_dmm(a) TYPE(LIBXSMM_DMMFUNCTION), INTENT(IN), TARGET :: a(:) TYPE(LIBXSMM_DMMFUNCTION), POINTER :: p TYPE(C_PTR) :: libxsmm_ptr_dmm p => a(LBOUND(a,1)); libxsmm_ptr_dmm = C_LOC(p%handle) END FUNCTION FUNCTION libxsmm_ptr_smm(a) TYPE(LIBXSMM_SMMFUNCTION), INTENT(IN), TARGET :: a(:) TYPE(LIBXSMM_SMMFUNCTION), POINTER :: p TYPE(C_PTR) :: libxsmm_ptr_smm p => a(LBOUND(a,1)); libxsmm_ptr_smm = C_LOC(p%handle) END FUNCTION ``` -------------------------------- ### libxsmm_init() Source: https://libxsmm.readthedocs.io/en/latest/fortran/interface/libxsmm_init.html Initializes the LIBXSMM library. This subroutine must be called before using other LIBXSMM functions. ```APIDOC ## libxsmm_init() ### Description Initializes the LIBXSMM library. This subroutine must be called before using other LIBXSMM functions. ### Method subroutine ### Endpoint libxsmm_init ### Parameters #### Arguments _None_ ### Request Example ```fortran call libxsmm_init() ``` ### Response #### Success Response No return value. #### Response Example ``` (no output) ``` ``` -------------------------------- ### Basic dispatch and invoke pattern for TPP Source: https://libxsmm.readthedocs.io/libxsmm_tpp Shows the generic sequence: create operand shape, define a kernel function pointer, dispatch (JIT compile or retrieve), prepare parameters, and invoke. The placeholder `` must be replaced with the operation group. The parameter preparation step is incomplete (noted with `?.?`) and requires filling with actual parameters. ```c // Get the shape of the operands to build the right operand op_shape = libxsmm_create__shape(matrix dimensions, leading dimensions, data types); // Define a new kernel libxsmm_xsmmfunction kernel = {NULL}; // Dispatch (JIT compile or retrieve the previously compiled function) kernel. = libxsmm_dispatch_(op_shape, FLAGS); // Prepare the execution parameters libxsmm__param op_param; op_param.?.? = op parameters // Call the kernel kernel.(&op_param); ``` -------------------------------- ### libxsmm_ptr_b0/b1/b2: C-address of char arrays Source: https://libxsmm.readthedocs.io/en/latest/fortran/sourcefile/libxsmm.f.html Functions to get C pointer for CHARACTER(C_CHAR) arrays: scalar, 1D, and 2D. ```Fortran !> Determines the C-address of the given array. FUNCTION libxsmm_ptr_b0(a) CHARACTER(C_CHAR), INTENT(IN), TARGET :: a TYPE(C_PTR) :: libxsmm_ptr_b0 libxsmm_ptr_b0 = C_LOC(a) END FUNCTION FUNCTION libxsmm_ptr_b1(a) CHARACTER(C_CHAR), INTENT(IN), TARGET :: a(*) TYPE(C_PTR) :: libxsmm_ptr_b1 libxsmm_ptr_b1 = C_LOC(a) END FUNCTION FUNCTION libxsmm_ptr_b2(a) CHARACTER(C_CHAR), INTENT(IN) :: a(:,:) TYPE(C_PTR) :: libxsmm_ptr_b2 libxsmm_ptr_b2 = libxsmm_ptr_b1(a) END FUNCTION ``` -------------------------------- ### libxsmm_ptr_j0/j1/j2: C-address of int8 arrays Source: https://libxsmm.readthedocs.io/en/latest/fortran/sourcefile/libxsmm.f.html Functions to get C pointer for INTEGER(C_INT8_T) arrays: scalar, 1D, and 2D. ```Fortran !> Determines the C-address of the given array. FUNCTION libxsmm_ptr_j0(a) INTEGER(C_INT8_T), INTENT(IN), TARGET :: a TYPE(C_PTR) :: libxsmm_ptr_j0 libxsmm_ptr_j0 = C_LOC(a) END FUNCTION FUNCTION libxsmm_ptr_j1(a) INTEGER(C_INT8_T), INTENT(IN), TARGET :: a(*) TYPE(C_PTR) :: libxsmm_ptr_j1 libxsmm_ptr_j1 = C_LOC(a) END FUNCTION FUNCTION libxsmm_ptr_j2(a) INTEGER(C_INT8_T), INTENT(IN) :: a(:,:) TYPE(C_PTR) :: libxsmm_ptr_j2 libxsmm_ptr_j2 = libxsmm_ptr_j1(a) END FUNCTION ``` -------------------------------- ### Run C sample with manual OpenMP affinity Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Runs the C sample with manual OpenMP affinity settings and verbose output (LIBXSMM_VERBOSE=2). The output shows the same matrix parameters and performance metrics, plus a registry line indicating the tcopy kernel was used. ```bash LIBXSMM_VERBOSE=2 KMP_AFFINITY=balanced,granularity=fine,1 \ ./transpose o 20000 m=20000 n=20000 ldi=20000 ldo=20000 size=3052MB (double, out-of-place) bandwidth: 21.1 GB/s duration: 141 ms Registry: 20 MB (gemm=0 mcopy=0 tcopy=1) ``` -------------------------------- ### libxsmm_ptr_w0/w1/w2: C-address of short arrays Source: https://libxsmm.readthedocs.io/en/latest/fortran/sourcefile/libxsmm.f.html Functions to get C pointer for INTEGER(C_SHORT) arrays: scalar, 1D, and 2D. ```Fortran !> Determines the C-address of the given array. FUNCTION libxsmm_ptr_w0(a) INTEGER(C_SHORT), INTENT(IN), TARGET :: a TYPE(C_PTR) :: libxsmm_ptr_w0 libxsmm_ptr_w0 = C_LOC(a) END FUNCTION FUNCTION libxsmm_ptr_w1(a) INTEGER(C_SHORT), INTENT(IN), TARGET :: a(*) TYPE(C_PTR) :: libxsmm_ptr_w1 libxsmm_ptr_w1 = C_LOC(a) END FUNCTION FUNCTION libxsmm_ptr_w2(a) INTEGER(C_SHORT), INTENT(IN) :: a(:,:) TYPE(C_PTR) :: libxsmm_ptr_w2 libxsmm_ptr_w2 = libxsmm_ptr_w1(a) END FUNCTION ``` -------------------------------- ### Control verbosity level (libxsmm_get_verbosity, libxsmm_set_verbosity) Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_aux Get or set the verbosity level. The environment variable LIBXSMM_VERBOSE corresponds to libxsmm_set_verbosity. ```c int libxsmm_get_verbosity(void); void libxsmm_set_verbosity(int level); ``` -------------------------------- ### Build and run magazine benchmarks Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Builds the LIBXSMM library and the magazine sample, then runs the benchmark script. The benchmark streams matrix operands by default; edit magazine.h to change STREAM_A, STREAM_B, and STREAM_C. Runs single-threaded by default; use `make OMP=1` to parallelize the batch. ```shell cd libxsmm; make cd samples/magazine; make ``` ```shell ./benchmark.sh ``` -------------------------------- ### Build LIBXSMM with default options Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Compiles the library with static kernels for matrix sizes (5,25), (25,5), and (5,5). ```bash make MNK="5 25" ALPHA=1 BETA=0 ``` -------------------------------- ### libxsmm_ptr_l0/l1/l2: C-address of long long arrays Source: https://libxsmm.readthedocs.io/en/latest/fortran/sourcefile/libxsmm.f.html Functions to get C pointer for INTEGER(C_LONG_LONG) arrays: scalar, 1D, and 2D. ```Fortran !> Determines the C-address of the given array. FUNCTION libxsmm_ptr_l0(a) INTEGER(C_LONG_LONG), INTENT(IN), TARGET :: a TYPE(C_PTR) :: libxsmm_ptr_l0 libxsmm_ptr_l0 = C_LOC(a) END FUNCTION FUNCTION libxsmm_ptr_l1(a) INTEGER(C_LONG_LONG), INTENT(IN), TARGET :: a(*) TYPE(C_PTR) :: libxsmm_ptr_l1 libxsmm_ptr_l1 = C_LOC(a) END FUNCTION FUNCTION libxsmm_ptr_l2(a) INTEGER(C_LONG_LONG), INTENT(IN) :: a(:,:) TYPE(C_PTR) :: libxsmm_ptr_l2 libxsmm_ptr_l2 = libxsmm_ptr_l1(a) END FUNCTION ``` -------------------------------- ### Run basic tests with make tests Source: https://libxsmm.readthedocs.io/libxsmm_valid Runs the basic test suite for LIBXSMM. Use this to verify a default build. ```bash make tests ``` -------------------------------- ### libxsmm_xrelease Source: https://libxsmm.readthedocs.io/en/latest/fortran/lists/procedures.html Releases registered data associated with a key. This can be used to free memory before program termination, for example when reusing a key with a larger value. ```APIDOC ## libxsmm_xrelease ### Description Releases registered data associated with a key. This can be used to free memory before program termination, for example when reusing a key with a larger value. ### Method Interface ``` -------------------------------- ### Verbose output from failing test Source: https://libxsmm.readthedocs.io/libxsmm_valid Example output from a failing test when running with LIBXSMM_VERBOSE=4. The termination message shows the LIBXSMM_TARGET, registry size, and the command that was launched. ```text [...] LIBXSMM_TARGET: hsw Registry and code: 13 MB + 8 KB (meltw=1) Command: ./eltwise_binary_simple 1 0 F32 F32 F32 F32 10 10 10 10 [...] ``` -------------------------------- ### Invoke generator driver for dense_asm kernel Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_be Example invocation of the generator driver for a dense_asm kernel with DP precision, knl architecture, and AL2_BL2viaC prefetch strategy. ```bash bin/libxsmm_gemm_generator dense_asm foo.c foo 16 16 16 32 32 32 1 1 1 1 knl AL2_BL2viaC DP ``` -------------------------------- ### Profile LIBXSMM with perf record, inject, and report Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_prof Commands to record a profile with jitdump support, inject the jitdump data into perf.data, and view the report. Requires LIBXSMM_VERBOSE set to a negative value and the library built with JITDUMP=1 or PERF=2. Use -k mono to match CLOCK_MONOTONIC; otherwise perf inject may reject the dump. ```bash LIBXSMM_VERBOSE=-1 perf record -k mono -g -- ./myapplication perf inject -j -i perf.data -o perf.jit.data perf report -i perf.jit.data ``` -------------------------------- ### Invoke generator driver for dense kernel Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_be Example invocation of the generator driver for a dense kernel with DP precision, hsw architecture, and nopf prefetch strategy. ```bash bin/libxsmm_gemm_generator dense foo.c foo 16 16 16 32 32 32 1 1 1 1 hsw nopf DP ``` -------------------------------- ### Build the generator driver and run it Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_be Build the stand-alone generator driver with 'make generator' and then run the resulting binary 'bin/libxsmm_gemm_generator'. The driver is considered legacy and deprecated. ```bash make generator bin/libxsmm_gemm_generator ``` -------------------------------- ### Invoke generator driver for sparse kernel Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_be Example invocation of the generator driver for a sparse kernel with DP precision, hsw architecture, nopf prefetch strategy, and a CSC file bar.csc. ```bash bin/libxsmm_gemm_generator sparse foo.c foo 16 16 16 32 0 32 1 1 1 1 hsw nopf DP bar.csc ``` -------------------------------- ### Fortran interface for user-data dispatch: libxsmm_xregister and libxsmm_xdispatch Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_aux Fortran interface following the C flow: query with libxsmm_xdispatch, and if NULL, register with libxsmm_xregister. Structured keys must be zero-filled using libxsmm_xclear, even after element-wise initialization. A contiguous array key only needs element initialization. ```fortran FUNCTION libxsmm_xregister(key, keysize, valsize, valinit) TYPE(C_PTR), INTENT(IN), VALUE :: key TYPE(C_PTR), INTENT(IN), VALUE, OPTIONAL :: valinit INTEGER(C_INT), INTENT(IN) :: keysize, valsize TYPE(C_PTR) :: libxsmm_xregister END FUNCTION FUNCTION libxsmm_xdispatch(key, keysize) TYPE(C_PTR), INTENT(IN), VALUE :: key INTEGER(C_INT), INTENT(IN) :: keysize TYPE(C_PTR) :: libxsmm_xdispatch END FUNCTION ``` -------------------------------- ### libxsmm_smmcall_prf(kernel, a, b, c, pa, pb, pc) Source: https://libxsmm.readthedocs.io/en/latest/fortran/proc/libxsmm_smmcall_prf.html Calls the LIBXSMM kernel with the given arguments. The kernel must be a valid LIBXSMM_SMMFUNCTION. The matrices a, b, and c are the primary operands, while pa, pb, and pc are optional prefetch addresses. ```APIDOC ## libxsmm_smmcall_prf(kernel, a, b, c, pa, pb, pc) ### Description Calls the kernel with the given arguments. Alternatively, PROCPOINTER can be used as shown by the inner comments of this routine (LIBXSMM_FUNCTION6). The libxsmm_xmmcall routines can be used in FORTRAN77. ### Method CALL ### Interface ```fortran public subroutine libxsmm_smmcall_prf(kernel, a, b, c, pa, pb, pc) ``` ### Parameters #### Arguments - **kernel** (type(LIBXSMM_SMMFUNCTION)), intent(in) - The kernel to call. - **a** (real(kind=C_FLOAT)), intent(in), TARGET - First input matrix. - **b** (real(kind=C_FLOAT)), intent(in), TARGET - Second input matrix. - **c** (real(kind=C_FLOAT)), intent(inout), TARGET - Output matrix. - **pa** (real(kind=C_FLOAT)), intent(in), TARGET - Prefetch address for a. - **pb** (real(kind=C_FLOAT)), intent(in), TARGET - Prefetch address for b. - **pc** (real(kind=C_FLOAT)), intent(in), TARGET - Prefetch address for c. ### Usage Example ```fortran ! Example usage (conceptual) call libxsmm_smmcall_prf(kernel, a, b, c, pa, pb, pc) ``` ### Notes - The routine is part of the LIBXSMM Fortran interface. - For FORTRAN77 compatibility, consider using the libxsmm_xmmcall routines. ``` -------------------------------- ### Run benchmark kernels Source: https://libxsmm.readthedocs.io/libxsmm_samples Executes the benchmark kernels presented by the article. ```shell ./benchmark.sh ``` -------------------------------- ### Compile LIBXSMM for Knights Corner with KNC=1 Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Compile the library from the root directory using the Intel compiler suite. The make command sets MNK="5 25", ALPHA=1, BETA=0, PRECISION=1, OPT=3, OFFLOAD=1, and KNC=1 for Knights Corner. ```bash make MNK="5 25" ALPHA=1 BETA=0 PRECISION=1 OPT=3 OFFLOAD=1 KNC=1 ``` -------------------------------- ### public subroutine libxsmm_smmcall(kernel, a, b, c, pa, pb, pc) Source: https://libxsmm.readthedocs.io/en/latest/fortran/proc/libxsmm_smmcall.html Invokes a LIBXSMM small matrix multiplication kernel with the given input matrices and optional prefetch pointers. ```APIDOC ## SUBROUTINE libxsmm_smmcall(kernel, a, b, c, pa, pb, pc) ### Description Invokes a LIBXSMM small matrix multiplication kernel. See also libxsmm_smmcall_abc and libxsmm_smmcall_prf. ### Method SUBROUTINE ### Signature public subroutine libxsmm_smmcall(kernel, a, b, c, pa, pb, pc) ### Parameters #### Arguments - **kernel** (type(LIBXSMM_SMMFUNCTION)) - intent(in) - The kernel function handle. - **a** (real(kind=C_FLOAT)) - intent(in), TARGET - Input matrix A. - **b** (real(kind=C_FLOAT)) - intent(in), TARGET - Input matrix B. - **c** (real(kind=C_FLOAT)) - intent(inout), TARGET - Output matrix C. - **pa** (real(kind=C_FLOAT)) - intent(in), optional, TARGET - Prefetch pointer A. - **pb** (real(kind=C_FLOAT)) - intent(in), optional, TARGET - Prefetch pointer B. - **pc** (real(kind=C_FLOAT)) - intent(in), optional, TARGET - Prefetch pointer C. ### Related Procedures - libxsmm_smmcall_abc - libxsmm_smmcall_prf ``` -------------------------------- ### CMake FetchContent module for building LIBXSMM Source: https://libxsmm.readthedocs.io/en/latest Use this CMake module to fetch and build LIBXSMM as a static library in a custom build. It sets LIBXSMM_DEFAULT_CONFIG as a public compile definition and disables the BLAS fallback with __BLAS=0. The example excludes the generator driver source file from the build. ```cmake include(FetchContent) FetchContent_Declare( xsmm URL https://github.com/chelini/libxsmm/archive/.tar.gz URL_HASH SHA256= ) FetchContent_GetProperties(xsmm) if(NOT xsmm_POPULATED) FetchContent_Populate(xsmm) endif() set(LIBXSMMROOT ${xsmm_SOURCE_DIR}) file(GLOB _GLOB_XSMM_SRCS LIST_DIRECTORIES false CONFIGURE_DEPENDS ${LIBXSMMROOT}/src/*.c) list(REMOVE_ITEM _GLOB_XSMM_SRCS ${LIBXSMMROOT}/src/libxsmm_generator_gemm_driver.c) set(XSMM_INCLUDE_DIRS ${LIBXSMMROOT}/include) add_library(xsmm STATIC ${_GLOB_XSMM_SRCS}) target_include_directories(xsmm PUBLIC ${XSMM_INCLUDE_DIRS}) target_compile_definitions(xsmm PUBLIC LIBXSMM_DEFAULT_CONFIG ) target_compile_definitions(xsmm PRIVATE __BLAS=0 ) ``` -------------------------------- ### Build SPECFEM with custom Fortran flags Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Shows how to add specific Fortran compiler flags during the build. ```bash cd sample/specfem make FCFLAGS="-O3 -fopenmp" [...] ``` -------------------------------- ### Check LIBXSMM version in a prebuilt application using LIBXSMM_VERBOSE Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_qna Set the LIBXSMM_VERBOSE environment variable to 1 or higher to print LIBXSMM version and target information when the program terminates. This works without any input or workload. The example shows running the CP2K executable and the resulting output lines. ```bash LIBXSMM_VERBOSE=1 exe/Linux-x86-64-intelx/cp2k.psmp [...] LIBXSMM_VERSION: release-1.11 LIBXSMM_TARGET: clx ``` -------------------------------- ### Build elementwise tests Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Builds the LIBXSMM library and the elementwise tests. The first make command builds the library with default settings; the alternative command builds without BLAS support and with static linking disabled. Then the sample tests are built in the eltwise directory. ```bash cd /path/to/libxsmm make or make BLAS=0 LIBXSMM_NO_BLAS=1 STATIC=0 -j 64 cd /path/to/libxsmm/samples/eltwise make -j 16 ``` -------------------------------- ### Basic dispatch and invoke pattern for LIBXSMM TPP Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_tpp Shows the generic pattern for using LIBXSMM TPP: create a shape, define a kernel, dispatch (JIT compile or retrieve), prepare parameters, and invoke. The placeholder must be replaced with the specific operation group. ```c // Get the shape of the operands to build the right operand op_shape = libxsmm_create__shape(matrix dimensions, leading dimensions, data types); // Define a new kernel libxsmm_xsmmfunction kernel = {NULL}; // Dispatch (JIT compile or retrieve the previously compiled function) kernel. = libxsmm_dispatch_(op_shape, FLAGS); // Prepare the execution parameters libxsmm__param op_param; op_param.?.? = op parameters // Call the kernel kernel.(&op_param); ``` -------------------------------- ### Run stpm sample Source: https://libxsmm.readthedocs.io/libxsmm_samples Usage command for the stpm sample, which replicates the axhelm kernel. Arguments: m n k size1 size. ```bash ./stpm m n k size1 size ``` -------------------------------- ### Build SPECFEM via LIBXSMM root make target Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Shortens the build by using a 'specfem' make target in the LIBXSMM root directory. ```bash make MNK="5 25" ALPHA=1 BETA=0 PRECISION=1 OPT=3 AVX=1 specfem ``` -------------------------------- ### Compile LIBXSMM for Haswell with AVX=2 Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Compile the library from the root directory using the Intel compiler suite. The make command sets MNK="5 25", ALPHA=1, BETA=0, PRECISION=1, OPT=3, and AVX=2 for Haswell. ```bash make MNK="5 25" ALPHA=1 BETA=0 PRECISION=1 OPT=3 AVX=2 ``` -------------------------------- ### Build LIBXSMM for Sandy Bridge CPUs Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Compiles with AVX=1 for Sandy Bridge architecture. ```bash make MNK="5 25" ALPHA=1 BETA=0 PRECISION=1 OPT=3 AVX=1 ``` -------------------------------- ### Build for POWER with IBM XL Compiler (PLATFORM=1) Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_compat Build LIBXSMM for the POWER platform using the IBM XL Compiler for Linux. The PLATFORM=1 setting is required to unlock compilation on POWER. ```makefile make PLATFORM=1 CC=xlc CXX=xlc++ FC=xlf ``` -------------------------------- ### Collect VTune hotspots with call stacks for an application Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_prof Runs VTune's 'hotspots' collection with hardware sampling and stack collection on an application. The '-data-limit 0' disables data limits, and '-knob stack-size=0' collects full stacks. ```bash vtune -r resultdir -data-limit 0 -collect hotspots \ -knob enable-stack-collection=true \ -knob sampling-mode=hw \ -knob stack-size=0 \ -- ./myapplication ``` -------------------------------- ### public function libxsmm_xdispatch(key, keysize) Source: https://libxsmm.readthedocs.io/en/latest/fortran/proc/libxsmm_xdispatch.html Queries a user-defined value from LIBXSMM's code registry using a key and its size. ```APIDOC ## libxsmm_xdispatch ### Description Queries a user-defined value from LIBXSMM's code registry. ### Function Signature ```fortran public function libxsmm_xdispatch(key, keysize) result(retval) ``` ### Parameters #### Arguments - **key** (type(C_PTR), intent(in), VALUE) - Required - The key used to look up the value in the registry. - **keysize** (integer(kind=C_INT), intent(in)) - Required - The size of the key in bytes. ### Return Value - **retval** (type(C_PTR)) - A pointer to the user-defined value associated with the key, or NULL if not found. ``` -------------------------------- ### Build LIBXSMM with Cygwin Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_compat Builds LIBXSMM in the Cygwin environment. Note that all limitations of Microsoft Windows apply. ```bash make ``` -------------------------------- ### Build LIBXSMM for Haswell CPUs Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Compiles with AVX=2 for Haswell architecture. ```bash make MNK="5 25" ALPHA=1 BETA=0 PRECISION=1 OPT=3 AVX=2 ``` -------------------------------- ### Build LIBXSMM out-of-tree Source: https://libxsmm.readthedocs.io/en/latest Build LIBXSMM in an out-of-tree fashion by running make with the path to the LIBXSMM Makefile. ```makefile cd libxsmm-install make -f /path/to/libxsmm/Makefile ``` -------------------------------- ### Requesting AL2jpst Prefetch Strategy Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_tune Requests the 'AL2jpst' prefetch strategy via PREFETCH=8. Extends kernel signatures with pa, pb, and pc arguments for prefetching. ```make make PREFETCH=8 ``` -------------------------------- ### libxsmm_dispatch_meltw_binary Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_tpp Dispatches a binary element-wise kernel. ```APIDOC ## libxsmm_dispatch_meltw_binary ### Description Dispatches (JIT-compiles or retrieves) a binary element-wise kernel based on the given operation type, shape, and flags. Returns a function pointer that can be invoked with the actual operands. ### Function Signature ```c libxsmm_meltwfunction_binary libxsmm_dispatch_meltw_binary( const libxsmm_meltw_binary_type binary_type, const libxsmm_meltw_binary_shape binary_shape, const libxsmm_bitfield binary_flags ); ``` ### Parameters - **binary_type** (libxsmm_meltw_binary_type) - Required - The type of binary operation (e.g., Add, Sub). - **binary_shape** (libxsmm_meltw_binary_shape) - Required - The shape descriptor created by `libxsmm_create_meltw_binary_shape`. - **binary_flags** (libxsmm_bitfield) - Required - Flags controlling the operation behavior. ### Returns - **libxsmm_meltwfunction_binary** - A function pointer to the compiled kernel. ``` -------------------------------- ### Run tests with STATIC=0 Source: https://libxsmm.readthedocs.io/libxsmm_valid Runs tests with the STATIC=0 key-value pair, which represents a unique (re-)build and test configuration. ```bash make STATIC=0 tests ``` -------------------------------- ### C API for user-data dispatch: libxsmm_xregister and libxsmm_xdispatch Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_aux Registers a key-value pair and dispatches a value by key. The key must be binary reproducible; structured keys should be zero-filled before initialization. The key size is limited to LIBXSMM_DESCRIPTOR_MAXSIZE (96 bytes), while the value size is arbitrary. The value is copied and can be released with libxsmm_xrelease. ```c void* libxsmm_xregister(const void* key, size_t key_size, size_t value_size, const void* value_init); void* libxsmm_xdispatch(const void* key, size_t key_size); ``` -------------------------------- ### Build LIBXSMM for Knights Corner (KNC) Source: https://libxsmm.readthedocs.io/en/latest/libxsmm_samples Compiles for Knights Corner, also creating a Sandy Bridge version. ```bash make MNK="5 25" ALPHA=1 BETA=0 PRECISION=1 OPT=3 AVX=1 \ OFFLOAD=1 KNC=1 ``` -------------------------------- ### Run a single test with scripts/tool_test.sh 1 Source: https://libxsmm.readthedocs.io/libxsmm_valid Selects and runs a single test from the whole collection. ```bash scripts/tool_test.sh 1 ```