Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Songtao Chu
Yes, the output of VecView is still 0 on WSL.



From: Satish Balay 
Sent: Tuesday, April 10, 2018 2:25
To: Songtao Chu
Cc: petsc-users
Subject: Re: [petsc-users] How to access Vector data properly

Does my corrected code also print wrong info on WSL?

Satish

On Mon, 9 Apr 2018, Songtao Chu wrote:

> Thank you for your correction.
> I rebuilt the code under Linux, the bug disappeared. It seems this is a bug 
> of WSL.
>
> 
> From: Satish Balay 
> Sent: Tuesday, April 10, 2018 0:20
> To: Songtao Chu
> Cc: Matthew Knepley; petsc-users@mcs.anl.gov
> Subject: Re: [petsc-users] How to access Vector data properly
>
> On Mon, 9 Apr 2018, Songtao Chu wrote:
>
> > static char help[] = "\n\n";
> > #include 
> > #include 
> >
> >
> > int main(int argc,char **argv)
> > {
> > PetscMPIInt rank;
> > PetscErrorCode  ierr;
> > Vec global,local,natural;
> > DM  da;
> > PetscReal   *val;
> > PetscInti,x,xm;
> >
> >
> > ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
> > ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
> >
> > ierr = 
> > DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
> > ierr = DMSetUp(da);CHKERRQ(ierr);
> > ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
> >
> > ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> > ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> > ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> > rank);CHKERRQ(ierr);
>
> Shouldn't this call be removed? Nothing of substance is printed here.
>
> > for (i = x; i < x + xm; ++i) {
> > val[i] = i;
> > }
> > ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> >
> > VecView(global,PETSC_VIEWER_STDOUT_WORLD);
> >
> > ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> > ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> > ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> > rank);CHKERRQ(ierr);
>
> Shouldn't this be on PETSC_COMM_WORLD?
>
> > for (i = x; i < x + xm; ++i) {
> > ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
> > val[i]);CHKERRQ(ierr);
>
> Again PETSC_COMM_WORLD?
>
>
> > }
> > ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> > PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);
> >
> > ierr = PetscFinalize();
> > return ierr;
> > }
>
> Attaching the fixed code.
>
> Satish
> ---
> balay@asterix /home/balay/download-pine
> $ ./ex1
> Vec Object: 1 MPI processes
>   type: seq
> 0.
> 1.
> 2.
> 3.
> 4.
> Rank=0
>01234
> balay@asterix /home/balay/download-pine
> $ mpiexec -n 2 ./ex1
> Vec Object: 2 MPI processes
>   type: mpi
> Process [0]
> 0.
> 1.
> 2.
> Process [1]
> 3.
> 4.
> Rank=0
>012
> Rank=1
>34
> balay@asterix /home/balay/download-pine
> $ mpiexec -n 3 ./ex1
> Vec Object: 3 MPI processes
>   type: mpi
> Process [0]
> 0.
> 1.
> Process [1]
> 2.
> 3.
> Process [2]
> 4.
> Rank=0
>01
> Rank=1
>23
> Rank=2
>4
> balay@asterix /home/balay/download-pine
> $
>



Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Satish Balay
Does my corrected code also print wrong info on WSL?

Satish

On Mon, 9 Apr 2018, Songtao Chu wrote:

> Thank you for your correction.
> I rebuilt the code under Linux, the bug disappeared. It seems this is a bug 
> of WSL.
> 
> 
> From: Satish Balay 
> Sent: Tuesday, April 10, 2018 0:20
> To: Songtao Chu
> Cc: Matthew Knepley; petsc-users@mcs.anl.gov
> Subject: Re: [petsc-users] How to access Vector data properly
> 
> On Mon, 9 Apr 2018, Songtao Chu wrote:
> 
> > static char help[] = "\n\n";
> > #include 
> > #include 
> >
> >
> > int main(int argc,char **argv)
> > {
> > PetscMPIInt rank;
> > PetscErrorCode  ierr;
> > Vec global,local,natural;
> > DM  da;
> > PetscReal   *val;
> > PetscInti,x,xm;
> >
> >
> > ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
> > ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
> >
> > ierr = 
> > DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
> > ierr = DMSetUp(da);CHKERRQ(ierr);
> > ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
> >
> > ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> > ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> > ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> > rank);CHKERRQ(ierr);
> 
> Shouldn't this call be removed? Nothing of substance is printed here.
> 
> > for (i = x; i < x + xm; ++i) {
> > val[i] = i;
> > }
> > ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> >
> > VecView(global,PETSC_VIEWER_STDOUT_WORLD);
> >
> > ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> > ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> > ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> > rank);CHKERRQ(ierr);
> 
> Shouldn't this be on PETSC_COMM_WORLD?
> 
> > for (i = x; i < x + xm; ++i) {
> > ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
> > val[i]);CHKERRQ(ierr);
> 
> Again PETSC_COMM_WORLD?
> 
> 
> > }
> > ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> > PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);
> >
> > ierr = PetscFinalize();
> > return ierr;
> > }
> 
> Attaching the fixed code.
> 
> Satish
> ---
> balay@asterix /home/balay/download-pine
> $ ./ex1
> Vec Object: 1 MPI processes
>   type: seq
> 0.
> 1.
> 2.
> 3.
> 4.
> Rank=0
>01234
> balay@asterix /home/balay/download-pine
> $ mpiexec -n 2 ./ex1
> Vec Object: 2 MPI processes
>   type: mpi
> Process [0]
> 0.
> 1.
> 2.
> Process [1]
> 3.
> 4.
> Rank=0
>012
> Rank=1
>34
> balay@asterix /home/balay/download-pine
> $ mpiexec -n 3 ./ex1
> Vec Object: 3 MPI processes
>   type: mpi
> Process [0]
> 0.
> 1.
> Process [1]
> 2.
> 3.
> Process [2]
> 4.
> Rank=0
>01
> Rank=1
>23
> Rank=2
>4
> balay@asterix /home/balay/download-pine
> $
> 



Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Matthew Knepley
On Mon, Apr 9, 2018 at 1:15 PM, Songtao Chu  wrote:

