Commits

Jed Brown committed 2942b97

Vec: restore support for PETSC_VIEWER_NATIVE

It is useful for debugging to be able to write a DMDA Vec in PETSc
ordering instead of in native ordering. This functionality was removed
in

commit 742cafdd48f845c927348e65bdee73999c739268
Author: Shri Abhyankar <abhyshr@mcs.anl.gov>
Date: Thu Jul 22 14:57:46 2010 -0500

Removed PETSC_VIEWER_NATIVE related code

and discussed on petsc-dev in

http://mid.mail-archive.com/8738qk1d3u.fsf@mcs.anl.gov

This commit partially reverts the above and adds a test.
PETSC_VIEWER_NATIVE is the documented way to write the Vec contents
without converting to natural ordering. This interface should be
preserved until there is a cleaner alternative.

Reported-by: Gautam Bisht <gbisht@lbl.gov>

  • Participants
  • Parent commits a0d4f57

Comments (0)

Files changed (7)

File include/petsc-private/vecimpl.h

   PetscErrorCode (*restorearrayread)(Vec,const PetscScalar**);
   PetscErrorCode (*stridesubsetgather)(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
   PetscErrorCode (*stridesubsetscatter)(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
+  PetscErrorCode (*viewnative)(Vec,PetscViewer);
+  PetscErrorCode (*loadnative)(Vec,PetscViewer);
 };
 
 /*

File src/dm/examples/tests/ex23.c

   PetscMPIInt      size;
   PetscInt         N = 6,m=PETSC_DECIDE,n=PETSC_DECIDE,p=PETSC_DECIDE,M=8,dof=1,stencil_width=1,P=5,pt = 0,st = 0;
   PetscErrorCode   ierr;
-  PetscBool        flg2,flg3;
+  PetscBool        flg2,flg3,native = PETSC_FALSE;
   DMBoundaryType   bx           = DM_BOUNDARY_NONE,by = DM_BOUNDARY_NONE,bz = DM_BOUNDARY_NONE;
   DMDAStencilType  stencil_type = DMDA_STENCIL_STAR;
   DM               da;
   ierr = PetscOptionsGetInt(NULL,"-dof",&dof,NULL);CHKERRQ(ierr);
   ierr = PetscOptionsGetInt(NULL,"-stencil_width",&stencil_width,NULL);CHKERRQ(ierr);
   ierr = PetscOptionsGetInt(NULL,"-periodic",&pt,NULL);CHKERRQ(ierr);
+  ierr = PetscOptionsGetBool(NULL,"-native",&native,NULL);CHKERRQ(ierr);
   if (pt == 1) bx = DM_BOUNDARY_PERIODIC;
   if (pt == 2) by = DM_BOUNDARY_PERIODIC;
   if (pt == 3) {bx = DM_BOUNDARY_PERIODIC; by = DM_BOUNDARY_PERIODIC;}
   ierr = DMCreateGlobalVector(da,&global4);CHKERRQ(ierr);
 
   ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
+  if (native) {ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE);CHKERRQ(ierr);}
   ierr = VecSetRandom(global1,rdm);CHKERRQ(ierr);
   ierr = VecView(global1,viewer);CHKERRQ(ierr);
   ierr = VecSetRandom(global3,rdm);CHKERRQ(ierr);
   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
 
   ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer);CHKERRQ(ierr);
+  if (native) {ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE);CHKERRQ(ierr);}
   ierr = VecLoad(global2,viewer);CHKERRQ(ierr);
   ierr = VecLoad(global4,viewer);CHKERRQ(ierr);
   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
 
+  if (native) {
+    Vec filenative;
+    PetscBool same;
+    ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer);CHKERRQ(ierr);
+    ierr = DMDACreateNaturalVector(da,&filenative);CHKERRQ(ierr);
+    /* DMDA "natural" Vec does not commandeer VecLoad.  The following load will only work when run on the same process
+     * layout, where as the standard VecView/VecLoad (using DMDA and not PETSC_VIEWER_NATIVE) can be read on a different
+     * number of processors. */
+    ierr = VecLoad(filenative,viewer);CHKERRQ(ierr);
+    ierr = VecEqual(global2,filenative,&same);CHKERRQ(ierr);
+    if (!same) {
+      ierr = PetscPrintf(PETSC_COMM_WORLD,"ex23: global vector does not match contents of file\n");CHKERRQ(ierr);
+      ierr = VecView(global2,0);CHKERRQ(ierr);
+      ierr = VecView(filenative,0);CHKERRQ(ierr);
+    }
+    ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
+    ierr = VecDestroy(&filenative);CHKERRQ(ierr);
+  }
+
   ierr = VecAXPY(global2,mone,global1);CHKERRQ(ierr);
   ierr = VecNorm(global2,NORM_MAX,&norm);CHKERRQ(ierr);
   if (norm != 0.0) {

File src/dm/examples/tests/makefile

               done; \
             done; \
           done;
+runex23_2:
+	-@${MPIEXEC} -n 2 ./ex23 -two -native > ex23_2.tmp 2>&1; \
+         ${DIFF} output/ex23_2.out ex23_2.tmp || printf "${PWD}\nPossible problem with ex23_2, diffs above\n=========================================\n"; \
+         ${RM} ex23_2.tmp
 
 runex24:
 	-@${MPIEXEC} -n 1 ./ex24  | grep -v -i Process > ex24_1.tmp 2>&1;	  \
                             ex34.PETSc runex34 ex34.rm ex36.PETSc runex36_1d runex36_2d runex36_2dp1 runex36_2dp2 runex36_3d runex36_3dp1 ex36.rm
 TESTEXAMPLES_C_X	  = ex2.PETSc runex2 ex2.rm ex3.PETSc runex3 ex3.rm ex5.PETSc runex5 ex5.rm ex6.PETSc runex6 \
                             ex6.rm ex7.PETSc ex7.rm  ex11.PETSc runex11 runex11_2 runex11_3 ex11.rm ex14.PETSc runex14 ex14.rm \
-                            ex13.PETSc runex13 ex13.rm ex23.PETSc runex23 ex23.rm ex37.PETSc runex37 ex37.rm
+                            ex13.PETSc runex13 ex13.rm ex23.PETSc runex23 runex23_2 ex23.rm ex37.PETSc runex37 ex37.rm
 TESTEXAMPLES_FORTRAN	  =
 TESTEXAMPLES_C_X_MPIUNI = ex1.PETSc ex1.rm ex2.PETSc ex2.rm ex3.PETSc ex3.rm ex4.PETSc ex4.rm ex5.PETSc ex5.rm\
                             ex6.PETSc ex6.rm ex12.PETSc ex12.rm

File src/dm/examples/tests/output/ex23_2.out

Empty file added.

File src/vec/vec/impls/mpi/pbvec.c

                                 0,
                                 0,
                                 VecStrideSubSetGather_Default,
-                                VecStrideSubSetScatter_Default
+                                VecStrideSubSetScatter_Default,
+                                0,
+                                0
 };
 
 #undef __FUNCT__

