<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
Thanks to all of you.<BR>
<BR>
Yes, Ethan is correct. I just changed the code as shown as below, and the result is correct:<BR>
!========================<BR> do i=1,Iend-Istart<BR> write(6,*)'check xx_v',i,xx_v(i),myid<BR> enddo <BR>
!========================<BR>
Now, I got the main concept of the pointer to the array, it is locally and begin with 1. If I want to use them globally, I should convert the local index to the global.<BR>
<BR>
Thanks again for all the considerate responses. Your advices always clear the trouble on the way to the final goal. I appreciate very much.<BR> <BR>
> From: ecoon@lanl.gov<BR>> To: petsc-users@mcs.anl.gov<BR>> Date: Wed, 5 Jan 2011 12:52:20 -0700<BR>> Subject: Re: [petsc-users] error in calling VecGetArrayf90()<BR>> <BR>> See the below program and output (done with dev, but I don't think this<BR>> has changed). In F90, the local arrays, gotten by VecGetArrayF90, are<BR>> all indexed:<BR>> <BR>> xx_v(1:(Iend-Istart)). <BR>> <BR>> This should really be moot -- It's way easier to follow the example code<BR>> for ex50f90.F and pass their arrays into a subroutine which casts the<BR>> shape in the "PETSc way", indexed from (Istart:Iend-1). <BR>> <BR>> Ethan<BR>> <BR>> <BR>> ---<BR>> <BR>> <BR>> #define PETSC_USE_FORTRAN_MODULES 1<BR>> #include "finclude/petscsysdef.h"<BR>> #include "finclude/petscvecdef.h"<BR>> <BR>> program test<BR>> use petsc<BR>> implicit none<BR>> <BR>> Vec x<BR>> PetscInt Istart, Iend<BR>> PetscScalar, pointer:: xx_v(:)<BR>> PetscInt i<BR>> PetscErrorCode ierr<BR>> PetscInt twenty<BR>> PetscInt rank<BR>> <BR>> twenty = 20<BR>> <BR>> call PetscInitialize(PETSC_NULL_CHARACTER, ierr)<BR>> call mpi_comm_rank(PETSC_COMM_WORLD,rank,ierr)<BR>> call VecCreateMPI(PETSC_COMM_WORLD, PETSC_DECIDE, twenty, x, ierr)<BR>> <BR>> call VecGetArrayF90(x, xx_v, ierr)<BR>> xx_v = rank+1<BR>> call VecRestoreArrayF90(x, xx_v, ierr)<BR>> <BR>> call VecView(x, PETSC_VIEWER_STDOUT_WORLD, ierr)<BR>> <BR>> call VecGetOwnershipRange(x, Istart, Iend)<BR>> write (*,*) 'range on rank', rank, ':', Istart, Iend<BR>> <BR>> call VecGetArrayF90(x, xx_v, ierr)<BR>> CHKMEMQ<BR>> do i = 1,(Iend-Istart)<BR>> write (*,*) i, xx_v(i), rank<BR>> enddo<BR>> CHKMEMQ<BR>> call VecRestoreArrayF90(x, xx_v, ierr)<BR>> <BR>> call VecDestroy(x, ierr)<BR>> call PetscFinalize(ierr)<BR>> end program test<BR>> <BR>> -----<BR>> <BR>> $> ${PETSC_DIR}/${PETSC_ARCH}/bin/mpiexec -n 2 ./test<BR>> Vector Object:<BR>> type: mpi<BR>> Process [0]<BR>> 1<BR>> 1<BR>> 1<BR>> 1<BR>> 1<BR>> 1<BR>> 1<BR>> 1<BR>> 1<BR>> 1<BR>> Process [1]<BR>> 2<BR>> 2<BR>> 2<BR>> 2<BR>> 2<BR>> 2<BR>> 2<BR>> 2<BR>> 2<BR>> 2<BR>> range on rank 1 : 10 20<BR>> 1 2.0000000000000000 1<BR>> 2 2.0000000000000000 1<BR>> 3 2.0000000000000000 1<BR>> 4 2.0000000000000000 1<BR>> 5 2.0000000000000000 1<BR>> 6 2.0000000000000000 1<BR>> 7 2.0000000000000000 1<BR>> 8 2.0000000000000000 1<BR>> 9 2.0000000000000000 1<BR>> 10 2.0000000000000000 1<BR>> range on rank 0 : 0 10<BR>> 1 1.00000000000000000 0<BR>> 2 1.00000000000000000 0<BR>> 3 1.00000000000000000 0<BR>> 4 1.00000000000000000 0<BR>> 5 1.00000000000000000 0<BR>> 6 1.00000000000000000 0<BR>> 7 1.00000000000000000 0<BR>> 8 1.00000000000000000 0<BR>> 9 1.00000000000000000 0<BR>> 10 1.00000000000000000 0<BR>> <BR>> ---<BR>> <BR>> <BR>> On Wed, 2011-01-05 at 13:21 -0600, Barry Smith wrote:<BR>> > I don't think this is correct. You are suppose to use the local indexing for each process. With the strange index starting at 1 instead of 0.<BR>> > <BR>> > <BR>> > Barry<BR>> > <BR>> > <BR>> > On Jan 5, 2011, at 1:03 PM, Ethan Coon wrote:<BR>> > <BR>> > > On all processors, the array you get is indexed:<BR>> > > <BR>> > > xx_v(1:(Iend-Istart))<BR>> > > <BR>> > > while the Istart and Iend are global indices into the global Vec.<BR>> > > <BR>> > > It's only by luck that the values on proc 3 are correct (this code<BR>> > > should probably seg fault as it is accessing memory outside of the<BR>> > > bounds of xx_v).<BR>> > > <BR>> > > To access the array, you'll want:<BR>> > > <BR>> > > do i=1, (Iend-Istart)<BR>> > > write(6,*)'check xx_v',i,xx_v(i),myid<BR>> > > enddo<BR>> > > <BR>> > > or (better yet) pass it into a subroutine to get the array indexed<BR>> > > correctly, like demonstrated in the example.<BR>> > > <BR>> > > Ethan<BR>> > > <BR>> > > On Wed, 2011-01-05 at 11:23 -0600, Peter Wang wrote:<BR>> > >> Thanks, Satish,<BR>> > >> <BR>> > >> The index of the array is modified to i+1:<BR>> > >> !===================<BR>> > >> do i=Istart,Iend-1<BR>> > >> write(6,*)'check xx_v',i+1,xx_v(i+1),myid<BR>> > >> enddo <BR>> > >> !===================<BR>> > >> <BR>> > >> However, only the elements on root process (process 0) and the<BR>> > >> last process (process 3) are corrent, is there any ohter logical<BR>> > >> error?<BR>> > >> <BR>> > >> check xx_v 1 3999.9999999999982 0<BR>> > >> check xx_v 2 3999.9999999999982 0<BR>> > >> check xx_v 3 3999.9999999999982 0<BR>> > >> check xx_v 4 3999.9999999999982 0<BR>> > >> check xx_v 5 3999.9999999999982 0<BR>> > >> check xx_v 6 3000.0000000000005 0<BR>> > >> check xx_v 7 3000.0000000000005 0<BR>> > >> check xx_v 8 2.61360726650019422E-321 1 <BR>> > >> check xx_v 9 7.90505033345994471E-323 1<BR>> > >> check xx_v 10 1.69759663277221785E-312 1<BR>> > >> check xx_v 11 6.16840020108069212E-317 1<BR>> > >> check xx_v 12 6.16840316547456717E-317 1<BR>> > >> check xx_v 13 6.16832658529946177E-317 1<BR>> > >> check xx_v 14 1.99665037664579820E-314 2<BR>> > >> check xx_v 15 6.19784009071943448E-317 2<BR>> > >> check xx_v 16 6.20249221284067566E-317 2<BR>> > >> check xx_v 17 6.20218737433719161E-317 2<BR>> > >> check xx_v 18 6.18236051996958238E-317 2<BR>> > >> check xx_v 19 6.16840316547456717E-317 2<BR>> > >> check xx_v 20 6.18107199676522841E-317 3<BR>> > >> check xx_v 21 0.0000000000000000 3<BR>> > >> check xx_v 22 0.0000000000000000 3<BR>> > >> check xx_v 23 0.0000000000000000 3<BR>> > >> check xx_v 24 0.0000000000000000 3<BR>> > >> check xx_v 25 0.0000000000000000 3<BR>> > >> <BR>> > >> <BR>> > >> <BR>> > >>> Date: Tue, 4 Jan 2011 22:49:50 -0600<BR>> > >>> From: balay@mcs.anl.gov<BR>> > >>> To: petsc-users@mcs.anl.gov<BR>> > >>> Subject: Re: [petsc-users] error in calling VecGetArrayf90()<BR>> > >>> <BR>> > >>> The global index starts at Istart - but the array index starts at 1<BR>> > >> [for fortran array]<BR>> > >>> <BR>> > >>> Satish<BR>> > >>> <BR>> > >>> On Tue, 4 Jan 2011, Peter Wang wrote:<BR>> > >>> <BR>> > >>>> <BR>> > >>>> In last question, the pointer xx_v is local data. However, if<BR>> > >> write them to the monitor or assign them to another array, the value<BR>> > >> is incorrect.<BR>> > >>>> <BR>> > >>>> The protion of the code to display them on the monitor is like as<BR>> > >> following: <BR>> > >>>> call MatGetOwnershipRange(A,Istart,Iend,ierr)<BR>> > >>>> call VecGetArrayF90(x,xx_v,ierr) ! Vector x is matched with Matrix<BR>> > >> A in the same communicator<BR>> > >>>> <BR>> > >>>> write(*,*)xx_v,myid ! write the poiner array together<BR>> > >>>> <BR>> > >>>> do i=Istart,Iend-1<BR>> > >>>> write(6,*)'check xx_v',i,xx_v(i),myid !write the element of the<BR>> > >> array one by one with local range (Istart to Iend-1)<BR>> > >>>> enddo <BR>> > >>>> <BR>> > >>>> <BR>> > >>>> =========The result is as following: ( the values of the elements<BR>> > >> from 7 to 20 are not correct !!)<BR>> > >>>> <BR>> > >>>> 3999.9999999999982 3999.9999999999982 3999.9999999999982<BR>> > >> 3999.9999999999982 3999.9999999999982 3000.0000000000005<BR>> > >> 3000.0000000000005 0<BR>> > >>>> <BR>> > >>>> 3000.0000000000009 3000.0000000000009 3000.0000000000009<BR>> > >> 2000.0000000000011 2000.0000000000011 2000.0000000000000 1<BR>> > >>>> <BR>> > >>>> 2000.0000000000009 2000.0000000000009 1000.0000000000003<BR>> > >> 1000.0000000000003 1000.0000000000003 999.99999999999989 2<BR>> > >>>> <BR>> > >>>> 1000.0000000000000 0.0000000000000000 0.0000000000000000<BR>> > >> 0.0000000000000000 0.0000000000000000 0.0000000000000000 3<BR>> > >>>> <BR>> > >>>> <BR>> > >>>> check xx_v 0 0.0000000000000000 0.0000000000000000 0<BR>> > >>>> check xx_v 1 3999.9999999999982 3999.9999999999982 0<BR>> > >>>> check xx_v 2 3999.9999999999982 3999.9999999999982 0<BR>> > >>>> check xx_v 3 3999.9999999999982 3999.9999999999982 0<BR>> > >>>> check xx_v 4 3999.9999999999982 3999.9999999999982 0<BR>> > >>>> check xx_v 5 3999.9999999999982 3999.9999999999982 0<BR>> > >>>> check xx_v 6 3000.0000000000005 3000.0000000000005 0<BR>> > >>>> check xx_v 7 1.99665037664579820E-314 1.99665037664579820E-314 1<BR>> > >>>> check xx_v 8 2.61360726650019422E-321 2.61360726650019422E-321 1<BR>> > >>>> check xx_v 9 7.90505033345994471E-323 7.90505033345994471E-323 1<BR>> > >>>> check xx_v 10 1.69759663277221785E-312 1.69759663277221785E-312 1<BR>> > >>>> check xx_v 11 6.16846344148335980E-317 6.16846344148335980E-317 1<BR>> > >>>> check xx_v 12 6.16846640587723485E-317 6.16846640587723485E-317 1<BR>> > >>>> check xx_v 13 6.16838982570212945E-317 6.16838982570212945E-317 2<BR>> > >>>> check xx_v 14 1.99665037664579820E-314 1.99665037664579820E-314 2<BR>> > >>>> check xx_v 15 6.19790333112210216E-317 6.19790333112210216E-317 2<BR>> > >>>> check xx_v 16 6.20255545324334334E-317 6.20255545324334334E-317 2<BR>> > >>>> check xx_v 17 6.20225061473985929E-317 6.20225061473985929E-317 2<BR>> > >>>> check xx_v 18 6.18242376037225006E-317 6.18242376037225006E-317 2<BR>> > >>>> check xx_v 19 6.16846640587723485E-317 6.16846640587723485E-317 3<BR>> > >>>> check xx_v 20 6.18113523716789609E-317 6.18113523716789609E-317 3<BR>> > >>>> check xx_v 21 0.0000000000000000 0.0000000000000000 3<BR>> > >>>> check xx_v 22 0.0000000000000000 0.0000000000000000 3<BR>> > >>>> check xx_v 23 0.0000000000000000 0.0000000000000000 3<BR>> > >>>> check xx_v 24 0.0000000000000000 0.0000000000000000 3<BR>> > >>>> <BR>> > >>>> ======The vector x is :<BR>> > >>>> Process [0]<BR>> > >>>> 4000<BR>> > >>>> 4000<BR>> > >>>> 4000<BR>> > >>>> 4000<BR>> > >>>> 4000<BR>> > >>>> 3000<BR>> > >>>> 3000<BR>> > >>>> Process [1]<BR>> > >>>> 3000<BR>> > >>>> 3000<BR>> > >>>> 3000<BR>> > >>>> 2000<BR>> > >>>> 2000<BR>> > >>>> 2000<BR>> > >>>> Process [2]<BR>> > >>>> 2000<BR>> > >>>> 2000<BR>> > >>>> 1000<BR>> > >>>> 1000<BR>> > >>>> 1000<BR>> > >>>> 1000<BR>> > >>>> Process [3]<BR>> > >>>> 1000<BR>> > >>>> 0<BR>> > >>>> 0<BR>> > >>>> 0<BR>> > >>>> 0<BR>> > >>>> 0<BR>> > >>>> <BR>> > >>>> <BR>> > >>>> <BR>> > >>>> <BR>> > >>>>> Date: Tue, 4 Jan 2011 17:50:11 -0600<BR>> > >>>>> From: balay@mcs.anl.gov<BR>> > >>>>> To: petsc-users@mcs.anl.gov<BR>> > >>>>> Subject: Re: [petsc-users] error in calling VecGetArrayf90()<BR>> > >>>>> <BR>> > >>>>> Did you included "finclude/petscvec.h90" in your code - as the<BR>> > >> example did?<BR>> > >>>>> <BR>> > >>>>> satish<BR>> > >>>>> <BR>> > >>>>> On Tue, 4 Jan 2011, Peter Wang wrote:<BR>> > >>>>> <BR>> > >>>>>> <BR>> > >>>>>> I am trying to obtain the value of each element of a solution<BR>> > >> Vector by KSPsolve(). <BR>> > >>>>>> <BR>> > >>>>>> The variables are defined according the example of ex4f90.F in<BR>> > >> \petsc-3.1-p5\src\snes\examples\tutorials\ as following,<BR>> > >>>>>> <BR>> > >>>>>> PetscScalar, pointer :: xx_v(:)<BR>> > >>>>>> <BR>> > >>>>>> ...<BR>> > >>>>>> call KSPSolve(ksp,b,x,ierr)<BR>> > >>>>>> call VecView(x,PETSC_VIEWER_STDOUT_WORLD,ierr)<BR>> > >>>>>> <BR>> > >>>>>> call VecGetArrayF90(x,xx_v,ierr)<BR>> > >>>>>> call VecRestoreArrayF90(x,xx_v,ierr)<BR>> > >>>>>> <BR>> > >>>>>> ...<BR>> > >>>>>> <BR>> > >>>>>> But, the error keeps coming out when call<BR>> > >> VecGetArrayF90(x,xx_v,ierr) and call VecRestoreArrayF90(x,xx_v,ierr)<BR>> > >> are not commented off.<BR>> > >>>>>> <BR>> > >>>>>> <BR>> > >>>>>> The error information shows:<BR>> > >>>>>> Caught signal number 11 SEGV: Segmentation Violation, probably<BR>> > >> memory access out of range<BR>> > >>>>>> <BR>> > >>>>>> [0]PETSC ERROR: --------------------- Stack Frames<BR>> > >> ------------------------------------<BR>> > >>>>>> [0]PETSC ERROR: Note: The EXACT line numbers in the stack are<BR>> > >> not available,<BR>> > >>>>>> [0]PETSC ERROR: INSTEAD the line number of the start of the<BR>> > >> function<BR>> > >>>>>> [0]PETSC ERROR: is given.<BR>> > >>>>>> [0]PETSC ERROR: [0] F90Array1dCreate line 52<BR>> > >> src/sys/f90-src/f90_cwrap.c<BR>> > >>>>>> [0]PETSC ERROR: --------------------- Error Message<BR>> > >> ------------------------------------<BR>> > >>>>>> <BR>> > >>>>>> I checked the code according the example, but cannot see any<BR>> > >> difference to that. Just don't know why the pointer array xx_v doesn't<BR>> > >> work here? Thanks.<BR>> > >>>>>> <BR>> > >>>>>> <BR>> > >>>>> <BR>> > >>>> <BR>> > >>> <BR>> > > <BR>> > > -- <BR>> > > -------------------------------------<BR>> > > Ethan Coon<BR>> > > Post-Doctoral Researcher<BR>> > > Mathematical Modeling and Analysis<BR>> > > Los Alamos National Laboratory<BR>> > > 505-665-8289<BR>> > > <BR>> > > http://www.ldeo.columbia.edu/~ecoon/<BR>> > > -------------------------------------<BR>> > > <BR>> > <BR>> <BR>> -- <BR>> -------------------------------------<BR>> Ethan Coon<BR>> Post-Doctoral Researcher<BR>> Mathematical Modeling and Analysis<BR>> Los Alamos National Laboratory<BR>> 505-665-8289<BR>> <BR>> http://www.ldeo.columbia.edu/~ecoon/<BR>> -------------------------------------<BR>> <BR>                                            </body>
</html>