minimal working spack+cmake+petsc+lapack+clangd setup

next
commit 0858036401

@ -0,0 +1,2 @@
# .clang-format
IndentWidth: 4

2
.gitignore vendored

@ -0,0 +1,2 @@
build/
.spack-env/

@ -0,0 +1,3 @@
{
"clangd.arguments": ["-background-index", "-compile-commands-dir=build/"]
}

@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.31.1)
# Enable compile_commands.json generation for IDE support
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_C_COMPILER gcc)
set(CMAKE_CXX_COMPILER g++)
set(CMAKE_Fortran_COMPILER gfortran)
project(main)
add_executable(main main.c)
find_package(PkgConfig REQUIRED)
pkg_search_module(OpenBLAS REQUIRED IMPORTED_TARGET openblas)
pkg_search_module(PETSc REQUIRED IMPORTED_TARGET petsc)
pkg_search_module(OpenMPI REQUIRED IMPORTED_TARGET ompi)
target_include_directories(main PUBLIC ${PETSc_INCLUDE_DIRS} ${OpenBLAS_INCLUDE_DIRS} ${OpenMPI_INCLUDE_DIRS})
target_link_libraries(main PUBLIC m PkgConfig::PETSc PkgConfig::OpenBLAS PkgConfig::OpenMPI)

@ -0,0 +1,14 @@
# Arnoldi Iteration
## Usage
To build main from main.c
```bash shell
$ rm -rf build
$ mkdir build
$ cd build
$ cmake ..
$ make
$ mpirun -n 2 ./main
```

273
main.c

