yes, exactly<br><br><div class="gmail_quote">On Sun, Oct 11, 2009 at 10:04 PM, Yaakoub El Khamra <span dir="ltr"><<a href="mailto:yelkhamra@gmail.com">yelkhamra@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
This is for Mayank's project isn't it<br>
<br>
Regards<br>
<font color="#888888">Yaakoub El Khamra<br>
</font><div><div></div><div class="h5"><br>
<br>
<br>
<br>
On Sun, Oct 11, 2009 at 9:59 PM, Yin Feng <<a href="mailto:yfeng1@tigers.lsu.edu">yfeng1@tigers.lsu.edu</a>> wrote:<br>
> The completed code is:<br>
><br>
> void PETSc(double mat[],double vec[],double sol[],long N,double tol) {<br>
><br>
> Vec x,b;<br>
><br>
> Mat A;<br>
><br>
> KSP ksp;<br>
><br>
> PC pc;<br>
><br>
> PetscInt i,j,col[N];<br>
><br>
> PetscScalar value[N];<br>
><br>
> PetscScalar val;<br>
><br>
> PetscInitialize(PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL);<br>
><br>
> VecCreate(PETSC_COMM_WORLD,&x);<br>
><br>
> VecSetSizes(x,PETSC_DECIDE,N);<br>
><br>
> VecSetFromOptions(x);<br>
><br>
> VecDuplicate(x,&b);<br>
><br>
> MatCreate(PETSC_COMM_WORLD,&A);<br>
><br>
> MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);<br>
><br>
> MatSetFromOptions(A);<br>
><br>
> for (i=0; i<N; i++) {<br>
><br>
> for (j=0;j<N;j++) {<br>
><br>
> value[j]=mat[i*N+j];<br>
><br>
> col[j]=j;<br>
><br>
> }<br>
><br>
> MatSetValues(A,1,&i,N,col,value,INSERT_VALUES);<br>
><br>
> }<br>
><br>
> MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);<br>
><br>
> MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);<br>
><br>
><br>
><br>
> for (i=0; i<N; i++) {<br>
><br>
> val=vec[i];<br>
><br>
> VecSetValues(b,1,&i,&val,INSERT_VALUES);<br>
><br>
> val=sol[i];<br>
><br>
> VecSetValues(x,1,&i,&val,INSERT_VALUES);<br>
><br>
> }<br>
><br>
> VecAssemblyBegin(b);<br>
><br>
> VecAssemblyEnd(b);<br>
><br>
> KSPCreate(PETSC_COMM_WORLD,&ksp);<br>
><br>
> KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);<br>
><br>
> KSPGetPC(ksp,&pc);<br>
><br>
> KSPSetType(ksp, KSPGMRES);<br>
><br>
> PCSetType(pc, PCBJACOBI);<br>
><br>
> KSPSetTolerances(ksp,tol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);<br>
><br>
> KSPSetFromOptions(ksp);<br>
><br>
> KSPSolve(ksp,b,x);<br>
><br>
> for (i=0;i<N;i++) {<br>
><br>
> VecGetValues(x,1,&i,&sol[i]);<br>
><br>
> }<br>
><br>
><br>
><br>
> VecDestroy(x);<br>
><br>
> VecDestroy(b);<br>
><br>
> MatDestroy(A);<br>
><br>
> KSPDestroy(ksp);<br>
><br>
> PetscFinalize();<br>
><br>
> }<br>
><br>
> Once this function is call in a loop, it reports error.<br>
> On Sun, Oct 11, 2009 at 9:38 PM, Yin Feng <<a href="mailto:yfeng1@tigers.lsu.edu">yfeng1@tigers.lsu.edu</a>> wrote:<br>
>><br>
>> I put PETSc solver in one function and use another function to call that.<br>
>> This problem only appears when I put the function with PETSc solver in a<br>
>> loop,<br>
>> it works well at first step, and reports error "An error occurred in<br>
>> MPI_Comm_rank after MPI was finalized"<br>
>> at second time. The program is designed to support only one processor<br>
>> like:<br>
>><br>
>> Vec x,b;<br>
>><br>
>> Mat A;<br>
>><br>
>> KSP ksp;<br>
>><br>
>> PC pc;<br>
>><br>
>> PetscInt i,j,col[N];<br>
>><br>
>> PetscScalar value[N];<br>
>><br>
>> PetscScalar val;<br>
>><br>
>> PetscInitialize(PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL);<br>
>><br>
>> VecCreate(PETSC_COMM_WORLD,&x);<br>
>><br>
>> VecSetSizes(x,PETSC_DECIDE,N);<br>
>><br>
>> VecSetFromOptions(x);<br>
>><br>
>> VecDuplicate(x,&b);<br>
>><br>
>> MatCreate(PETSC_COMM_WORLD,&A);<br>
>><br>
>> MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);<br>
>><br>
>> MatSetFromOptions(A);<br>
>><br>
>> ........<br>
>><br>
>> ........<br>
>><br>
>> KSPCreate(PETSC_COMM_WORLD,&ksp);<br>
>><br>
>> KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);<br>
>><br>
>> KSPGetPC(ksp,&pc);<br>
>><br>
>> ................<br>
>> ...............<br>
>><br>
>><br>
>> KSPSetTolerances(ksp,tol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);<br>
>><br>
>> KSPSetFromOptions(ksp);<br>
>><br>
>> KSPSolve(ksp,b,x);<br>
>><br>
>> VecDestroy(x);<br>
>><br>
>> VecDestroy(b);<br>
>><br>
>> MatDestroy(A);<br>
>><br>
>> KSPDestroy(ksp);<br>
>><br>
>> PetscFinalize();<br>
>><br>
>> Any one has ideal about this?<br>
>><br>
>> The detailed error description is:<br>
>><br>
>> An error occurred in MPI_Comm_rank<br>
>><br>
>> *** after MPI was finalized<br>
>><br>
>> *** MPI_ERRORS_ARE_FATAL (goodbye)<br>
>><br>
>> Abort before MPI_INIT completed successfully; not able to guarantee that<br>
>> all other processes were killed!<br>
>><br>
>> Thank you so much in advance!<br>
><br>
><br>
</div></div></blockquote></div><br>