Is it possible to dynamically resize a sparse matrix in the Petsc library?

This may be a Petsc newbie question, but...

I'm using Petsc to solve a large sparse linear system. The initial creation of the matrix is fairly slow, which I'm given to understand is largely due to memory allocations; that's tolerable for the moment. My problem is that as the simulation progresses, the structure of my domain/mesh changes such that the size of the resulting matrix will need to be increased. Do I really need to create a brand new matrix for this each time, or is there a way to just resize an existing matrix? (i.e. in order to avoid the cost of re-allocating the entire matrix from scratch.)

I have tried calling MatSetSizes, but it gives me an error: "Cannot change/reset row sizes to [...]" Am I doing something wrong, or is this a fundamental limitation?

• There are ways to flexibly allocate, rather than knowing everything upfront, but no way to change the global matrix size. Since the data structures are compressed and optimized for application, I am not sure there is much that could be salvaged. I think a better strategy would be to apply the operator matrix-free, and use a cheap approximation to form a preconditioner from. Reforming the PC matrix is not a big deal. Dec 12, 2011 at 20:09

The general advice given on the mailing list is usually about efficient assembly.

The short answer is, no; this is a fundamental limitation. The longer answer is that, for one, there is no "end-all-be-all" sparse matrix implementation. In general, it will be faster to:

1. loop over your mesh / domain, counting how many entries you'll need in your assembled matrix
2. create / allocate said matrix
3. assemble matrix

Most of the time will be spent in the linear / nonlinear solvers, so the matrix assembly is small (well, at least that's the goal) compared to computing the solution.

You might want to check out creating a VecScatter; or even better, trying to use PETSc's DMDA (for structured) or Sieve (experimental, for unstructured and mostly FEM domains) to manage your mesh. These PETSc mesh objects will maintain much of the nitty-gritty partitioning, matrix creation, and vec scattering for you, so you don't have to write the MPI code yourself.

The short answer is no. Once a matrix has been assembled, you can't resize it, MatSetSizes() is used for setting the size before assembly.

I would look into making the matrix creation faster - if you preallocate the matrix appropriately, see for example this question in the PETSc FAQ.