File src/vec/vec/impls/seq/bvec2.c

                                0,
                                0,
                                VecStrideSubSetGather_Default,
-                               VecStrideSubSetScatter_Default
+                               VecStrideSubSetScatter_Default,
+                               0,
+                               0
 };
 
 

File src/vec/vec/interface/vector.c

   if (vec->stash.n || vec->bstash.n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call VecAssemblyBegin/End() before viewing this vector");
 
   ierr = PetscLogEventBegin(VEC_View,vec,viewer,0,0);CHKERRQ(ierr);
+  ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
   if (iascii) {
     PetscInt rows,bs;
 
     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)vec,viewer);CHKERRQ(ierr);
-    ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
       ierr = VecGetSize(vec,&rows);CHKERRQ(ierr);
       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
     }
   }
-  ierr = (*vec->ops->view)(vec,viewer);CHKERRQ(ierr);
+  if (format == PETSC_VIEWER_NATIVE && vec->ops->viewnative) {
+    ierr = (*vec->ops->viewnative)(vec,viewer);CHKERRQ(ierr);
+  } else {
+    ierr = (*vec->ops->view)(vec,viewer);CHKERRQ(ierr);
+  }
   ierr = PetscLogEventEnd(VEC_View,vec,viewer,0,0);CHKERRQ(ierr);
   PetscFunctionReturn(0);
 }
 {
   PetscErrorCode ierr;
   PetscBool      isbinary,ishdf5;
+  PetscViewerFormat format;
 
   PetscFunctionBegin;
   PetscValidHeaderSpecific(newvec,VEC_CLASSID,1);
   if (!((PetscObject)newvec)->type_name && !newvec->ops->create) {
     ierr = VecSetType(newvec, VECSTANDARD);CHKERRQ(ierr);
   }
-  ierr = (*newvec->ops->load)(newvec,viewer);CHKERRQ(ierr);
+  ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
+  if (format == PETSC_VIEWER_NATIVE && newvec->ops->loadnative) {
+    ierr = (*newvec->ops->loadnative)(newvec,viewer);CHKERRQ(ierr);
+  } else {
+    ierr = (*newvec->ops->load)(newvec,viewer);CHKERRQ(ierr);
+  }
   ierr = PetscLogEventEnd(VEC_Load,viewer,0,0,0);CHKERRQ(ierr);
   PetscFunctionReturn(0);
 }
 
 .seealso: VecCreate(), MatShellSetOperation()
 @*/
-PetscErrorCode  VecSetOperation(Vec vec,VecOperation op, void (*f)(void))
+PetscErrorCode VecSetOperation(Vec vec,VecOperation op, void (*f)(void))
 {
   PetscFunctionBegin;
   PetscValidHeaderSpecific(vec,VEC_CLASSID,1);
+  if (op == VECOP_VIEW && !vec->ops->viewnative) {
+    vec->ops->viewnative = vec->ops->view;
+  } else if (op == VECOP_LOAD && !vec->ops->loadnative) {
+    vec->ops->loadnative = vec->ops->load;
+  }
   (((void(**)(void))vec->ops)[(int)op]) = f;
   PetscFunctionReturn(0);
 }