@ -0,0 +1,273 @@
static char help[] = "Basic vector routines.\n\n";
/*
Include "petscvec.h" so that we can use vectors. Note that this file
automatically includes:
petscsys.h - base PETSc routines petscis.h - index sets
petscviewer.h - viewers
*/
#include <lapacke.h>
#include <stdio.h>
#include <stdlib.h>
#include <petscvec.h>
int example_lapack() {
int n = 2; // Dimension of the matrix A
int nrhs = 1; // Number of right-hand sides
int lda = 2; // Leading dimension of A
int ldb = 2; // Leading dimension of B
int info; // Status output
int ipiv[2]; // Pivot indices for the LU decomposition
// Define matrix A (row-major order)
double A[4] = {3, 1, 1, 2};
// Define right-hand side vector b
double b[2] = {9, 8};
// Call LAPACK dgesv to solve Ax = b
info = LAPACKE_dgesv(LAPACK_ROW_MAJOR, n, nrhs, A, lda, ipiv, b, ldb);
// Check for success
if (info == 0) {
printf("Solution:\n");
for (int i = 0; i < n; i++) {
printf("x[%d] = %f\n", i, b[i]);
}
} else if (info < 0) {
printf("Argument %d had an illegal value.\n", -info);
} else {
printf("Matrix is singular. Solution could not be computed.\n");
}
}
int main(int argc, char **argv) {
printf("LAPACKE Example:\n");
example_lapack();
printf("PETSC Example:\n");
Vec x, y, w; /* vectors */
Vec *z; /* array of vectors */
PetscReal norm, v, v1, v2, maxval;
PetscInt n = 20, maxind;
PetscScalar one = 1.0, two = 2.0, three = 3.0, dots[3], dot;
PetscFunctionBeginUser;
PetscCall(PetscInitialize(&argc, &argv, NULL, help));
PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
/*
Create a vector, specifying only its global dimension.
When using VecCreate(), VecSetSizes() and VecSetFromOptions(), the vector
format (currently parallel, shared, or sequential) is determined at
runtime. Also, the parallel partitioning of the vector is determined by
PETSc at runtime.
*/
PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
PetscCall(VecSetSizes(x, PETSC_DECIDE, n));
PetscCall(VecSetFromOptions(x));
/*
Duplicate some work vectors (of the same format and
partitioning as the initial vector).
*/
PetscCall(VecDuplicate(x, &y));
PetscCall(VecDuplicate(x, &w));
/*
Duplicate more work vectors (of the same format and
partitioning as the initial vector). Here we duplicate
an array of vectors, which is often more convenient than
duplicating individual ones.
*/
PetscCall(VecDuplicateVecs(x, 3, &z));
/*
Set the vectors to entries to a constant value.
*/
PetscCall(VecSet(x, one));
PetscCall(VecSet(y, two));
PetscCall(VecSet(z[0], one));
PetscCall(VecSet(z[1], two));
PetscCall(VecSet(z[2], three));
/*
Demonstrate various basic vector routines.
*/
PetscCall(VecDot(x, y, &dot));
PetscCall(VecMDot(x, 3, z, dots));
/*
Note: If using a complex numbers version of PETSc, then
PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
(when using real numbers) it is undefined.
*/
PetscCall(
PetscPrintf(PETSC_COMM_WORLD, "Vector length %" PetscInt_FMT "\n", n));
PetscCall(VecMax(x, &maxind, &maxval));
PetscCall(PetscPrintf(PETSC_COMM_WORLD,
"VecMax %g, VecInd %" PetscInt_FMT "\n",
(double)maxval, maxind));
PetscCall(VecMin(x, &maxind, &maxval));
PetscCall(PetscPrintf(PETSC_COMM_WORLD,
"VecMin %g, VecInd %" PetscInt_FMT "\n",
(double)maxval, maxind));
PetscCall(PetscPrintf(PETSC_COMM_WORLD,
"All other values should be near zero\n"));
PetscCall(VecScale(x, two));
PetscCall(VecNorm(x, NORM_2, &norm));
v = norm - 2.0 * PetscSqrtReal((PetscReal)n);
if (v > -PETSC_SMALL && v < PETSC_SMALL)
v = 0.0;
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "VecScale %g\n", (double)v));
PetscCall(VecCopy(x, w));
PetscCall(VecNorm(w, NORM_2, &norm));
v = norm - 2.0 * PetscSqrtReal((PetscReal)n);
if (v > -PETSC_SMALL && v < PETSC_SMALL)
v = 0.0;
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "VecCopy %g\n", (double)v));
PetscCall(VecAXPY(y, three, x));
PetscCall(VecNorm(y, NORM_2, &norm));
v = norm - 8.0 * PetscSqrtReal((PetscReal)n);
if (v > -PETSC_SMALL && v < PETSC_SMALL)
v = 0.0;
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "VecAXPY %g\n", (double)v));
PetscCall(VecAYPX(y, two, x));
PetscCall(VecNorm(y, NORM_2, &norm));
v = norm - 18.0 * PetscSqrtReal((PetscReal)n);
if (v > -PETSC_SMALL && v < PETSC_SMALL)
v = 0.0;
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "VecAYPX %g\n", (double)v));
PetscCall(VecSwap(x, y));
PetscCall(VecNorm(y, NORM_2, &norm));
v = norm - 2.0 * PetscSqrtReal((PetscReal)n);
if (v > -PETSC_SMALL && v < PETSC_SMALL)
v = 0.0;
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "VecSwap %g\n", (double)v));
PetscCall(VecNorm(x, NORM_2, &norm));
v = norm - 18.0 * PetscSqrtReal((PetscReal)n);
if (v > -PETSC_SMALL && v < PETSC_SMALL)
v = 0.0;
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "VecSwap %g\n", (double)v));
PetscCall(VecWAXPY(w, two, x, y));
PetscCall(VecNorm(w, NORM_2, &norm));
v = norm - 38.0 * PetscSqrtReal((PetscReal)n);
if (v > -PETSC_SMALL && v < PETSC_SMALL)
v = 0.0;
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "VecWAXPY %g\n", (double)v));
PetscCall(VecPointwiseMult(w, y, x));
PetscCall(VecNorm(w, NORM_2, &norm));
v = norm - 36.0 * PetscSqrtReal((PetscReal)n);
if (v > -PETSC_SMALL && v < PETSC_SMALL)
v = 0.0;
PetscCall(
PetscPrintf(PETSC_COMM_WORLD, "VecPointwiseMult %g\n", (double)v));
PetscCall(VecPointwiseDivide(w, x, y));
PetscCall(VecNorm(w, NORM_2, &norm));
v = norm - 9.0 * PetscSqrtReal((PetscReal)n);
if (v > -PETSC_SMALL && v < PETSC_SMALL)
v = 0.0;
PetscCall(
PetscPrintf(PETSC_COMM_WORLD, "VecPointwiseDivide %g\n", (double)v));
PetscCall(VecSetValue(y, 0, 0.0, INSERT_VALUES));
PetscCall(VecAssemblyBegin(y));
PetscCall(VecAssemblyEnd(y));
PetscCall(VecPointwiseDivide(w, x, y));
PetscCall(VecNorm(w, NORM_2, &norm));
v = norm - 9.0 * PetscSqrtReal((PetscReal)n);
if (v > -PETSC_SMALL && v < PETSC_SMALL)
v = 0.0;
PetscCall(
PetscPrintf(PETSC_COMM_WORLD, "VecPointwiseDivide %g\n", (double)v));
dots[0] = one;
dots[1] = three;
dots[2] = two;
PetscCall(VecSet(x, one));
PetscCall(VecMAXPY(x, 3, dots, z));
PetscCall(VecNorm(z[0], NORM_2, &norm));
v = norm - PetscSqrtReal((PetscReal)n);
if (v > -PETSC_SMALL && v < PETSC_SMALL)
v = 0.0;
PetscCall(VecNorm(z[1], NORM_2, &norm));
v1 = norm - 2.0 * PetscSqrtReal((PetscReal)n);
if (v1 > -PETSC_SMALL && v1 < PETSC_SMALL)
v1 = 0.0;
PetscCall(VecNorm(z[2], NORM_2, &norm));
v2 = norm - 3.0 * PetscSqrtReal((PetscReal)n);
if (v2 > -PETSC_SMALL && v2 < PETSC_SMALL)
v2 = 0.0;
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "VecMAXPY %g %g %g \n", (double)v,
(double)v1, (double)v2));
/*
Free work space. All PETSc objects should be destroyed when they
are no longer needed.
*/
PetscCall(VecDestroy(&x));
PetscCall(VecDestroy(&y));
PetscCall(VecDestroy(&w));
PetscCall(VecDestroyVecs(3, &z));
PetscCall(PetscFinalize());
return 0;
}
/*TEST
testset:
output_file: output/ex1_1.out
# This is a test where the exact numbers are critical
diff_args: -j
test:
test:
suffix: cuda
args: -vec_type cuda
requires: cuda
test:
suffix: kokkos
args: -vec_type kokkos
requires: kokkos_kernels
test:
suffix: hip
args: -vec_type hip
requires: hip
test:
suffix: 2
nsize: 2
test:
suffix: 2_cuda
nsize: 2
args: -vec_type cuda
requires: cuda
test:
suffix: 2_kokkos
nsize: 2
args: -vec_type kokkos
requires: kokkos_kernels
test:
suffix: 2_hip
nsize: 2
args: -vec_type hip
requires: hip
TEST*/

