[petsc-users] [BUG]: error occurs when calling PetscInitialize and PetscFinalize multiple times

Barry Smith bsmith at mcs.anl.gov
Fri May 11 14:23:34 CDT 2012


On May 11, 2012, at 1:58 PM, Mengda Wu wrote:

> Hello all,
> 
>    I am using Petsc-3.2-p7 complied under cygwin and using Petsc's uni-mpi implementation. I found I cannot call PetscInitialize and PetscFinalize multiple times.
> Here is the code (between ==) that can reproduce the error. It seems that the second PetscFinalize() generates an error at line 1221 at pinit.c 
> which is
>  ierr = MPI_Finalize();CHKERRQ(ierr);

   Actually out of the kindness of our own hearts you can call PetscInitialize() PetscFinalize() multiple times.  Can you please try this under petsc-dev (which is now frozen in preparation for our next release) and report any problems ASAP to petsc-maint at mcs.anl.gov

    Barry


Note: Since MPI_Init() can only be called once, if you wish to call PetscInitialize/PetscFinalize() multiple times with a real MPI you must call MPI_Init() ONCE before calling PetscInitialize() 

> 
> 
> ==============================================================
> #include "petscksp.h"
> #include <iostream>
> 
> int main(int argc,char *args[])
> {
>   PetscErrorCode ierr;
>   PetscMPIInt    size;
> 
>   PetscInitialize(&argc,&args,(char *)0, PETSC_NULL);
>   std::cout << "Initialize 1" << std::endl;
>   
>   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
>   if (size != 1) SETERRQ(PETSC_COMM_WORLD,1,"This is a uniprocessor example only!");
> 
>   ierr = PetscFinalize();
>   std::cout << "Finalize 1" << std::endl;
> 
>   PetscInitialize(&argc,&args,(char *)0, PETSC_NULL);
>   std::cout << "Initialize 2" << std::endl;
>   ierr = PetscFinalize();
>   std::cout << "Finalize 2" << std::endl;
> 
>   PetscInitialize(&argc,&args,(char *)0, PETSC_NULL);
>   std::cout << "Initialize 3" << std::endl;
>   ierr = PetscFinalize();
>   std::cout << "Finalize 3" << std::endl;
> 
>   PetscInitialize(&argc,&args,(char *)0, PETSC_NULL);
>   std::cout << "Initialize 4" << std::endl;
>   ierr = PetscFinalize();
>   std::cout << "Finalize 4" << std::endl;
> 
>   return 0;
> }
> ==============================================================
> 
> I got the following output:
> Initialize 1
> Finalize 1
> Initialize 2
> [0]PETSC ERROR: PetscFinalize() line 1221 in src/sys/objects/D:\Library\PETSc\pe
> tsc-3.2\src\sys\objects\pinit.c
> Finalize 2
> Initialize 3
> Options have not been enabled.
> You might have forgotten to call PetscInitialize().
> 
> This application has requested the Runtime to terminate it in an unusual way.
> Please contact the application's support team for more information.
> 
> 
> Thanks,
> Mengda
> 



More information about the petsc-users mailing list