> Thank you for your correction.
> I rebuilt the code under Linux, the bug disappeared. It seems this is a
> bug of WSL.
>

Thanks,

Matt


> --
> *From:* Satish Balay 
> *Sent:* Tuesday, April 10, 2018 0:20
> *To:* Songtao Chu
> *Cc:* Matthew Knepley; petsc-users@mcs.anl.gov
> *Subject:* Re: [petsc-users] How to access Vector data properly
>
> On Mon, 9 Apr 2018, Songtao Chu wrote:
>
> > static char help[] = "\n\n";
> > #include 
> > #include 
> >
> >
> > int main(int argc,char **argv)
> > {
> > PetscMPIInt rank;
> > PetscErrorCode  ierr;
> > Vec global,local,natural;
> > DM  da;
> > PetscReal   *val;
> > PetscInti,x,xm;
> >
> >
> > ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return
> ierr;
> > ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
> >
> > ierr = DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&
> da);CHKERRQ(ierr);
> > ierr = DMSetUp(da);CHKERRQ(ierr);
> > ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
> >
> > ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> > ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> > ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n",
> rank);CHKERRQ(ierr);
>
> Shouldn't this call be removed? Nothing of substance is printed here.
>
> > for (i = x; i < x + xm; ++i) {
> > val[i] = i;
> > }
> > ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> >
> > VecView(global,PETSC_VIEWER_STDOUT_WORLD);
> >
> > ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> > ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> > ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n",
> rank);CHKERRQ(ierr);
>
> Shouldn't this be on PETSC_COMM_WORLD?
>
> > for (i = x; i < x + xm; ++i) {
> > ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ",
> val[i]);CHKERRQ(ierr);
>
> Again PETSC_COMM_WORLD?
>
>
> > }
> > ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> > PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);
> >
> > ierr = PetscFinalize();
> > return ierr;
> > }
>
> Attaching the fixed code.
>
> Satish
> ---
> balay@asterix /home/balay/download-pine
> $ ./ex1
> Vec Object: 1 MPI processes
>   type: seq
> 0.
> 1.
> 2.
> 3.
> 4.
> Rank=0
>01234
> balay@asterix /home/balay/download-pine
> $ mpiexec -n 2 ./ex1
> Vec Object: 2 MPI processes
>   type: mpi
> Process [0]
> 0.
> 1.
> 2.
> Process [1]
> 3.
> 4.
> Rank=0
>012
> Rank=1
>34
> balay@asterix /home/balay/download-pine
> $ mpiexec -n 3 ./ex1
> Vec Object: 3 MPI processes
>   type: mpi
> Process [0]
> 0.
> 1.
> Process [1]
> 2.
> 3.
> Process [2]
> 4.
> Rank=0
>01
> Rank=1
>23
> Rank=2
>4
> balay@asterix /home/balay/download-pine
> $
>



-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener

https://www.cse.buffalo.edu/~knepley/ <http://www.caam.rice.edu/~mk51/>


Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Songtao Chu
Thank you for your correction.
I rebuilt the code under Linux, the bug disappeared. It seems this is a bug of 
WSL.


From: Satish Balay 
Sent: Tuesday, April 10, 2018 0:20
To: Songtao Chu
Cc: Matthew Knepley; petsc-users@mcs.anl.gov
Subject: Re: [petsc-users] How to access Vector data properly

On Mon, 9 Apr 2018, Songtao Chu wrote:

> static char help[] = "\n\n";
> #include 
> #include 
>
>
> int main(int argc,char **argv)
> {
> PetscMPIInt rank;
> PetscErrorCode  ierr;
> Vec global,local,natural;
> DM  da;
> PetscReal   *val;
> PetscInti,x,xm;
>
>
> ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
> ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
>
> ierr = 
> DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
> ierr = DMSetUp(da);CHKERRQ(ierr);
> ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);

