commit a98b356345400e412c39350206bf36b515d4672a Author: Antonio De Lucreziis Date: Sun Mar 3 18:11:16 2024 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81ca160 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +# Local files +.env +*.local* + +# NodeJS +node_modules/ + +# Binaries +bin/ +.out/ +out/ +dist/ + +# Editors +.vscode/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..22ca580 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "deps/petsc"] + path = deps/petsc + url = https://gitlab.com/petsc/petsc.git + branch = release diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..39ef269 --- /dev/null +++ b/Makefile @@ -0,0 +1,60 @@ +PETSC_DIR = ./deps/petsc +PETSC_ARCH = arch-linux-c-debug + +petsc.pc := $(PETSC_DIR)/$(PETSC_ARCH)/lib/pkgconfig/petsc.pc + +PACKAGES := $(petsc.pc) + +CC := $(shell pkg-config --variable=ccompiler $(PACKAGES)) +CXX := $(shell pkg-config --variable=cxxcompiler $(PACKAGES)) +FC := $(shell pkg-config --variable=fcompiler $(PACKAGES)) +CFLAGS_OTHER := $(shell pkg-config --cflags-only-other $(PACKAGES)) +CFLAGS := $(shell pkg-config --variable=cflags_extra $(PACKAGES)) $(CFLAGS_OTHER) +CXXFLAGS := $(shell pkg-config --variable=cxxflags_extra $(PACKAGES)) $(CFLAGS_OTHER) +FFLAGS := $(shell pkg-config --variable=fflags_extra $(PACKAGES)) +CPPFLAGS := $(shell pkg-config --cflags-only-I $(PACKAGES)) +LDFLAGS := $(shell pkg-config --libs-only-L --libs-only-other $(PACKAGES)) +LDFLAGS += $(patsubst -L%, $(shell pkg-config --variable=ldflag_rpath $(PACKAGES))%, $(shell pkg-config --libs-only-L $(PACKAGES))) +LDLIBS := $(shell pkg-config --libs-only-l $(PACKAGES)) -lm +CUDAC := $(shell pkg-config --variable=cudacompiler $(PACKAGES)) +CUDAC_FLAGS := $(shell pkg-config --variable=cudaflags_extra $(PACKAGES)) +CUDA_LIB := $(shell pkg-config --variable=cudalib $(PACKAGES)) +CUDA_INCLUDE := $(shell pkg-config --variable=cudainclude $(PACKAGES)) + +all: setup main + +setup: + mkdir -p bin + mkdir -p deps + +print: + @echo ====== PETSc ====== + @echo PETSC_DIR=$(PETSC_DIR) + @echo PETSC_ARCH=$(PETSC_ARCH) + @echo ====== Compilers ====== + @echo CC=$(CC) + @echo CXX=$(CXX) + @echo FC=$(FC) + @echo COMPILE.cc=$(COMPILE.cc) + @echo LINK.cc=$(LINK.cc) + @echo ====== Flags ====== + @echo CFLAGS=$(CFLAGS) + @echo CXXFLAGS=$(CXXFLAGS) + @echo FFLAGS=$(FFLAGS) + @echo CPPFLAGS=$(CPPFLAGS) + @echo LDFLAGS=$(LDFLAGS) + @echo LDLIBS=$(LDLIBS) + @echo ====== Cuda ====== + @echo CUDAC=$(CUDAC) + @echo CUDAC_FLAGS=$(CUDAC_FLAGS) + @echo CUDA_LIB=$(CUDA_LIB) + @echo CUDA_INCLUDE=$(CUDA_INCLUDE) + +bin/% : %.c + $(LINK.cc) -o $@ $^ $(LDLIBS) +bin/%.o: %.cxx + $(COMPILE.cc) $(OUTPUT_OPTION) $< +bin/%.o: %.c + $(CC) $(OUTPUT_OPTION) $< +bin/%.o : %.cu + $(CUDAC) -c $(CPPFLAGS) $(CUDAC_FLAGS) $(CUDA_INCLUDE) -o $@ $< diff --git a/README.md b/README.md new file mode 100644 index 0000000..6abe989 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# Progetto di Calcolo Scientifico + +Metodo di Arnoldi per autovalori utilizzando PETSc. + +## Setup + +```bash +$ git submodule init +$ git submodule update + +$ cd deps/petsc +$ ./configure +$ make +``` + +## Compile + +```bash +$ make all +``` \ No newline at end of file diff --git a/deps/petsc b/deps/petsc new file mode 160000 index 0000000..6903f02 --- /dev/null +++ b/deps/petsc @@ -0,0 +1 @@ +Subproject commit 6903f0243818938898d808bb21b0de29ccfe6a88 diff --git a/main.c b/main.c new file mode 100644 index 0000000..a3686cf --- /dev/null +++ b/main.c @@ -0,0 +1,100 @@ +static char help[] = "Solves a tridiagonal linear system with KSP.\n\n"; + +#include +#include + +int main(int argc, char **args) +{ + Vec x, b, u; /* approx solution, RHS, exact solution */ + Mat A; /* linear system matrix */ + KSP ksp; /* linear solver context */ + PC pc; /* preconditioner context */ + PetscReal norm; /* norm of solution error */ + PetscInt i, n = 10, col[3], its; + PetscMPIInt size; + PetscScalar value[3]; + + PetscFunctionBeginUser; + PetscCall(PetscInitialize(&argc, &args, (char *)0, help)); + PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size)); + PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!"); + + PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL)); + + PetscCall(VecCreate(PETSC_COMM_SELF, &x)); + PetscCall(PetscObjectSetName((PetscObject)x, "Solution")); + PetscCall(VecSetSizes(x, PETSC_DECIDE, n)); + PetscCall(VecSetFromOptions(x)); + PetscCall(VecDuplicate(x, &b)); + PetscCall(VecDuplicate(x, &u)); + + PetscCall(MatCreate(PETSC_COMM_SELF, &A)); + PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, n, n)); + PetscCall(MatSetFromOptions(A)); + PetscCall(MatSetUp(A)); + + value[0] = -1.0; + value[1] = 2.0; + value[2] = -1.0; + for (i = 1; i < n - 1; i++) { + col[0] = i - 1; + col[1] = i; + col[2] = i + 1; + PetscCall(MatSetValues(A, 1, &i, 3, col, value, INSERT_VALUES)); + } + i = n - 1; + col[0] = n - 2; + col[1] = n - 1; + PetscCall(MatSetValues(A, 1, &i, 2, col, value, INSERT_VALUES)); + i = 0; + col[0] = 0; + col[1] = 1; + value[0] = 2.0; + value[1] = -1.0; + PetscCall(MatSetValues(A, 1, &i, 2, col, value, INSERT_VALUES)); + PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); + PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); + + PetscCall(VecSet(u, 1.0)); + PetscCall(MatMult(A, u, b)); + + PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp)); + + PetscCall(KSPSetOperators(ksp, A, A)); + + PetscCall(KSPGetPC(ksp, &pc)); + PetscCall(PCSetType(pc, PCJACOBI)); + PetscCall(KSPSetTolerances(ksp, 1.e-5, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); + + PetscCall(KSPSetFromOptions(ksp)); + + PetscCall(KSPSolve(ksp, b, x)); + + PetscCall(VecAXPY(x, -1.0, u)); + PetscCall(VecNorm(x, NORM_2, &norm)); + PetscCall(KSPGetIterationNumber(ksp, &its)); + PetscCall(PetscPrintf(PETSC_COMM_SELF, "Norm of error %g, Iterations %" PetscInt_FMT "\n", (double)norm, its)); + + PetscCall(MatShift(A, 2.0)); + PetscCall(KSPSolve(ksp, b, x)); + + PetscCall(KSPDestroy(&ksp)); + + if (PCMPIServerActive) { + PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp)); + PetscCall(KSPSetOptionsPrefix(ksp, "prefix_test_")); + PetscCall(MatSetOptionsPrefix(A, "prefix_test_")); + PetscCall(KSPSetOperators(ksp, A, A)); + PetscCall(KSPSetFromOptions(ksp)); + PetscCall(KSPSolve(ksp, b, x)); + PetscCall(KSPDestroy(&ksp)); + } + + PetscCall(VecDestroy(&x)); + PetscCall(VecDestroy(&u)); + PetscCall(VecDestroy(&b)); + PetscCall(MatDestroy(&A)); + + PetscCall(PetscFinalize()); + return 0; +}