2 ///home/couturie/work/petsc-3.5.1/arch-linux2-c-debug/bin/mpirun -np 1 ./ex10 -f ~/Downloads/crashbasis/crashbasis.bin -ksp_type gmres -pc_type none
4 //temps simi pour lui et nous
7 // /home/couturie/work/petsc-3.5.1/arch-linux2-c-debug/bin/mpirun -np 1 ./ex10 -f ~/Downloads/parabolic_fem/parabolic_fem.bin -ksp_type gmres -pc_type ilu
8 //converge avec nous mais lentement
14 // /home/couturie/work/petsc-3.5.1/arch-linux2-c-debug/bin/mpirun -np 1 ./ex10 -f ~/Downloads/epb3/epb3.bin -ksp_type fgmres -pc_type sor
15 //fonctionne avec ilu asm
17 //temps simi pour lui et nous
19 // /home/couturie/work/petsc-3.5.1/arch-linux2-c-debug/bin/mpirun -np 1 ./ex10 -f ~/Downloads/atmosmodj/atmosmodj.bin -ksp_type fgmres -pc_type none
22 // /home/couturie/work/petsc-3.5.1/arch-linux2-c-debug/bin/mpirun -np 1 ./ex10 -f ~/Downloads/bfwa398/bfwa398.bin -ksp_type gmres -pc_type none
25 // /home/couturie/work/petsc-3.5.1/arch-linux2-c-debug/bin/mpirun -np 1 ./ex10 -f ~/Downloads/torso3/torso3.bin -ksp_type gmres -pc_type none
28 static char help[] = "Reads a PETSc matrix and vector from a file and solves a linear system.\n\
29 This version first preloads and solves a small system, then loads \n\
30 another (larger) system and solves it as well. This example illustrates\n\
31 preloading of instructions with the smaller system so that more accurate\n\
32 performance monitoring can be done with the larger one (that actually\n\
33 is the system of interest). See the 'Performance Hints' chapter of the\n\
34 users manual for a discussion of preloading. Input parameters include\n\
35 -f0 <input_file> : first file to load (small system)\n\
36 -f1 <input_file> : second file to load (larger system)\n\n\
37 -nearnulldim <0> : number of vectors in the near-null space immediately following matrix\n\n\
38 -trans : solve transpose system instead\n\n";
40 This code can be used to test PETSc interface to other packages.\n\
41 Examples of command line options: \n\
42 ./ex10 -f0 <datafile> -ksp_type preonly \n\
44 -num_numfac <num_numfac> -num_rhs <num_rhs> \n\
45 -ksp_type preonly -pc_type lu -pc_factor_mat_solver_package superlu or superlu_dist or mumps \n\
46 -ksp_type preonly -pc_type cholesky -pc_factor_mat_solver_package mumps \n\
47 mpiexec -n <np> ./ex10 -f0 <datafile> -ksp_type cg -pc_type asm -pc_asm_type basic -sub_pc_type icc -mat_type sbaij
51 Concepts: KSP^solving a linear system
56 Include "petscksp.h" so that we can use KSP solvers. Note that this file
57 automatically includes:
58 petscsys.h - base PETSc routines petscvec.h - vectors
60 petscis.h - index sets petscksp.h - Krylov subspace methods
61 petscviewer.h - viewers petscpc.h - preconditioners
66 #define __FUNCT__ "main"
76 int KrylovMinimize(Mat A, Vec b, Vec x) {
81 PetscScalar gamma, alpha, oldgamma, beta;
82 PetscReal norm=20, Eprecision=1e-7, cgprec=1e-40;
83 PetscInt giter=0, ColS=8, col=0, Emaxiter=50000000, iter=0, iterations=20, Iiter=0;
95 Vec Alpha, p, ss, vect, r, q, Ax;
100 ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
101 ierr = KSPSetOperators(ksp,A,A);CHKERRQ(ierr);
105 ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
112 ierr = PetscPrintf(PETSC_COMM_WORLD, "Size of vector %D\n", size); CHKERRQ(ierr);
115 MatGetOwnershipRange(A,&aa,&bb);
117 // ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%D %D\n", aa,bb); CHKERRQ(ierr);
118 //PetscSynchronizedFlush(PETSC_COMM_WORLD);
121 ierr = MatCreate(PETSC_COMM_WORLD, &S);CHKERRQ(ierr);
122 ierr = MatSetSizes(S, bb-aa, PETSC_DECIDE, size, ColS); CHKERRQ(ierr);
123 ierr = MatSetType(S, MATDENSE); CHKERRQ(ierr);
124 ierr = MatSetUp(S); CHKERRQ(ierr);
126 ierr = MatGetOwnershipRange(S, &Istart, &Iend); CHKERRQ(ierr);
128 ierr = VecCreate(PETSC_COMM_WORLD, &Alpha); CHKERRQ(ierr);
129 ierr = VecSetSizes(Alpha, PETSC_DECIDE, ColS); CHKERRQ(ierr);
130 ierr = VecSetFromOptions(Alpha); CHKERRQ(ierr);
131 ierr = VecDuplicate(Alpha, &vect); CHKERRQ(ierr);
132 ierr = VecDuplicate(Alpha, &p); CHKERRQ(ierr);
133 ierr = VecDuplicate(Alpha, &ss); CHKERRQ(ierr);
134 ierr = VecDuplicate(b, &r); CHKERRQ(ierr);
135 ierr = VecDuplicate(b, &q); CHKERRQ(ierr);
136 ierr = VecDuplicate(b, &Ax); CHKERRQ(ierr);
138 ierr = VecDuplicate(b,&x_old);CHKERRQ(ierr);
139 ierr = VecDuplicate(b,&residu);CHKERRQ(ierr);
143 //indexes of row (these indexes are global)
144 ind_row = (PetscInt*)malloc(sizeof(PetscInt)*(Iend-Istart));
145 for(i=0; i<Iend-Istart; i++) ind_row[i] = i + Istart;
148 // ierr = KSPGMRESSetRestart(ksp, 16); CHKERRQ(ierr);
149 ierr = KSPSetTolerances(ksp, 1e-10, 1e-10, PETSC_DEFAULT, 30); CHKERRQ(ierr);
150 ierr = KSPSetInitialGuessNonzero(ksp, PETSC_TRUE); CHKERRQ(ierr);
154 //GMRES WITH MINIMIZATION
156 ierr = KSPSetUp(ksp);CHKERRQ(ierr);
157 while(giter<Emaxiter && norm>Eprecision ){
158 for(col=0; col<ColS && norm>Eprecision; col++){
162 ierr = KSPSolve(ksp, b, x); CHKERRQ(ierr);
164 ierr = KSPGetIterationNumber(ksp, &its); CHKERRQ(ierr);
165 total += its; Iiter ++;
170 ierr = VecGetArray(x, &array);
171 ierr = MatSetValues(S, Iend-Istart, ind_row, 1, &col, array, INSERT_VALUES);
172 VecRestoreArray(x, &array);
176 KSPGetResidualNorm(ksp,&norm);
179 ierr = VecCopy(x, residu); CHKERRQ(ierr);
180 ierr = VecAXPY(residu, -1, x_old); CHKERRQ(ierr);
181 ierr = VecNorm(residu, NORM_INFINITY, &norm); CHKERRQ(ierr);
185 ierr = PetscPrintf(PETSC_COMM_WORLD, "Norm of error %g, outer iteration %D\n", norm, giter); CHKERRQ(ierr);
186 //ierr = VecCopy(x, x_old); CHKERRQ(ierr);
193 if( norm>Eprecision) {
195 MatAssemblyBegin(S, MAT_FINAL_ASSEMBLY);
196 MatAssemblyEnd(S, MAT_FINAL_ASSEMBLY);
201 MatMatMult(A,S, MAT_INITIAL_MATRIX,PETSC_DEFAULT,&AS);
205 MatMatMult(A,S, MAT_REUSE_MATRIX,PETSC_DEFAULT,&AS);
210 //Minimization with CGLS
211 MatMult(AS, Alpha, r); VecAYPX(r, -1, b); //r_0 = b-AS*x_0
214 MatMultTranspose(AS, r, p); //p_0 = AS'*r_0
219 ierr = VecCopy(p, ss); CHKERRQ(ierr); //p_0 = s_0
220 VecNorm(ss, NORM_2, &gamma); gamma = gamma * gamma; //gamma = norm2(s)^2
221 while(gamma>cgprec && iter<iterations){
222 MatMult(AS, p, q); //q = AS*p
223 VecNorm(q, NORM_2, &alpha); alpha = alpha * alpha; //alpha = norm2(q)^2
224 alpha = gamma / alpha;
225 VecAXPY(Alpha, alpha, p); //Alpha += alpha*p
226 VecAXPY(r, -alpha, q); //r -= alpha*q
227 MatMultTranspose(AS, r, ss); // ss = AS'*r
229 VecNorm(ss, NORM_2, &gamma); gamma = gamma * gamma; //gamma = norm2(s)^2
230 beta = gamma / oldgamma;
231 VecAYPX(p, beta, ss); //p = s+beta*p;
237 MatMult(S, Alpha, x); //x = S*Alpha;
242 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Execution time : %g (s)\n", T2-T1); CHKERRQ(ierr);
243 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Total number of iterations : %D\n", total); CHKERRQ(ierr);
257 int KrylovMinimizeLSQR(Mat A, Vec b, Vec x) {
262 PetscScalar alpha, beta;
263 PetscReal norm=20, Eprecision=1e-7, tol=1e-40;
264 PetscInt giter=0, ColS=8, col=0, Emaxiter=50000000, iter=0, iterations=20, Iiter=0;
270 PetscInt Istart,Iend;
277 PetscScalar norm_ksp;
278 Vec u,v,d,uu,vt,zero_long,zero_short,x_lsqr;
282 ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
283 ierr = KSPSetOperators(ksp,A,A);CHKERRQ(ierr);
284 ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
291 ierr = PetscPrintf(PETSC_COMM_WORLD, "Size of vector %D\n", size); CHKERRQ(ierr);
294 MatGetOwnershipRange(A,&aa,&bb);
296 // ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%D %D\n", aa,bb); CHKERRQ(ierr);
297 //PetscSynchronizedFlush(PETSC_COMM_WORLD);
300 ierr = MatCreate(PETSC_COMM_WORLD, &S);CHKERRQ(ierr);
301 ierr = MatSetSizes(S, bb-aa, PETSC_DECIDE, size, ColS); CHKERRQ(ierr);
302 ierr = MatSetType(S, MATDENSE); CHKERRQ(ierr);
303 ierr = MatSetUp(S); CHKERRQ(ierr);
305 ierr = MatGetOwnershipRange(S, &Istart, &Iend); CHKERRQ(ierr);
309 ierr = VecDuplicate(b, &Ax); CHKERRQ(ierr);
311 ierr = VecDuplicate(b,&x_old);CHKERRQ(ierr);
312 ierr = VecDuplicate(b,&residu);CHKERRQ(ierr);
316 ierr = VecDuplicate(b,&u);CHKERRQ(ierr);
319 ierr = VecDuplicate(b,&uu);CHKERRQ(ierr);
320 ierr = VecDuplicate(b,&zero_long);CHKERRQ(ierr);
321 ierr = VecSet(zero_long,0);CHKERRQ(ierr);
324 ierr = VecCreate(PETSC_COMM_WORLD, &v); CHKERRQ(ierr);
325 ierr = VecSetSizes(v, PETSC_DECIDE, ColS); CHKERRQ(ierr);
326 ierr = VecSetFromOptions(v); CHKERRQ(ierr);
327 ierr = VecDuplicate(v,&zero_short);CHKERRQ(ierr);
328 ierr = VecSet(zero_short,0);CHKERRQ(ierr);
329 ierr = VecDuplicate(v,&d);CHKERRQ(ierr);
330 ierr = VecDuplicate(v,&vt);CHKERRQ(ierr);
331 ierr = VecDuplicate(v,&x_lsqr);CHKERRQ(ierr);
334 //indexes of row (these indexes are global)
335 ind_row = (PetscInt*)malloc(sizeof(PetscInt)*(Iend-Istart));
336 for(i=0; i<Iend-Istart; i++) ind_row[i] = i + Istart;
339 // ierr = KSPGMRESSetRestart(ksp, 16); CHKERRQ(ierr);
340 ierr = KSPSetTolerances(ksp, 1e-10, 1e-10, PETSC_DEFAULT, 30); CHKERRQ(ierr);
341 ierr = KSPSetInitialGuessNonzero(ksp, PETSC_TRUE); CHKERRQ(ierr);
346 //GMRES WITH MINIMIZATION
348 ierr = KSPSetUp(ksp);CHKERRQ(ierr);
349 while(giter<Emaxiter && norm>Eprecision ){
350 for(col=0; col<ColS && norm>Eprecision; col++){
354 ierr = KSPSolve(ksp, b, x); CHKERRQ(ierr);
356 ierr = KSPGetIterationNumber(ksp, &its); CHKERRQ(ierr);
357 total += its; Iiter ++;
362 ierr = VecGetArray(x, &array);
363 ierr = MatSetValues(S, Iend-Istart, ind_row, 1, &col, array, INSERT_VALUES);
364 VecRestoreArray(x, &array);
368 KSPGetResidualNorm(ksp,&norm);
372 ierr = VecCopy(x, residu); CHKERRQ(ierr);
373 ierr = VecAXPY(residu, -1, x_old); CHKERRQ(ierr);
374 ierr = VecNorm(residu, NORM_INFINITY, &norm); CHKERRQ(ierr);
378 ierr = PetscPrintf(PETSC_COMM_WORLD, "Norm of error %g, outer iteration %D\n", norm, giter); CHKERRQ(ierr);
379 //ierr = VecCopy(x, x_old); CHKERRQ(ierr);
386 if( norm>Eprecision) {
388 MatAssemblyBegin(S, MAT_FINAL_ASSEMBLY);
389 MatAssemblyEnd(S, MAT_FINAL_ASSEMBLY);
396 MatMatMult(A,S, MAT_INITIAL_MATRIX,PETSC_DEFAULT,&AS);
401 MatMatMult(A,S, MAT_REUSE_MATRIX,PETSC_DEFAULT,&AS);
413 PetscScalar n2b,tolb,normr,c,s,phibar,normar,norma,thet,rhot,rho,phi;
416 VecNorm(b, NORM_2, &n2b); //n2b = norm(b);
417 ierr = VecCopy(b, u); CHKERRQ(ierr); //u=b
418 VecNorm(u, NORM_2, &beta); // beta=norm(u)
421 VecAYPX(u,1/beta,zero_long); // u = u / beta;
426 MatMultTranspose(AS, u, v); //v=A'*u
427 ierr = VecSet(x_lsqr,0);CHKERRQ(ierr);
428 VecNorm(v, NORM_2, &alpha); // alpha=norm(v)
430 VecAYPX(v,1/alpha,zero_short); // v = v / alpha;
432 ierr = VecSet(d,0);CHKERRQ(ierr);
433 normar = alpha * beta;
436 for(i=0;i<iterations;i++) {
437 MatMult(AS, v, uu); //uu=A*v
438 VecAYPX(u, -alpha, uu); //u = uu-alpha*u;
439 VecNorm(u, NORM_2, &beta); // beta=norm(u)
440 VecAYPX(u,1/beta,zero_long); // u = u / beta;
441 norma=sqrt(norma*norma+alpha*alpha+beta*beta); // norma = norm([norma alpha beta]);
444 rho = sqrt(rhot*rhot + beta*beta);
448 if (phi == 0) { // stagnation of the method
452 VecAYPX(d,-thet,v); //d = (v - thet * d);
453 VecAYPX(d,1/rho,zero_short); //d=d/ rho;
456 if (normar/(norma*normr) <= tol) { // check for convergence in min{|b-A*x|}
459 if (normr <= tolb) { // check for convergence in A*x=b
464 VecAXPY(x_lsqr,phi,d); // x_lsqr=x_lsqr+phi*d
465 normr = abs(s) * normr;
466 MatMultTranspose(AS, u, vt); //vt=A'*u;
467 VecAYPX(v,-beta,vt); // v = vt - beta * v;
468 VecNorm(v, NORM_2, &alpha); // alpha=norm(v)
469 VecAYPX(v,1/alpha,zero_short); // v = v / alpha;
470 normar = alpha * abs( s * phi);
477 MatMult(S, x_lsqr, x); //x = S*x_small;
482 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Execution time LSQR : %g (s)\n", T2-T1); CHKERRQ(ierr);
483 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Total number of iterations LSQR : %D\n", total); CHKERRQ(ierr);
502 int main(int argc,char **args)
504 KSP ksp; /* linear solver context */
505 Mat A,A2; /* matrix */
506 Vec x,b,u; /* approx solution, RHS, exact solution */
507 PetscViewer fd; /* viewer */
508 char file[4][PETSC_MAX_PATH_LEN]; /* input file name */
509 PetscBool table =PETSC_FALSE,flg,trans=PETSC_FALSE,initialguess = PETSC_FALSE;
510 PetscBool outputSoln=PETSC_FALSE;
512 PetscInt its,num_numfac,m,n,M,nearnulldim = 0;
514 PetscBool preload=PETSC_TRUE,isSymmetric,cknorm=PETSC_FALSE,initialguessfile = PETSC_FALSE;
516 char initialguessfilename[PETSC_MAX_PATH_LEN];
518 PetscInitialize(&argc,&args,(char*)0,help);
519 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
521 ierr = PetscOptionsGetString(NULL,"-f",file[0],PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
523 ierr = PetscStrcpy(file[1],file[0]);CHKERRQ(ierr);
524 preload = PETSC_FALSE;
526 ierr = PetscOptionsGetString(NULL,"-f0",file[0],PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
527 if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f0 or -f option");
528 ierr = PetscOptionsGetString(NULL,"-f1",file[1],PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
529 if (!flg) preload = PETSC_FALSE; /* don't bother with second system */
533 PetscPreLoadBegin(preload,"Load system");
537 ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[PetscPreLoadIt],FILE_MODE_READ,&fd);CHKERRQ(ierr);
539 ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
540 ierr = MatSetFromOptions(A);CHKERRQ(ierr);
541 ierr = MatLoad(A,fd);CHKERRQ(ierr);
543 /* char ordering[256] = MATORDERINGRCM;
544 IS rowperm = NULL,colperm = NULL;
545 ierr = MatGetOrdering(A2,ordering,&rowperm,&colperm);CHKERRQ(ierr);
546 ierr = MatPermute(A2,rowperm,colperm,&A);CHKERRQ(ierr);
549 ierr = VecCreate(PETSC_COMM_WORLD,&b);CHKERRQ(ierr);
551 ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr);
553 ierr = MatGetLocalSize(A,&m,NULL);CHKERRQ(ierr);
554 ierr = VecSetSizes(b,m,PETSC_DECIDE);CHKERRQ(ierr);
555 ierr = VecSetFromOptions(b);CHKERRQ(ierr);
556 ierr = VecSet(b,1);CHKERRQ(ierr);
561 ierr = MatGetVecs(A,&x,NULL);CHKERRQ(ierr);
562 ierr = VecDuplicate(b,&u);CHKERRQ(ierr);
564 ierr = VecSet(x,1.0);CHKERRQ(ierr);
567 ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
568 ierr = KSPSetInitialGuessNonzero(ksp,initialguess);CHKERRQ(ierr);
575 ierr = KSPSetOperators(ksp,A,A);CHKERRQ(ierr);
577 ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
579 ierr = KSPSetTolerances(ksp,1e-10,1e-10,PETSC_DEFAULT,5000000);CHKERRQ(ierr);
583 PCGetType(pc, &type);
585 PetscPrintf(PETSC_COMM_WORLD, "PC TYPE %s \n", type);
590 ierr = KSPSetUp(ksp);CHKERRQ(ierr);
591 ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
595 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Execution time : %g (s)\n", T2-T1); CHKERRQ(ierr);
596 ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr);
600 ierr = MatMult(A,x,u);CHKERRQ(ierr);
601 ierr = VecAXPY(u,-1.0,b);CHKERRQ(ierr);
602 ierr = VecNorm(u,NORM_2,&norm);CHKERRQ(ierr);
603 ierr = PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g iterations %D\n",(double)norm,its);CHKERRQ(ierr);
610 VecDuplicate(b,&sol);
612 KrylovMinimize(A, b, x2);
618 VecNorm(sol, NORM_2, &norm);
619 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Error Krylov Minimization %g\n",norm);
628 VecDuplicate(b,&sol);
630 KrylovMinimizeLSQR(A, b, x2);
636 VecNorm(sol, NORM_2, &norm);
637 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Error KrylovLSQR Minimization %g\n",norm);
643 Free work space. All PETSc objects should be destroyed when they
644 are no longer needed.
646 ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = VecDestroy(&b);CHKERRQ(ierr);
647 ierr = VecDestroy(&u);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr);
648 ierr = KSPDestroy(&ksp);CHKERRQ(ierr);
650 /* -----------------------------------------------------------
651 End of linear solver loop
652 ----------------------------------------------------------- */
654 ierr = PetscFinalize();