Shouldn't this call be removed? Nothing of substance is printed here.

> for (i = x; i < x + xm; ++i) {
> val[i] = i;
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
>
> VecView(global,PETSC_VIEWER_STDOUT_WORLD);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);

Shouldn't this be on PETSC_COMM_WORLD?

> for (i = x; i < x + xm; ++i) {
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
> val[i]);CHKERRQ(ierr);

Again PETSC_COMM_WORLD?


> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);
>
> ierr = PetscFinalize();
> return ierr;
> }

Attaching the fixed code.

Satish
---
balay@asterix /home/balay/download-pine
$ ./ex1
Vec Object: 1 MPI processes
  type: seq
0.
1.
2.
3.
4.
Rank=0
   01234
balay@asterix /home/balay/download-pine
$ mpiexec -n 2 ./ex1
Vec Object: 2 MPI processes
  type: mpi
Process [0]
0.
1.
2.
Process [1]
3.
4.
Rank=0
   012
Rank=1
   34
balay@asterix /home/balay/download-pine
$ mpiexec -n 3 ./ex1
Vec Object: 3 MPI processes
  type: mpi
Process [0]
0.
1.
Process [1]
2.
3.
Process [2]
4.
Rank=0
   01
Rank=1
   23
Rank=2
   4
balay@asterix /home/balay/download-pine
$


Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Songtao Chu
Yes, but the bug remains the same.
Then I switched my development environment to Linux just now, this code ran 
without any bug. So it seems this is a bug of WSL?


From: Matthew Knepley 
Sent: Tuesday, April 10, 2018 0:42
To: Songtao Chu
Cc: Hong; petsc-users@mcs.anl.gov
Subject: Re: [petsc-users] How to access Vector data properly

Did you build EXACTLY the code I sent back?

  Thanks,

Matt

On Mon, Apr 9, 2018 at 12:13 PM, Songtao Chu 
mailto:st@outlook.com>> wrote:
The two output results are different in my test.
If I use the way of assignment variables in current code. I got error output of 
VecView as below:
Vec Object: 2 MPI processes
  type: mpi
Vec Object: Vec_0x7fffd6895a90_0 2 MPI processes
  type: mpi
Process [0]
0.
0.
0.
Process [1]
0.
0.

Or if I use the previous way (DMDANaturalToGlobal), I got error output of 
PetscSynchronizedPrintf:
Rank=0
   000
Rank=1
   00

Both are fine if I run without mpiexec.

Besides, my development environment is CLion + WSL + GCC 7.3.



From: Matthew Knepley mailto:knep...@gmail.com>>
Sent: Tuesday, April 10, 2018 0:01
To: Songtao Chu
Cc: Hong; petsc-users@mcs.anl.gov<mailto:petsc-users@mcs.anl.gov>
Subject: Re: [petsc-users] How to access Vector data properly

This works fine for me:


knepley/feature-pc-patch $:/PETSc3/petsc/petsc-pylith/tmp$ 
../arch-master-debug/bin/mpiexec -n 2 ./tester

Vec Object: 2 MPI processes

  type: mpi

Process [0]

0.

1.

2.

Process [1]

3.

4.

Rank=0

   012

Rank=1

   34

What is the problem?

   Matt


On Mon, Apr 9, 2018 at 11:51 AM, Songtao Chu 
mailto:st@outlook.com>> wrote:

static char help[] = "\n\n";
#include 
#include 


int main(int argc,char **argv)
{
PetscMPIInt rank;
PetscErrorCode  ierr;
Vec global,local,natural;
DM  da;
PetscReal   *val;
PetscInti,x,xm;


ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);

ierr = 
DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
ierr = DMSetUp(da);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
val[i] = i;
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);

VecView(global,PETSC_VIEWER_STDOUT_WORLD);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
val[i]);CHKERRQ(ierr);
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);

ierr = PetscFinalize();
return ierr;
}



From: Matthew Knepley mailto:knep...@gmail.com>>
Sent: Monday, April 9, 2018 23:29
To: Chu Songtao
Cc: Hong; petsc-users@mcs.anl.gov<mailto:petsc-users@mcs.anl.gov>
Subject: Re: [petsc-users] How to access Vector data properly

On Mon, Apr 9, 2018 at 11:19 AM, Chu Songtao 
mailto:st@outlook.com>> wrote:
Hong,
I added the function to the end, but the results didn't change.
Then I tested another thing, assigning global by Get(Restore)Array instead of 
DMDANaturalToGlobal. And the results switched. VecView outputs 0 and 
PetscSynchronizedPrintf outputs correct values. Strange.

Send the whole code. You have a logic error somewhere.

   Matt


ierr = 
DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
ierr = DMSetUp(da);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
val[i] = i;
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);

VecView(global,PETSC_VIEWER_STDOUT_WORLD);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 

Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Matthew Knepley
Did you build EXACTLY the code I sent back?

  Thanks,

Matt

On Mon, Apr 9, 2018 at 12:13 PM, Songtao Chu  wrote:

> The two output results are different in my test.
> If I use the way of assignment variables in current code. I got error
> output of VecView as below:
> Vec Object: 2 MPI processes
>   type: mpi
> Vec Object: Vec_0x7fffd6895a90_0 2 MPI processes
>   type: mpi
> Process [0]
> 0.
> 0.
> 0.
> Process [1]
> 0.
> 0.
>
> Or if I use the previous way (DMDANaturalToGlobal), I got error output
> of PetscSynchronizedPrintf:
> Rank=0
>000
> Rank=1
>00
>
> Both are fine if I run without mpiexec.
>
> Besides, my development environment is CLion + WSL + GCC 7.3.
>
>
> --
> *From:* Matthew Knepley 
> *Sent:* Tuesday, April 10, 2018 0:01
> *To:* Songtao Chu
> *Cc:* Hong; petsc-users@mcs.anl.gov
> *Subject:* Re: [petsc-users] How to access Vector data properly
>
> This works fine for me:
>
> knepley/feature-pc-patch $:/PETSc3/petsc/petsc-pylith/tmp$
> ../arch-master-debug/bin/mpiexec -n 2 ./tester
>
> Vec Object: 2 MPI processes
>
>   type: mpi
>
> Process [0]
>
> 0.
>
> 1.
>
> 2.
>
> Process [1]
>
> 3.
>
> 4.
>
> Rank=0
>
>012
>
> Rank=1
>
>34
>
> What is the problem?
>
>Matt
>
>
> On Mon, Apr 9, 2018 at 11:51 AM, Songtao Chu  wrote:
>
> static char help[] = "\n\n";
> #include 
> #include 
>
>
> int main(int argc,char **argv)
> {
> PetscMPIInt rank;
> PetscErrorCode  ierr;
> Vec global,local,natural;
> DM  da;
> PetscReal   *val;
> PetscInti,x,xm;
>
>
> ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
> ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
>
> ierr = 
> DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
> ierr = DMSetUp(da);CHKERRQ(ierr);
> ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);
> for (i = x; i < x + xm; ++i) {
> val[i] = i;
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
>
> VecView(global,PETSC_VIEWER_STDOUT_WORLD);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);
> for (i = x; i < x + xm; ++i) {
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
> val[i]);CHKERRQ(ierr);
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);
>
> ierr = PetscFinalize();
> return ierr;
> }
>
>
>
> --
> *From:* Matthew Knepley 
> *Sent:* Monday, April 9, 2018 23:29
> *To:* Chu Songtao
> *Cc:* Hong; petsc-users@mcs.anl.gov
> *Subject:* Re: [petsc-users] How to access Vector data properly
>
> On Mon, Apr 9, 2018 at 11:19 AM, Chu Songtao  wrote:
>
> Hong,
> I added the function to the end, but the results didn't change.
> Then I tested another thing, assigning global by Get(Restore)Array instead
> of DMDANaturalToGlobal. And the results switched. VecView outputs 0
> and PetscSynchronizedPrintf outputs correct values. Strange.
>
>
> Send the whole code. You have a logic error somewhere.
>
>Matt
>
>
> ierr = 
> DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
> ierr = DMSetUp(da);CHKERRQ(ierr);
> ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);
> for (i = x; i < x + xm; ++i) {
> val[i] = i;
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
>
> VecView(global,PETSC_VIEWER_STDOUT_WORLD);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PE

Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Satish Balay
On Mon, 9 Apr 2018, Songtao Chu wrote:

> static char help[] = "\n\n";
> #include 
> #include 
> 
> 
> int main(int argc,char **argv)
> {
> PetscMPIInt rank;
> PetscErrorCode  ierr;
> Vec global,local,natural;
> DM  da;
> PetscReal   *val;
> PetscInti,x,xm;
> 
> 
> ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
> ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
> 
> ierr = 
> DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
> ierr = DMSetUp(da);CHKERRQ(ierr);
> ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
> 
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);

Shouldn't this call be removed? Nothing of substance is printed here.

> for (i = x; i < x + xm; ++i) {
> val[i] = i;
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> 
> VecView(global,PETSC_VIEWER_STDOUT_WORLD);
> 
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);

Shouldn't this be on PETSC_COMM_WORLD?

> for (i = x; i < x + xm; ++i) {
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
> val[i]);CHKERRQ(ierr);

Again PETSC_COMM_WORLD?


> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);
> 
> ierr = PetscFinalize();
> return ierr;
> }

Attaching the fixed code.

Satish
---
balay@asterix /home/balay/download-pine
$ ./ex1
Vec Object: 1 MPI processes
  type: seq
0.
1.
2.
3.
4.
Rank=0
   01234 
balay@asterix /home/balay/download-pine
$ mpiexec -n 2 ./ex1
Vec Object: 2 MPI processes
  type: mpi
Process [0]
0.
1.
2.
Process [1]
3.
4.
Rank=0
   012 
Rank=1
   34 
