Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| petsc [2015/02/25 08:45] – modification externe 127.0.0.1 | petsc [2016/11/28 12:29] (Version actuelle) – [Compilation (méthode classique) :] admin | ||
|---|---|---|---|
| Ligne 30: | Ligne 30: | ||
| ====== Compilation ====== | ====== Compilation ====== | ||
| - | PETSc dépend de MPI (Open MPI), pour compiler des programmes il faut charger | + | PETSc dépend de MPI (Open MPI), pour compiler des programmes il faut charger |
| <code bash> | <code bash> | ||
| - | $ module load ompi petsc | + | $ module load |
| </ | </ | ||
| Ligne 50: | Ligne 50: | ||
| PetscSynchronizedPrintf( PETSC_COMM_WORLD," | PetscSynchronizedPrintf( PETSC_COMM_WORLD," | ||
| - | PetscSynchronizedFlush( PETSC_COMM_WORLD ); | + | PetscSynchronizedFlush( PETSC_COMM_WORLD, stdout); |
| PetscFinalize( ); | PetscFinalize( ); | ||
| Ligne 60: | Ligne 60: | ||
| ===== Compilation (méthode classique) : ===== | ===== Compilation (méthode classique) : ===== | ||
| - | |||
| - | <note warning> | ||
| - | <code bash> | ||
| + | < | ||
| mpicc hello_petsc.c -o hello_petsc \ | mpicc hello_petsc.c -o hello_petsc \ | ||
| | | ||
| Ligne 93: | Ligne 91: | ||
| <code bash> | <code bash> | ||
| - | $ module load ompi petsc && make | + | $ module load |
| </ | </ | ||
| Ligne 166: | Ligne 164: | ||
| <file C Vec.c> | <file C Vec.c> | ||
| + | #include " | ||
| - | #include petscvec.h | + | int main(int argc,char **argv) |
| - | int m ain(int argc,char **argv) | + | |
| { | { | ||
| - | Vec x; | + | |
| - | int n = 20,ierr; | + | int n = 20,ierr; |
| - | PetscTruth flg; | + | |
| - | PetscScalar one = 1.0, dot; | + | PetscScalar one = 1.0, dot; |
| - | PetscInitialize(& | + | |
| - | PetscOptionsGetInt(PETSC_NULL," | + | |
| - | VecCreate(PETSC_COM M_WORLD,& x); | + | |
| + | | ||
| + | |||
| + | | ||
| - | VecSetSizes(x, | + | |
| - | VecSetFromOptions(x); | + | |
| - | VecSet(x, | + | |
| - | VecDot(x, | + | |
| - | PetscPrintf(PETSC_COMM_WORLD," | + | |
| - | VecDestroy(x); | + | |
| - | PetscFinalize(); | + | |
| - | return 0; | + | |
| } | } | ||
| + | |||
| + | |||
| </ | </ | ||
| Ligne 266: | Ligne 269: | ||
| |Transpose-free QMR| Arbitrary matrix | | |Transpose-free QMR| Arbitrary matrix | | ||
| |etc|etc| | |etc|etc| | ||
| + | |||
| + | |||
| + | ====== Interface Python ====== | ||
| + | Petsc4py est l' | ||
| + | Pour l' | ||
| + | <code bash> | ||
| + | $ module load lang/ | ||
| + | </ | ||
| + | |||
| + | Exemple d' | ||
| + | |||
| + | <file python petsc.py> | ||
| + | |||
| + | import petsc4py, sys | ||
| + | petsc4py.init(sys.argv) | ||
| + | |||
| + | from petsc4py import PETSc | ||
| + | |||
| + | from mpi4py import MPI | ||
| + | comm = MPI.COMM_WORLD | ||
| + | |||
| + | # grid size and spacing | ||
| + | m, n = 101, 101 | ||
| + | hx = 1.0/(m-1) | ||
| + | hy = 1.0/(n-1) | ||
| + | |||
| + | # create sparse matrix | ||
| + | A = PETSc.Mat() | ||
| + | A.create(PETSc.COMM_WORLD) | ||
| + | A.setSizes([m*n, | ||
| + | A.setType(' | ||
| + | A.setPreallocationNNZ(5) | ||
| + | |||
| + | # precompute values for setting | ||
| + | # diagonal and non-diagonal entries | ||
| + | diagv = 2.0/hx**2 + 2.0/hy**2 | ||
| + | offdx = -1.0/hx**2 | ||
| + | offdy = -1.0/hy**2 | ||
| + | |||
| + | # loop over owned block of rows on this | ||
| + | # processor and insert entry values | ||
| + | Istart, Iend = A.getOwnershipRange() | ||
| + | for I in range(Istart, | ||
| + | A[I,I] = diagv | ||
| + | i = I//n # map row number to | ||
| + | j = I - i*n # grid coordinates | ||
| + | if i> 0 : J = I-n; A[I,J] = offdx | ||
| + | if i< m-1: J = I+n; A[I,J] = offdx | ||
| + | if j> 0 : J = I-1; A[I,J] = offdy | ||
| + | if j< n-1: J = I+1; A[I,J] = offdy | ||
| + | # communicate off-processor values | ||
| + | # and setup internal data structures | ||
| + | # for performing parallel operations | ||
| + | A.assemble() | ||
| + | |||
| + | ksp = PETSc.KSP() | ||
| + | ksp.create(PETSc.COMM_WORLD) | ||
| + | # use conjugate gradients | ||
| + | ksp.setType(' | ||
| + | # and incomplete Cholesky | ||
| + | # | ||
| + | # obtain sol & rhs vectors | ||
| + | x, b = A.createVecs() | ||
| + | x.set(0) | ||
| + | b.set(1) | ||
| + | # and next solve | ||
| + | ksp.setOperators(A) | ||
| + | ksp.setFromOptions() | ||
| + | ksp.solve(b, | ||
| + | |||
| + | myviewer = PETSc.Viewer().createASCII(" | ||
| + | |||
| + | x.view(myviewer) | ||
| + | comm.Barrier() | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | Exécution : | ||
| + | <code bash> | ||
| + | $ mpirun -np 4 python petsc.py | ||
| + | </ | ||