How to write a program, which can be run on 1 and multiple processors?

Jed Brown jed at
Sun Apr 26 16:37:10 CDT 2009

Yixun Liu wrote:
> Hi,
> I want to make my code run on 1 or multiple processors. The code, which
> can run on multiple processors is like the following,
> MatCreate(PETSC_COMM_WORLD, &A);
>    MatSetSizes(A, 3*numOfVerticesOfOneProcessor,
> 3*numOfVerticesOfOneProcessor, systemSize, systemSize);

You don't have to provide both local and global size unless you want
PETSc to check that these numbers are compatible.

>    MatSetFromOptions(A);
>    MatMPIAIJSetPreallocation(A, 50, PETSC_NULL, 50, PETSC_NULL);
> However if I want to run on 1 processor I have to change the last code to:
> MatSeqAIJSetPreallocation(A,1000,PETSC_NULL);
you probably mean 100

> How to avoid changing code?

Call both always.  You can call {Seq,MPI}BAIJ preallocation while you're
at it.  The preallocation functions don't do anything unless they match
the matrix type that you have.


More information about the petsc-users mailing list