balay@asterix /home/balay/download-pine
$ mpiexec -n 3 ./ex1
Vec Object: 3 MPI processes
  type: mpi
Process [0]
0.
1.
Process [1]
2.
3.
Process [2]
4.
Rank=0
   01 
Rank=1
   23 
Rank=2
   4 
balay@asterix /home/balay/download-pine
$ static char help[] = "\n\n";
#include 
#include 


int main(int argc,char **argv)
{
PetscMPIInt rank;
PetscErrorCode  ierr;
Vec global;
DM  da;
PetscReal   *val;
PetscInti,x,xm;


ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);

ierr = 
DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
ierr = DMSetUp(da);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
val[i] = i;
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);

VecView(global,PETSC_VIEWER_STDOUT_WORLD);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%4.f ", 
val[i]);CHKERRQ(ierr);
}
ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD, "\n", rank);CHKERRQ(ierr);
PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT);
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);

ierr = VecDestroy(&global);CHKERRQ(ierr);
ierr = DMDestroy(&da);CHKERRQ(ierr);
ierr = PetscFinalize();
return ierr;
}


Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Songtao Chu
The two output results are different in my test.
If I use the way of assignment variables in current code. I got error output of 
VecView as below:
Vec Object: 2 MPI processes
  type: mpi
Vec Object: Vec_0x7fffd6895a90_0 2 MPI processes
  type: mpi
Process [0]
0.
0.
0.
Process [1]
0.
0.

Or if I use the previous way (DMDANaturalToGlobal), I got error output of 
PetscSynchronizedPrintf:
Rank=0
   000
Rank=1
   00

Both are fine if I run without mpiexec.

Besides, my development environment is CLion + WSL + GCC 7.3.



From: Matthew Knepley 
Sent: Tuesday, April 10, 2018 0:01
To: Songtao Chu
Cc: Hong; petsc-users@mcs.anl.gov
Subject: Re: [petsc-users] How to access Vector data properly

This works fine for me:


knepley/feature-pc-patch $:/PETSc3/petsc/petsc-pylith/tmp$ 
../arch-master-debug/bin/mpiexec -n 2 ./tester

Vec Object: 2 MPI processes

  type: mpi

Process [0]

0.

1.

2.

Process [1]

3.

4.

Rank=0

   012

Rank=1

   34

What is the problem?

   Matt


On Mon, Apr 9, 2018 at 11:51 AM, Songtao Chu 
mailto:st@outlook.com>> wrote:

static char help[] = "\n\n";
#include 
#include 


int main(int argc,char **argv)
{
PetscMPIInt rank;
PetscErrorCode  ierr;
Vec global,local,natural;
DM  da;
PetscReal   *val;
PetscInti,x,xm;


ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);

ierr = 
DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
ierr = DMSetUp(da);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
val[i] = i;
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);

VecView(global,PETSC_VIEWER_STDOUT_WORLD);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
val[i]);CHKERRQ(ierr);
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);

ierr = PetscFinalize();
return ierr;
}



From: Matthew Knepley mailto:knep...@gmail.com>>
Sent: Monday, April 9, 2018 23:29
To: Chu Songtao
Cc: Hong; petsc-users@mcs.anl.gov<mailto:petsc-users@mcs.anl.gov>
Subject: Re: [petsc-users] How to access Vector data properly

On Mon, Apr 9, 2018 at 11:19 AM, Chu Songtao 
mailto:st@outlook.com>> wrote:
Hong,
I added the function to the end, but the results didn't change.
Then I tested another thing, assigning global by Get(Restore)Array instead of 
DMDANaturalToGlobal. And the results switched. VecView outputs 0 and 
PetscSynchronizedPrintf outputs correct values. Strange.

Send the whole code. You have a logic error somewhere.

   Matt


ierr = 
DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
ierr = DMSetUp(da);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
val[i] = i;
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);

VecView(global,PETSC_VIEWER_STDOUT_WORLD);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
val[i]);CHKERRQ(ierr);
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);

$ mpiexec -n 2 ./test0
Rank=0
Rank=1
Vec Object: 2 MPI processes
  type: mpi
Vec Object: Vec_0x7fffd6629a90_0 2 MPI processes
  type: mpi
Rank=1
   34 Process [0]
0.
0.
0.
Process [1]
0.
0.
Rank=0
   012




Songtao :
You may need adding PetscSynchronizedFlush() to the end. See 
petsc/src/dm/examples/tests/ex43.c
Hong
Hello,
I am just a beginner. Now I get confused on how to correctl

Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Matthew Knepley
This works fine for me:

knepley/feature-pc-patch $:/PETSc3/petsc/petsc-pylith/tmp$
../arch-master-debug/bin/mpiexec -n 2 ./tester

Vec Object: 2 MPI processes

  type: mpi

Process [0]

0.

1.

2.

Process [1]

3.

4.

Rank=0

   012

Rank=1

   34

What is the problem?

   Matt


On Mon, Apr 9, 2018 at 11:51 AM, Songtao Chu  wrote:

> static char help[] = "\n\n";
> #include 
> #include 
>
>
> int main(int argc,char **argv)
> {
> PetscMPIInt rank;
> PetscErrorCode  ierr;
> Vec global,local,natural;
> DM  da;
> PetscReal   *val;
> PetscInti,x,xm;
>
>
> ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
> ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
>
> ierr = 
> DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
> ierr = DMSetUp(da);CHKERRQ(ierr);
> ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);
> for (i = x; i < x + xm; ++i) {
> val[i] = i;
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
>
> VecView(global,PETSC_VIEWER_STDOUT_WORLD);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);
> for (i = x; i < x + xm; ++i) {
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
> val[i]);CHKERRQ(ierr);
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);
>
>     ierr = PetscFinalize();
>     return ierr;
> }
>
>
>
> --
> *From:* Matthew Knepley 
> *Sent:* Monday, April 9, 2018 23:29
> *To:* Chu Songtao
> *Cc:* Hong; petsc-users@mcs.anl.gov
> *Subject:* Re: [petsc-users] How to access Vector data properly
>
> On Mon, Apr 9, 2018 at 11:19 AM, Chu Songtao  wrote:
>
> Hong,
> I added the function to the end, but the results didn't change.
> Then I tested another thing, assigning global by Get(Restore)Array instead
> of DMDANaturalToGlobal. And the results switched. VecView outputs 0
> and PetscSynchronizedPrintf outputs correct values. Strange.
>
>
> Send the whole code. You have a logic error somewhere.
>
>Matt
>
>
> ierr = 
> DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
> ierr = DMSetUp(da);CHKERRQ(ierr);
> ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);
> for (i = x; i < x + xm; ++i) {
> val[i] = i;
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
>
> VecView(global,PETSC_VIEWER_STDOUT_WORLD);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);
> for (i = x; i < x + xm; ++i) {
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
> val[i]);CHKERRQ(ierr);
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);
>
> $ mpiexec -n 2 ./test0
> Rank=0
> Rank=1
> Vec Object: 2 MPI processes
>   type: mpi
> Vec Object: Vec_0x7fffd6629a90_0 2 MPI processes
>   type: mpi
> Rank=1
>34 Process [0]
> 0.
> 0.
> 0.
> Process [1]
> 0.
> 0.
> Rank=0
>012
>
>
> --
>
> Songtao :
> You may need adding PetscSynchronizedFlush() to the end.
> See petsc/src/dm/examples/tests/ex43.c
> Hong
>
> Hello,
> I am just a beginner. Now I get confused on how to correctly use Vec
> and DMDA.
> I set 1D DMDA global vector with natural values for a test. Then use
> VecView and DMDAVecGetArray to view the data, but the results are
> different. I don't know why.
>
> this is the code:
>
> PetscMP

Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Songtao Chu
static char help[] = "\n\n";
#include 
#include 


int main(int argc,char **argv)
{
PetscMPIInt rank;
PetscErrorCode  ierr;
Vec global,local,natural;
DM  da;
PetscReal   *val;
PetscInti,x,xm;


ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);

ierr = 
DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
ierr = DMSetUp(da);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
val[i] = i;
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);

VecView(global,PETSC_VIEWER_STDOUT_WORLD);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
val[i]);CHKERRQ(ierr);
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);

ierr = PetscFinalize();
return ierr;
}



From: Matthew Knepley 
Sent: Monday, April 9, 2018 23:29
To: Chu Songtao
Cc: Hong; petsc-users@mcs.anl.gov
Subject: Re: [petsc-users] How to access Vector data properly

On Mon, Apr 9, 2018 at 11:19 AM, Chu Songtao 
mailto:st@outlook.com>> wrote:
Hong,
I added the function to the end, but the results didn't change.
Then I tested another thing, assigning global by Get(Restore)Array instead of 
DMDANaturalToGlobal. And the results switched. VecView outputs 0 and 
PetscSynchronizedPrintf outputs correct values. Strange.

Send the whole code. You have a logic error somewhere.

   Matt


ierr = 
DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
ierr = DMSetUp(da);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
val[i] = i;
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);

VecView(global,PETSC_VIEWER_STDOUT_WORLD);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
val[i]);CHKERRQ(ierr);
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);

$ mpiexec -n 2 ./test0
Rank=0
Rank=1
Vec Object: 2 MPI processes
  type: mpi
Vec Object: Vec_0x7fffd6629a90_0 2 MPI processes
  type: mpi
Rank=1
   34 Process [0]
0.
0.
0.
Process [1]
0.
0.
Rank=0
   012




Songtao :
You may need adding PetscSynchronizedFlush() to the end. See 
petsc/src/dm/examples/tests/ex43.c
Hong
Hello,
I am just a beginner. Now I get confused on how to correctly use Vec and 
DMDA.
I set 1D DMDA global vector with natural values for a test. Then use 
VecView and DMDAVecGetArray to view the data, but the results are different. I 
don't know why.

this is the code:

