[petsc-users] Repeated Solves

Barry Smith bsmith at mcs.anl.gov
Wed Jul 20 16:59:49 CDT 2011


1) Do not call the MatAssembly stuff here, you haven't put values into the matrix so it doesn't need to be assembled

> //Setup hamiltonian
> ierr = MatCreate(PETSC_COMM_WORLD, &hamiltonian);CHKERRQ(ierr);
> ierr = MatSetType(hamiltonian, MATAIJ);CHKERRQ(ierr);
> ierr = MatSetSizes(hamiltonian, PETSC_DECIDE, PETSC_DECIDE, rows, columns);CHKERRQ(ierr);
> ierr = MatAssemblyBegin(hamiltonian, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
> ierr = MatAssemblyEnd(hamiltonian, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);

2)  Do not pull out the factored matrix into the variable hamiltonian in PCFactorGetMatrix(), use some other name for it.

> //Setup PETSc Solver
> ierr = KSPSetOperators(ksp, hamiltonian, hamiltonian, DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
> ierr = KSPSetUp(ksp);CHKERRQ(ierr);
> 	
> //Factor and solve
> ierr = PCFactorGetMatrix(pc, &hamiltonian);CHKERRQ(ierr);
> ierr = MatMatSolve(hamiltonian, identityMat, inverseHamiltonian);CHKERRQ(ierr);

3) Call MatZeroEntries() on hamiltonian immediately after you your MatMatSolve() so that it is empty and ready for the next values you are going to put in.

   Barry


On Jul 20, 2011, at 1:58 PM, Adam Byrd wrote:

> I'm attempting to integrate PETSc into an existing simulation and I have successfully gotten PETSc to handle the first matrix inversion, but I'm having trouble setting it up properly for subsequent inversions. It currently does the inversions without complaining, but gives incorrect output. I believe I need to 'reset' the matrix being inverted. I am inverting the same matrix each time, but the values and nonzero structure change. I create and set up the matrix, ksp, and pc once, then call a routine that fills the matrix and calls KSPSetOperators() before using MatMatSolve again. The routine is called multiple times.
> 
> I am including the actual code, but here is the outline of what I'm currently trying:
> 
> //Setup hamiltonian
> ierr = MatCreate(PETSC_COMM_WORLD, &hamiltonian);CHKERRQ(ierr);
> ierr = MatSetType(hamiltonian, MATAIJ);CHKERRQ(ierr);
> ierr = MatSetSizes(hamiltonian, PETSC_DECIDE, PETSC_DECIDE, rows, columns);CHKERRQ(ierr);
> ierr = MatAssemblyBegin(hamiltonian, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
> ierr = MatAssemblyEnd(hamiltonian, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
> 
> //Setup PETSc Solver
> ierr = KSPCreate(PETSC_COMM_WORLD, &ksp);CHKERRQ(ierr);
> ierr = KSPSetOperators(ksp, hamiltonian, hamiltonian, SAME_NONZERO_PATTERN);CHKERRQ(ierr); 
> ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
> ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
> ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
> ierr = test_invert(hamiltonian, inverseHamiltonian, identityMat, pc, ksp, ...);CHKERRQ(ierr);
> 
> 
> test_invert (looped part):
> 
> //Several MatSetValue() calls
> //Setup PETSc Hamiltonian
> ierr = MatAssemblyBegin(hamiltonian, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
> ierr = MatAssemblyEnd(hamiltonian, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
> 	
> //Setup PETSc Solver
> ierr = KSPSetOperators(ksp, hamiltonian, hamiltonian, DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
> ierr = KSPSetUp(ksp);CHKERRQ(ierr);
> 	
> //Factor and solve
> ierr = PCFactorGetMatrix(pc, &hamiltonian);CHKERRQ(ierr);
> ierr = MatMatSolve(hamiltonian, identityMat, inverseHamiltonian);CHKERRQ(ierr);
> 
> 
> What needs to be done to the matrix to clear it out and use it again? MatZeroEntries doesn't want to work on an unfactored matrix, plus the nonzero structure changes. Do I need to reset the inverseHamiltonian as well?
> 
> 
> <petsccntor.zip>



More information about the petsc-users mailing list