Commits

Lisandro Dalcin committed 43dd805

More work on binary I/O

Comments (0)

Files changed (5)

demo/InputOutput.c

   if (n2<3) p[2] = p[0]; if (n2<2) p[1] = p[0];
   if (n3<3) C[2] = C[0]; if (n3<2) C[1] = C[0];
   for (i=0; i<dim; i++)  if (C[i] ==-1) C[i] = p[i] - 1;
+
+  MPI_Comm comm = PETSC_COMM_WORLD;
   
   IGA iga;
-  ierr = IGACreate(PETSC_COMM_WORLD,&iga);CHKERRQ(ierr);
+  ierr = IGACreate(comm,&iga);CHKERRQ(ierr);
   ierr = IGASetDim(iga,dim);CHKERRQ(ierr);
   ierr = IGASetDof(iga,dof);CHKERRQ(ierr);
   for (i=0; i<dim; i++) {
   ierr = IGASetFromOptions(iga);CHKERRQ(ierr);
   ierr = IGASetUp(iga);CHKERRQ(ierr);
   
-  MPI_Comm comm;
-  ierr = IGAGetComm(iga,&comm);CHKERRQ(ierr);
   ierr = IGAWrite(iga,"iga.dat");CHKERRQ(ierr);
-  IGA iganew;
-  ierr = IGACreate(comm,&iganew);CHKERRQ(ierr);
-  ierr = IGARead(iganew,"iga.dat");CHKERRQ(ierr);
-  ierr = IGASetUp(iganew);CHKERRQ(ierr);
+  ierr = IGAWrite(iga,"iga.dat");CHKERRQ(ierr); /* just for testing */
 
-  ierr = IGADestroy(&iganew);CHKERRQ(ierr);
+  IGA iga1;
+  ierr = IGACreate(comm,&iga1);CHKERRQ(ierr);
+  ierr = IGARead(iga1,"iga.dat");CHKERRQ(ierr);
+  ierr = IGASetUp(iga1);CHKERRQ(ierr);
+  ierr = IGARead(iga1,"iga.dat");CHKERRQ(ierr);  /* just for testing */
+  ierr = IGASetUp(iga1);CHKERRQ(ierr);
+  ierr = IGADestroy(&iga1);CHKERRQ(ierr);
+
+  PetscViewer viewer;
+  ierr = PetscViewerBinaryOpen(comm,"iga.dat",FILE_MODE_WRITE,&viewer);
+  ierr = IGASave(iga,viewer);CHKERRQ(ierr);
+  ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
+
+  IGA iga2;
+  ierr = IGACreate(comm,&iga2);CHKERRQ(ierr);
+  ierr = PetscViewerBinaryOpen(comm,"iga.dat",FILE_MODE_READ,&viewer);
+  ierr = IGALoad(iga2,viewer);CHKERRQ(ierr);
+  ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
+  ierr = IGASetUp(iga2);CHKERRQ(ierr);
+  ierr = IGADestroy(&iga2);CHKERRQ(ierr);
+
   ierr = IGADestroy(&iga);CHKERRQ(ierr);
   ierr = PetscFinalize();CHKERRQ(ierr);
   return 0;
 runex0a_8:
 	-@${MPIEXEC} -n 8 ./InputOutput ${OPTS} -dim 3 -N 13,11,7 -p 3,2,1
 runex0a.rm:
-	-@${RM} -f iga.dat
+	-@${RM} -f iga.dat iga.dat.info
 
 runex1a_1:
 	-@${MPIEXEC} -n 1 ./L2Proj1D ${OPTS}
 };
 
 extern PetscClassId IGA_CLASSID;
+#define IGA_FILE_CLASSID 1211299
 
 extern PetscErrorCode IGAInitializePackage(const char path[]);
 
   if (!viewer) {ierr = PetscViewerASCIIGetStdout(((PetscObject)iga)->comm,&viewer);CHKERRQ(ierr);}
   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
   PetscCheckSameComm(iga,1,viewer,2);
