I put PETSc solver in one function and use another function to call that.<div>This problem only appears when I put the function with PETSc solver in a loop,</div><div>it works well at first step, and reports error "<span class="Apple-style-span" style="border-collapse: collapse; white-space: pre; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">An error occurred in MPI_Comm_rank after MPI was finalized<span class="Apple-style-span" style="border-collapse: separate; white-space: normal; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; ">"</span></span></div>
<div>at second time. The program is designed to support only one processor like:</div><div><br></div><div><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>Vec x,b;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>Mat A;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>KSP ksp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>PC pc;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>PetscInt i,j,col[N];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>PetscScalar value[N];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>PetscScalar val;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>PetscInitialize(PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>VecCreate(PETSC_COMM_WORLD,&x);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>VecSetSizes(x,PETSC_DECIDE,N);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>VecSetFromOptions(x);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>VecDuplicate(x,&b);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>MatCreate(PETSC_COMM_WORLD,&A);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>MatSetFromOptions(A);</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"> ........</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"> ........</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>KSPCreate(PETSC_COMM_WORLD,&ksp);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre">        </span>KSPGetPC(ksp,&pc);</p><p></p><div><span class="Apple-style-span" style="font-family: Monaco; font-size: 10px;"> ................</span></div>
<div><span class="Apple-style-span" style="font-family: Monaco; font-size: 10px;"> ...............</span></div><div><span class="Apple-style-span" style="font-family: Monaco; font-size: 10px;"><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco">
KSPSetTolerances(ksp,tol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"> KSPSetFromOptions(ksp);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"> KSPSolve(ksp,b,x);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400"><span class="Apple-style-span" style="color: rgb(0, 0, 0);"><br></span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"> VecDestroy(x);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"> VecDestroy(b);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"> MatDestroy(A);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"> KSPDestroy(ksp);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"> PetscFinalize();</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><br></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco">
Any one has ideal about this? </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco">The detailed error description is:</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco">
An error occurred in MPI_Comm_rank</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco">*** after MPI was finalized</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco">*** MPI_ERRORS_ARE_FATAL (goodbye)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco">Abort before MPI_INIT completed successfully; not able to guarantee that all other processes were killed!</p><div><br></div><div>Thank you so much in advance!</div>
<div><br></div><p></p></span></div></div>