initial commit
commit
a98b356345
@ -0,0 +1,15 @@
|
||||
# Local files
|
||||
.env
|
||||
*.local*
|
||||
|
||||
# NodeJS
|
||||
node_modules/
|
||||
|
||||
# Binaries
|
||||
bin/
|
||||
.out/
|
||||
out/
|
||||
dist/
|
||||
|
||||
# Editors
|
||||
.vscode/
|
||||
@ -0,0 +1,4 @@
|
||||
[submodule "deps/petsc"]
|
||||
path = deps/petsc
|
||||
url = https://gitlab.com/petsc/petsc.git
|
||||
branch = release
|
||||
@ -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 $@ $<
|
||||
@ -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
|
||||
```
|
||||
@ -0,0 +1 @@
|
||||
Subproject commit 6903f0243818938898d808bb21b0de29ccfe6a88
|
||||
@ -0,0 +1,100 @@
|
||||
static char help[] = "Solves a tridiagonal linear system with KSP.\n\n";
|
||||
|
||||
#include <petscksp.h>
|
||||
#include <petscerror.h>
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
Reference in New Issue