what do you mean by setting the matrix type before the preallocation? I did it like this: call MatCreateMPIAIJ(PETSC_COMM_WORLD, Np, Np, Ng, Ng, & nmax, PETSC_NULL, nmax, PETSC_NULL, Jac, info) call MatSetLocalToGlobalMapping(Jac, lgm, info) (i also had d_nnz and o_nnz there, but I deleted it when I switched to matrix shell)