Reuse matrix and vector

jarunan at jarunan at
Thu Nov 5 03:32:17 CST 2009


I would like to reuse matrix and vector to save computing time. As the  
result in last iterations are not similar to the one from my old  
solver, so I am not sure if I program with PETSc the right way,  
especially, resetting values to matrix with  
MatMPIAIJSetPreallocationCSR(). Please take a look, here is how I  
program it:

At the beginning of the program I create the vector and matrix.

call VecCreateMPI(PETSC_COMM_WORLD,istorf_no_ovcell,PETSC_DETERMINE,rhs,ierr)
call VecDuplicate(rhs,sol,ierr)

call MatCreate(PETSC_COMM_WORLD,Ap,ierr)
call MatSetType(Ap,MATMPIAIJ,ierr)

Then, in each loop I reset values in the vector and the matrix.

do niter = 1,maxiter

     call VecSetValues(rhs,w,gindex_issu(1:w),f_issu(1:w),INSERT_VALUES,ierr)

     call VecAssemblyBegin(rhs,ierr)
     call VecAssemblyEnd(rhs,ierr)

     call MatMPIAIJSetPreallocationCSR(Ap,rowind,columnind,A,ierr)

     call MatAssemblyBegin(Ap,MAT_FINAL_ASSEMBLY,ierr)
     call MatAssemblyEnd(Ap,MAT_FINAL_ASSEMBLY,ierr)

     call solve_system
     call update_right_hand_side


call MatDestroy(Ap,ierr)
call VecDestroy(sol,ierr)
call VecDestroy(rhs,ierr)


Jarunan Panyasantisuk
Development Engineer
ASCOMP GmbH, Technoparkstr. 1
CH-8005 Zurich, Switzerland
Phone : +41 44 445 4072
Fax   : +41 44 445 4075
E-mail: jarunan at

More information about the petsc-users mailing list