PetscMPIInt rank;
PetscErrorCode  ierr;
Vec global,local,natural;
DM  da;
PetscReal   *xnatural,*val;
PetscInti,start,end,x,xm;


ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);

ierr = 
DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
ierr = DMSetUp(da);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);

ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr);
ierr = VecGetOwnershipRange(natural,&start,&end);CHKERRQ(ierr);
ierr = VecGetArray(natural,&xnatural);CHKERRQ(ierr);
for (i=start; ihttps://www.cse.buffalo.edu/~knepley/<http://www.caam.rice.edu/~mk51/>


Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Matthew Knepley
On Mon, Apr 9, 2018 at 11:19 AM, Chu Songtao  wrote:

> Hong,
> I added the function to the end, but the results didn't change.
> Then I tested another thing, assigning global by Get(Restore)Array instead
> of DMDANaturalToGlobal. And the results switched. VecView outputs 0
> and PetscSynchronizedPrintf outputs correct values. Strange.
>

Send the whole code. You have a logic error somewhere.

   Matt


> ierr = 
> DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
> ierr = DMSetUp(da);CHKERRQ(ierr);
> ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);
> for (i = x; i < x + xm; ++i) {
> val[i] = i;
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
>
> VecView(global,PETSC_VIEWER_STDOUT_WORLD);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);
> for (i = x; i < x + xm; ++i) {
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
> val[i]);CHKERRQ(ierr);
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
> PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);
>
> $ mpiexec -n 2 ./test0
> Rank=0
> Rank=1
> Vec Object: 2 MPI processes
>   type: mpi
> Vec Object: Vec_0x7fffd6629a90_0 2 MPI processes
>   type: mpi
> Rank=1
>34 Process [0]
> 0.
> 0.
> 0.
> Process [1]
> 0.
> 0.
> Rank=0
>012
>
>
> --
>
> Songtao :
> You may need adding PetscSynchronizedFlush() to the end.
> See petsc/src/dm/examples/tests/ex43.c
> Hong
>
> Hello,
> I am just a beginner. Now I get confused on how to correctly use Vec
> and DMDA.
> I set 1D DMDA global vector with natural values for a test. Then use
> VecView and DMDAVecGetArray to view the data, but the results are
> different. I don't know why.
>
> this is the code:
>
> PetscMPIInt rank;
> PetscErrorCode  ierr;
> Vec global,local,natural;
> DM  da;
> PetscReal   *xnatural,*val;
> PetscInti,start,end,x,xm;
>
>
> ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
> ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
>
> ierr = 
> DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
> ierr = DMSetUp(da);CHKERRQ(ierr);
> ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
>
> ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr);
> ierr = VecGetOwnershipRange(natural,&start,&end);CHKERRQ(ierr);
> ierr = VecGetArray(natural,&xnatural);CHKERRQ(ierr);
> for (i=start; i xnatural[i-start] = i;
> }
> ierr = VecRestoreArray(natural,&xnatural);CHKERRQ(ierr);
> ierr = 
> DMDANaturalToGlobalBegin(da,natural,INSERT_VALUES,global);CHKERRQ(ierr);
> ierr = DMDANaturalToGlobalEnd(da,natural,INSERT_VALUES,global);CHKERRQ(ierr);
> ierr = VecDestroy(&natural);CHKERRQ(ierr);
>
> VecView(global,PETSC_VIEWER_STDOUT_WORLD);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);
> for (i = x; i < x + xm; ++i) {
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
> val[i]);CHKERRQ(ierr);
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
>
> and the results are:
>
> $ ./test0
> Vec Object: 1 MPI processes
>   type: seq
> Vec Object: Vec_0x73cbc500_0 1 MPI processes
>   type: mpi
> Process [0]
> 0.
> 1.
> 2.
> 3.
> 4.
> Rank=0
>01234
>
> $ mpiexec -n 2 ./test0
> Vec Object: 2 MPI processes
>   type: mpi
> Vec Object: Vec_0x7fffcf948a90_0 2 MPI processes
>   type: mpi
> Process [0]
> 0.
> 1.
> 2.
> Process [1]
> 3.
> 4.
> Rank=0
>000 Rank=1
>00
>
>
> the seq version is correct, but the mpi version is wrong. Every element in
> val is 0.
>
>
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener

https://www.cse.buffalo.edu/~knepley/ 


Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Chu Songtao
Hong,
I added the function to the end, but the results didn't change.
Then I tested another thing, assigning global by Get(Restore)Array instead of 
DMDANaturalToGlobal. And the results switched. VecView outputs 0 and 
PetscSynchronizedPrintf outputs correct values. Strange.


ierr = 
DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
ierr = DMSetUp(da);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
val[i] = i;
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);

VecView(global,PETSC_VIEWER_STDOUT_WORLD);

ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
rank);CHKERRQ(ierr);
for (i = x; i < x + xm; ++i) {
ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
val[i]);CHKERRQ(ierr);
}
ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
PetscSynchronizedFlush(PETSC_COMM_SELF, PETSC_STDOUT);

