Hello,
For some small matrices, I'd like to use Petsc to perform direct LU
factorization on a sequential dense or AIJ matrix, and then use the factored
matrix later on via MatSolve. This occurs multiple times in my code, and each
factored matrix is in turn used multiple times.
I tried to wrap this factorization process in a function, which should return
the factored matrix, as follows:
void MatFactorize_Petsc(Mat &mat, Mat &mat_factored)
{
PC pc;
PCCreate(MPI_COMM_SELF, &pc);
PCSetOperators(pc, mat, mat);
PCSetType(pc, PCLU);
PCFactorSetMatSolverType(pc, MATSOLVERPETSC); // Or SuperLU
PCFactorSetUpMatSolverType(pc);
PCFactorGetMatrix(pc, &mat_factored);
PCSetUp(pc);
// PCDestroy(&pc);
return;
}
The command PCDestroy causes a segmentation fault, which I think happens
because retrieving the factored matrix does not increase the reference count.
Looking at the Petsc source, it basically returns a pointer to pc->data. So if
I want to use mat_factored outside the function, I cannot destroy the PC
object, which leads to memory leaks (as per Valgrind) even if I later call
MatDestroy(&mat_factored).
I tried using a temp matrix to get the factors, and then doing
MatDuplicate(temp, MAT_COPY_VALUES, &mat_factored), but MatDuplicate is not
allowed on factored matrices; same for MatConvert.
Is there a way I can achieve the desired behaviour, where mat_factored is not
"linked" to the pc or ksp object, keeping in mind that I'd like to be able to
choose between SuperLU, Petsc and SuperLU_dist solvers?
Thanks,
Shash