@ -0,0 +1,64 @@
# petsc@=3.22.2%gcc@=14.2.1~X~batch~cgns~complex~cuda~debug+double~exodusii~fftw+fortran~giflib+hdf5~hpddm~hwloc+hypre~int64~jpeg~knl~kokkos~libpng~libyaml~memkind+metis~mkl-pardiso~mmg~moab~mpfr+mpi~mumps~openmp~p4est~parmmg~ptscotch~random123~rocm~saws~scalapack+shared~strumpack~suite-sparse+superlu-dist~sycl~tetgen~trilinos~valgrind~zoltan build_system=generic clanguage=C memalign=none arch=linux-arch-skylake
module load petsc/3.22.2-gcc-14.2.1-gihwmbe
# openblas@=0.3.28%gcc@=14.2.1~bignuma~consistent_fpcsr+dynamic_dispatch+fortran~ilp64+locking+pic+shared build_system=makefile patches=d0b9276 symbol_suffix=none threads=none arch=linux-arch-skylake
module load openblas/0.3.28-gcc-14.2.1-qkz6rin
# gcc-runtime@=14.2.1%gcc@=14.2.1 build_system=generic arch=linux-arch-skylake
module load gcc-runtime/14.2.1-gcc-14.2.1-iznjxul
# glibc@=2.40%gcc@=14.2.1 build_system=autotools arch=linux-arch-skylake
module load glibc/2.40-gcc-14.2.1-hr3l3g3
# gmake@=4.4.1%gcc@=14.2.1~guile build_system=generic arch=linux-arch-skylake
module load gmake/4.4.1-gcc-14.2.1-hkretjr
# hdf5@=1.14.5%gcc@=14.2.1~cxx~fortran~hl~ipo~java~map+mpi+shared~subfiling~szip~threadsafe+tools api=default build_system=cmake build_type=Release generator=make arch=linux-arch-skylake
module load hdf5/1.14.5-gcc-14.2.1-6ll3mut
# openmpi@=5.0.5%gcc@=14.2.1+atomics~cuda~debug~gpfs~internal-hwloc~internal-libevent~internal-pmix~java~lustre~memchecker~openshmem~romio+rsh~static~two_level_namespace+vt+wrapper-rpath build_system=autotools fabrics=none romio-filesystem=none schedulers=none arch=linux-arch-skylake
module load openmpi/5.0.5-gcc-14.2.1-6xhzsjq
# hwloc@=2.11.1%gcc@=14.2.1~cairo~cuda~gl~level_zero~libudev+libxml2~nvml~opencl+pci~rocm build_system=autotools libs=shared,static arch=linux-arch-skylake
module load hwloc/2.11.1-gcc-14.2.1-2lq5p2l
# libpciaccess@=0.17%gcc@=14.2.1 build_system=autotools arch=linux-arch-skylake
module load libpciaccess/0.17-gcc-14.2.1-yy6pv2y
# libxml2@=2.13.4%gcc@=14.2.1+pic~python+shared build_system=autotools arch=linux-arch-skylake
module load libxml2/2.13.4-gcc-14.2.1-dcszipz
# xz@=5.4.6%gcc@=14.2.1~pic build_system=autotools libs=shared,static arch=linux-arch-skylake
module load xz/5.4.6-gcc-14.2.1-5zfe3p6
# zlib-ng@=2.2.1%gcc@=14.2.1+compat+new_strategies+opt+pic+shared build_system=autotools arch=linux-arch-skylake
module load zlib-ng/2.2.1-gcc-14.2.1-i6qthch
# ncurses@=6.5%gcc@=14.2.1~symlinks+termlib abi=none build_system=autotools patches=7a351bc arch=linux-arch-skylake
module load ncurses/6.5-gcc-14.2.1-z7vtx7m
# libevent@=2.1.12%gcc@=14.2.1+openssl build_system=autotools arch=linux-arch-skylake
module load libevent/2.1.12-gcc-14.2.1-yovxoxy
# openssl@=3.4.0%gcc@=14.2.1~docs+shared build_system=generic certs=mozilla arch=linux-arch-skylake
module load openssl/3.4.0-gcc-14.2.1-zrlz2dj
# numactl@=2.0.18%gcc@=14.2.1 build_system=autotools arch=linux-arch-skylake
module load numactl/2.0.18-gcc-14.2.1-jb2iuoi
# openssh@=9.9p1%gcc@=14.2.1+gssapi build_system=autotools arch=linux-arch-skylake
module load openssh/9.9p1-gcc-14.2.1-5gqzmmn
# krb5@=1.21.3%gcc@=14.2.1+shared build_system=autotools arch=linux-arch-skylake
module load krb5/1.21.3-gcc-14.2.1-jq6txro
# gettext@=0.22.5%gcc@=14.2.1+bzip2+curses+git~libunistring+libxml2+pic+shared+tar+xz build_system=autotools arch=linux-arch-skylake
module load gettext/0.22.5-gcc-14.2.1-n2v3h2a
# bzip2@=1.0.8%gcc@=14.2.1~debug~pic+shared build_system=generic arch=linux-arch-skylake
module load bzip2/1.0.8-gcc-14.2.1-6n7jklp
# tar@=1.35%gcc@=14.2.1 build_system=autotools zip=pigz arch=linux-arch-skylake
module load tar/1.35-gcc-14.2.1-r7qqd7q
# pigz@=2.8%gcc@=14.2.1 build_system=makefile arch=linux-arch-skylake
module load pigz/2.8-gcc-14.2.1-xlmu6ri
# zstd@=1.5.6%gcc@=14.2.1+programs build_system=makefile compression=none libs=shared,static arch=linux-arch-skylake
module load zstd/1.5.6-gcc-14.2.1-mtfh4kv
# libedit@=3.1-20240808%gcc@=14.2.1 build_system=autotools arch=linux-arch-skylake
module load libedit/3.1-20240808-gcc-14.2.1-mopvlvk
# libxcrypt@=4.4.35%gcc@=14.2.1~obsolete_api build_system=autotools patches=4885da3 arch=linux-arch-skylake
module load libxcrypt/4.4.35-gcc-14.2.1-jgg5ryn
# pmix@=5.0.3%gcc@=14.2.1~munge~python~restful build_system=autotools arch=linux-arch-skylake
module load pmix/5.0.3-gcc-14.2.1-5oppajs
# pkgconf@=2.2.0%gcc@=14.2.1 build_system=autotools arch=linux-arch-skylake
module load pkgconf/2.2.0-gcc-14.2.1-odevpf2
# hypre@=2.32.0%gcc@=14.2.1~caliper~complex~cublas~cuda~debug+fortran~gptune~gpu-aware-mpi~int64~internal-superlu+lapack~magma~mixedint+mpi~openmp~rocblas~rocm+shared~superlu-dist~sycl~umpire~unified-memory build_system=autotools precision=double arch=linux-arch-skylake
module load hypre/2.32.0-gcc-14.2.1-otntewq
# openblas@=0.3.28%gcc@=14.2.1~bignuma~consistent_fpcsr+dynamic_dispatch+fortran~ilp64+locking+pic+shared build_system=makefile patches=d0b9276 symbol_suffix=none threads=none arch=linux-arch-skylake
module load openblas/0.3.28-gcc-14.2.1-qkz6rin
# metis@=5.1.0%gcc@=14.2.1~gdb~int64~ipo~no_warning~real64+shared build_system=cmake build_type=Release generator=make patches=4991da9,93a7903,b1225da arch=linux-arch-skylake
module load metis/5.1.0-gcc-14.2.1-qug2wej
# parmetis@=4.0.3%gcc@=14.2.1~gdb~int64~ipo+shared build_system=cmake build_type=Release generator=make patches=4f89253,50ed208,704b84f arch=linux-arch-skylake
module load parmetis/4.0.3-gcc-14.2.1-nuynu5c
# superlu-dist@=9.1.0%gcc@=14.2.1~cuda~int64~ipo~openmp+parmetis~rocm+shared build_system=cmake build_type=Release generator=make arch=linux-arch-skylake
module load superlu-dist/9.1.0-gcc-14.2.1-a2ygwev

File diff suppressed because one or more lines are too long

@ -0,0 +1,18 @@
# This is a Spack Environment file.
#
# It describes a set of packages to be installed, along with
# configuration settings.
spack:
# add package specs to the `specs` list
specs:
- lapack
- petsc%gcc ^openmpi%gcc+internal-pmix
view: true
concretizer:
unify: true
modules:
default:
lmod:
core_compilers:
- gcc@=14.2.1
- clang@=18.1.8
Loading…
Cancel
Save