-  if (!iga->setup) PetscFunctionReturn(0);
+  if (!iga->setup) PetscFunctionReturn(0); /* XXX */
+  ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
+  if (isbinary) {ierr = IGASave(iga,viewer);CHKERRQ(ierr);}
   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII, &isascii );CHKERRQ(ierr);
-  ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
   if (!isascii) PetscFunctionReturn(0);
   {
     MPI_Comm  comm;
   ierr = IGAElementSetUp(iga->iterator);CHKERRQ(ierr);
 
   { /* */
-    PetscBool flg;
-    char filename[PETSC_MAX_PATH_LEN] = "";
+    PetscBool flg1,flg2;
+    char filename1[PETSC_MAX_PATH_LEN] = "";
+    char filename2[PETSC_MAX_PATH_LEN] = "";
     PetscViewer viewer;
-    ierr = PetscOptionsGetString(((PetscObject)iga)->prefix,"-iga_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
-    if (flg && !PetscPreLoadingOn) {
-      ierr = PetscViewerASCIIOpen(((PetscObject)iga)->comm,filename,&viewer);CHKERRQ(ierr);
+    ierr = PetscObjectOptionsBegin((PetscObject)iga);CHKERRQ(ierr);
+    ierr = PetscOptionsString("-iga_view",       "Information on IGA context",   "IGAView",filename1,filename1,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
+    ierr = PetscOptionsString("-iga_view_binary","Save to file in binary format","IGAView",filename2,filename2,PETSC_MAX_PATH_LEN,&flg2);CHKERRQ(ierr);
+    ierr = PetscOptionsEnd();CHKERRQ(ierr);//MatView_Private
+    if (flg1 && !PetscPreLoadingOn) {
+      ierr = PetscViewerASCIIOpen(((PetscObject)iga)->comm,filename1,&viewer);CHKERRQ(ierr);
       ierr = IGAView(iga,viewer);CHKERRQ(ierr);
       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
     }
+    if (flg2 && !PetscPreLoadingOn) {
+      PetscViewer newviewer=0;
+      if (filename2[0]) {
+        ierr = PetscViewerBinaryOpen(((PetscObject)iga)->comm,filename2,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
+        newviewer = viewer;
+      } else {
+        viewer = PETSC_VIEWER_BINARY_(((PetscObject)iga)->comm);
+        PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
+      }
+      ierr = IGAView(iga,viewer);CHKERRQ(ierr);
+      ierr = PetscViewerDestroy(&newviewer);CHKERRQ(ierr);
+    }
   }
 
   PetscFunctionReturn(0);
 #include "petiga.h"
 
-#define IGA_FILE_CLASSID DM_FILE_CLASSID
+static PetscErrorCode VecLoad_Binary_SkipHeader(Vec,PetscViewer);
 
 #undef  __FUNCT__
 #define __FUNCT__ "IGALoad"
       PetscScalar min_w,max_w;
       ierr = IGACreateGeomDM(iga,&dm_geom);CHKERRQ(ierr);
       ierr = DMCreateGlobalVector(dm_geom,&vec_geom_global);CHKERRQ(ierr);
-      ierr = VecLoad(vec_geom_global,viewer);CHKERRQ(ierr);
+      if (!skipheader) {
+        ierr = VecLoad(vec_geom_global,viewer);CHKERRQ(ierr);
+      } else {
+        Vec vec_geom_natural;
+        ierr = DMDACreateNaturalVector(dm_geom,&vec_geom_natural);CHKERRQ(ierr);
+        ierr = VecLoad_Binary_SkipHeader(vec_geom_natural,viewer);CHKERRQ(ierr);
+        ierr = DMDANaturalToGlobalBegin(dm_geom,vec_geom_natural,INSERT_VALUES,vec_geom_global);CHKERRQ(ierr);
+        ierr = DMDANaturalToGlobalEnd  (dm_geom,vec_geom_natural,INSERT_VALUES,vec_geom_global);CHKERRQ(ierr);
+        ierr = VecDestroy(&vec_geom_natural);CHKERRQ(ierr);
+      }
       ierr = DMCreateLocalVector(dm_geom,&vec_geom_local);CHKERRQ(ierr);
       ierr = DMGlobalToLocalBegin(dm_geom,vec_geom_global,INSERT_VALUES,vec_geom_local);CHKERRQ(ierr);
       ierr = DMGlobalToLocalEnd  (dm_geom,vec_geom_global,INSERT_VALUES,vec_geom_local);CHKERRQ(ierr);
   ierr = PetscViewerCreate(comm,&viewer);CHKERRQ(ierr);
   ierr = PetscViewerSetType(viewer,PETSCVIEWERBINARY);CHKERRQ(ierr);
   ierr = PetscViewerBinarySkipInfo(viewer);CHKERRQ(ierr);
+  /*ierr = PetscViewerBinarySetSkipHeader(viewer,PETSC_TRUE);CHKERRQ(ierr);*/
   ierr = PetscViewerFileSetMode(viewer,FILE_MODE_READ);CHKERRQ(ierr);
   ierr = PetscViewerFileSetName(viewer,filename);CHKERRQ(ierr);
   ierr = IGALoad(iga,viewer);CHKERRQ(ierr);
   ierr = PetscViewerCreate(comm,&viewer);CHKERRQ(ierr);
   ierr = PetscViewerSetType(viewer,PETSCVIEWERBINARY);CHKERRQ(ierr);
   ierr = PetscViewerBinarySkipInfo(viewer);CHKERRQ(ierr);
+  /*ierr = PetscViewerBinarySetSkipHeader(viewer,PETSC_TRUE);CHKERRQ(ierr);*/
   ierr = PetscViewerFileSetMode(viewer,FILE_MODE_WRITE);CHKERRQ(ierr);
   ierr = PetscViewerFileSetName(viewer,filename);CHKERRQ(ierr);
   ierr = IGASave(iga,viewer);CHKERRQ(ierr);
   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
   PetscFunctionReturn(0);
 }
+
+
+#undef  __FUNCT__
+#define __FUNCT__ "VecLoad_Binary_SkipHeader"
+static PetscErrorCode VecLoad_Binary_SkipHeader(Vec vec, PetscViewer viewer)
+{
+  MPI_Comm       comm;
+  PetscMPIInt    i,rank,size,tag;
+  int            fd;
+  PetscInt       n;
+  PetscScalar    *array,*work;
+  PetscErrorCode ierr;
+  PetscFunctionBegin;
+
+  ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
+  ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
+  ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
+  ierr = PetscCommGetNewTag(comm,&tag);CHKERRQ(ierr);
+  ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
+
+  ierr = VecGetLocalSize(vec,&n);CHKERRQ(ierr);
+  ierr = VecGetArray(vec,&array);CHKERRQ(ierr);
+  if (!rank) {
+    ierr = PetscBinaryRead(fd,array,n,PETSC_SCALAR);CHKERRQ(ierr);
+    if (size > 1) {
+      const PetscInt *range = vec->map->range;
+      n = 1;
+      for (i=1; i<size; i++)
+        n = PetscMax(n,range[i+1] - range[i]);
+      ierr = PetscMalloc(n*sizeof(PetscScalar),&work);CHKERRQ(ierr);
+      for (i=1; i<size; i++) {
+        n   = range[i+1] - range[i];
+        ierr = PetscBinaryRead(fd,work,n,PETSC_SCALAR);CHKERRQ(ierr);
+        ierr = MPI_Send(work,n,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr);
+      }
+      ierr = PetscFree(work);CHKERRQ(ierr);
+    }
+  } else {
+    MPI_Status status;
+    ierr = MPI_Recv(array,n,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr);
+  }
+  ierr = VecRestoreArray(vec,&array);CHKERRQ(ierr);
+  PetscFunctionReturn(0);
+}