$ mpiexec -n 2 ./test0
Rank=0
Rank=1
Vec Object: 2 MPI processes
  type: mpi
Vec Object: Vec_0x7fffd6629a90_0 2 MPI processes
  type: mpi
Rank=1
   34 Process [0]
0.
0.
0.
Process [1]
0.
0.
Rank=0
   012




Songtao :
You may need adding PetscSynchronizedFlush() to the end. See 
petsc/src/dm/examples/tests/ex43.c
Hong
Hello,
I am just a beginner. Now I get confused on how to correctly use Vec and 
DMDA.
I set 1D DMDA global vector with natural values for a test. Then use 
VecView and DMDAVecGetArray to view the data, but the results are different. I 
don't know why.

this is the code:

PetscMPIInt rank;
PetscErrorCode  ierr;
Vec global,local,natural;
DM  da;
PetscReal   *xnatural,*val;
PetscInti,start,end,x,xm;


ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);

ierr = 
DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
ierr = DMSetUp(da);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);

ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr);
ierr = VecGetOwnershipRange(natural,&start,&end);CHKERRQ(ierr);
ierr = VecGetArray(natural,&xnatural);CHKERRQ(ierr);
for (i=start; i

Re: [petsc-users] How to access Vector data properly

2018-04-09 Thread Hong
Songtao :
You may need adding PetscSynchronizedFlush() to the end.
See petsc/src/dm/examples/tests/ex43.c
Hong

> Hello,
> I am just a beginner. Now I get confused on how to correctly use Vec
> and DMDA.
> I set 1D DMDA global vector with natural values for a test. Then use
> VecView and DMDAVecGetArray to view the data, but the results are
> different. I don't know why.
>
> this is the code:
>
> PetscMPIInt rank;
> PetscErrorCode  ierr;
> Vec global,local,natural;
> DM  da;
> PetscReal   *xnatural,*val;
> PetscInti,start,end,x,xm;
>
>
> ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
> ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
>
> ierr = 
> DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
> ierr = DMSetUp(da);CHKERRQ(ierr);
> ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
>
> ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr);
> ierr = VecGetOwnershipRange(natural,&start,&end);CHKERRQ(ierr);
> ierr = VecGetArray(natural,&xnatural);CHKERRQ(ierr);
> for (i=start; i xnatural[i-start] = i;
> }
> ierr = VecRestoreArray(natural,&xnatural);CHKERRQ(ierr);
> ierr = 
> DMDANaturalToGlobalBegin(da,natural,INSERT_VALUES,global);CHKERRQ(ierr);
> ierr = DMDANaturalToGlobalEnd(da,natural,INSERT_VALUES,global);CHKERRQ(ierr);
> ierr = VecDestroy(&natural);CHKERRQ(ierr);
>
> VecView(global,PETSC_VIEWER_STDOUT_WORLD);
>
> ierr = DMDAGetCorners(da,&x,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
> ierr = DMDAVecGetArray(da, global, &val);CHKERRQ(ierr);
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "Rank=%d\n", 
> rank);CHKERRQ(ierr);
> for (i = x; i < x + xm; ++i) {
> ierr = PetscSynchronizedPrintf(PETSC_COMM_SELF, "%4.f ", 
> val[i]);CHKERRQ(ierr);
> }
> ierr = DMDAVecRestoreArray(da, global, &val);CHKERRQ(ierr);
>
> and the results are:
>
> $ ./test0
> Vec Object: 1 MPI processes
>   type: seq
> Vec Object: Vec_0x73cbc500_0 1 MPI processes
>   type: mpi
> Process [0]
> 0.
> 1.
> 2.
> 3.
> 4.
> Rank=0
>01234
>
> $ mpiexec -n 2 ./test0
> Vec Object: 2 MPI processes
>   type: mpi
> Vec Object: Vec_0x7fffcf948a90_0 2 MPI processes
>   type: mpi
> Process [0]
> 0.
> 1.
> 2.
> Process [1]
> 3.
> 4.
> Rank=0
>000 Rank=1
>00
>
>
> the seq version is correct, but the mpi version is wrong. Every element in
> val is 0.
>
>


[petsc-users] How to access Vector data properly

2018-04-09 Thread Chu Songtao
Hello,
I am just a beginner. Now I get confused on how to correctly use Vec and 
DMDA.
I set 1D DMDA global vector with natural values for a test. Then use 
VecView and DMDAVecGetArray to view the data, but the results are different. I 
don't know why.

this is the code:

PetscMPIInt rank;
PetscErrorCode  ierr;
Vec global,local,natural;
DM  da;
PetscReal   *xnatural,*val;
PetscInti,start,end,x,xm;


ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);

ierr = 
DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,5,1,1,NULL,&da);CHKERRQ(ierr);
ierr = DMSetUp(da);CHKERRQ(ierr);
ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);

ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr);
ierr = VecGetOwnershipRange(natural,&start,&end);CHKERRQ(ierr);
ierr = VecGetArray(natural,&xnatural);CHKERRQ(ierr);
for (i=start; i