I would like to create a parallel solver using petsc4py as one component of a larger application. I can easily get PETSc.COMM_WORLD.getSize() to be 4 by calling

mpiexec -n 4 python my_prog.py

however, this creates 4 applications, whereas I only want the PETSc solve in parallel. Currently it is only using one process, and provides performance essentially the same as scipy's spsolve.

I can refactor such that the KSP solve is its own program and spawn that from the main program, using IO between them to send the matrices etc, but that seems less than ideal.

Is it possible for petsc4py to create its own mpi procs via petsc4py.init or somesuch, or specify the number of partitions programatically? Or am I missing something more fundamental to the whole question?

  • $\begingroup$ This sounds like you're trying to implement a thread-like shared memory programming model in an MPI process based environment. I won't say it's impossible, but I suspect you'll be fighting the intentions of the developers, q.v. mcs.anl.gov/petsc/miscellaneous/petscthreads.html $\endgroup$
    – origimbo
    Commented Mar 20, 2017 at 23:46
  • $\begingroup$ Thank you for the quick reply. Then mpiexec -n 4 python child_solver.py would be the best method? Is there a preferred comm protocol for petsc4py when passing A and b (from Ax=b) as numpy arrays from the main proc to the child_solver? $\endgroup$ Commented Mar 20, 2017 at 23:56

1 Answer 1


Starting with all the data on one process is not scalable, though it may be practical if you only want a modest amount of parallelism. It is possible to use mpi4py spawn, but if you really want the rest of your program to use only one process and you don't want the extra processes to exist except during your solve, I would just launch an independent parallel job for the PETSc solve. If you don't mind having the (idle) processes around when you're not using them, you can just have all your logic on rank=0; PETSc will communicate values assembled there to an appropriate process.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.