Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
petsc [2016/01/26 15:21] – [Compilation] adminpetsc [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 les modules : **ompi** et **petsc**+PETSc dépend de MPI (Open MPI), pour compiler des programmes il faut charger le module: **numlib/petsc**
  
 <code bash> <code bash>
Ligne 50: Ligne 50:
 PetscSynchronizedPrintf( PETSC_COMM_WORLD,"Hello World from rank %d\n", rank ); PetscSynchronizedPrintf( PETSC_COMM_WORLD,"Hello World from rank %d\n", rank );
  
-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>Il est préférable d'utiliser la technique Makefile </note> 
-<code bash> 
  
 +<code> 
 mpicc hello_petsc.c -o hello_petsc \ mpicc hello_petsc.c -o hello_petsc \
  -I$PETSC_HOME/include \  -I$PETSC_HOME/include \
Ligne 93: Ligne 91:
  
 <code bash> <code bash>
-$ module load ompi petsc && make+$ module load  numlib/petsc && make
  
 </code> </code>
Ligne 166: Ligne 164:
  
 <file C Vec.c> <file C Vec.c>
 +#include "petscvec.h"
  
-#include petscvec.h +int main(int argc,char **argv)
-int m ain(int argc,char **argv)+
 { {
-Vec x; +    Vec x; 
-int n = 20,ierr; +    int n = 20,ierr; 
-PetscTruth flg; +     
-PetscScalar one = 1.0, dot; +    PetscScalar one = 1.0, dot; 
-PetscInitialize(& argc,& argv,0,0); +    
-PetscOptionsGetInt(PETSC_NULL,"-n",& n,PETSC_NULL); +    PetscInitialize(& argc,& argv,0,0); 
-VecCreate(PETSC_COM M_WORLD,& x);+    
 +    PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL); 
 +    
 +    VecCreate(PETSC_COMM_WORLD,&x);
  
-VecSetSizes(x,PETSC_DECIDE,n);+    VecSetSizes(x,PETSC_DECIDE,n);
  
-VecSetFromOptions(x);+    VecSetFromOptions(x);
  
-VecSet(x,one);+    VecSet(x,one);
  
-VecDot(x,x,& dot);+    VecDot(x,x,&dot);
  
-PetscPrintf(PETSC_COMM_WORLD,"Vector length % d\n",(int)dot);+    PetscPrintf(PETSC_COMM_WORLD,"Vector length % d\n",(int)dot);
  
-VecDestroy(x);+    VecDestroy(&x);
  
-PetscFinalize();+    PetscFinalize();
  
-return 0;+    return 0;
 } }
 +
 +
  
 </file> </file>
Ligne 266: Ligne 269:
 |Transpose-free QMR| Arbitrary matrix | |Transpose-free QMR| Arbitrary matrix |
 |etc|etc| |etc|etc|
 +
 +
 +====== Interface Python ======
 +Petsc4py est l'interface Python pour PETSc.
 +Pour l'utiliser il faut charger python :
 +<code bash>
 +$ module load lang/python/2.7.11
 +</code>
 +
 +Exemple d'utilisation
 +
 +<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, m*n])
 +A.setType('aij') # sparse
 +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, Iend) :
 +    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('cg')
 +# and incomplete Cholesky
 +#ksp.getPC().setType('icc')
 +# 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, x)
 +
 +myviewer = PETSc.Viewer().createASCII("test.txt", format=PETSc.Viewer.Format.ASCII_VTK,comm= PETSc.COMM_WORLD)
 +
 +x.view(myviewer)
 +comm.Barrier()
 +
 +
 +</file>
 +
 +Exécution :
 +<code bash>
 +$ mpirun -np 4 python petsc.py
 +</code>
  
  
  • petsc.1453821680.txt.gz
  • Dernière modification : 2016/01/26 15:21
  • de admin