PetscErrorCode PetscViewerDestroy_Binary(PetscViewer v) { PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data; PetscErrorCode ierr; PetscFunctionBegin; if (v->format == PETSC_VIEWER_BINARY_MATLAB) { MPI_Comm comm; FILE *info; ierr = PetscObjectGetComm((PetscObject)v,&comm);CHKERRQ(ierr); ierr = PetscViewerBinaryGetInfoPointer(v,&info);CHKERRQ(ierr); ierr = PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr); ierr = PetscFPrintf(comm,info,"#$$ close(fd);\n");CHKERRQ(ierr); ierr = PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr); } ierr = PetscViewerFileClose_Binary(v);CHKERRQ(ierr); ierr = PetscFree(vbinary);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C PetscBagView - Views a bag of values as either ASCII text or a binary file Collective on PetscBag Input Parameter: + bag - the bag of values - viewer - location to view the values Level: beginner Warning: Currently PETSc bags saved in a binary file can only be read back in on a machine of the same architecture. Let us know when this is a problem and we'll fix it. .seealso: PetscBag, PetscBagSetName(), PetscBagDestroy(), PetscBagLoad(), PetscBagGetData() PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar(), PetscBagRegisterEnum() PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName() @*/ PetscErrorCode PetscBagView(PetscBag bag,PetscViewer view) { PetscBool isascii,isbinary; PetscErrorCode ierr; PetscBagItem nitem = bag->bagitems; PetscFunctionBegin; ierr = PetscObjectTypeCompare((PetscObject)view,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)view,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); if (isascii) { if (bag->bagprefix) { ierr = PetscViewerASCIIPrintf(view,"PetscBag Object: %s (%s) %s\n",bag->bagname,bag->bagprefix,bag->baghelp);CHKERRQ(ierr); } else { ierr = PetscViewerASCIIPrintf(view,"PetscBag Object: %s %s\n",bag->bagname,bag->baghelp);CHKERRQ(ierr); } while (nitem) { if (nitem->dtype == PETSC_CHAR) { char *value = (char*)(((char*)bag) + nitem->offset); char tmp = value[nitem->msize-1]; /* special handling for fortran chars wihout null terminator */ value[nitem->msize-1] =0; ierr = PetscViewerASCIIPrintf(view," %s = %s; %s\n",nitem->name,value,nitem->help);CHKERRQ(ierr); value[nitem->msize-1] = tmp; } else if (nitem->dtype == PETSC_REAL) { PetscReal *value = (PetscReal*)(((char*)bag) + nitem->offset); PetscInt i; ierr = PetscViewerASCIIPrintf(view," %s = ",nitem->name);CHKERRQ(ierr); for (i=0; i<nitem->msize; i++) { ierr = PetscViewerASCIIPrintf(view,"%g ",(double)value[i]);CHKERRQ(ierr); } ierr = PetscViewerASCIIPrintf(view,"; %s\n",nitem->help);CHKERRQ(ierr); } else if (nitem->dtype == PETSC_SCALAR) { PetscScalar value = *(PetscScalar*)(((char*)bag) + nitem->offset); #if defined(PETSC_USE_COMPLEX) ierr = PetscViewerASCIIPrintf(view," %s = %g + %gi; %s\n",nitem->name,(double)PetscRealPart(value),(double)PetscImaginaryPart(value),nitem->help);CHKERRQ(ierr); #else ierr = PetscViewerASCIIPrintf(view," %s = %g; %s\n",nitem->name,(double)value,nitem->help);CHKERRQ(ierr); #endif } else if (nitem->dtype == PETSC_INT) { PetscInt i,*value = (PetscInt*)(((char*)bag) + nitem->offset); ierr = PetscViewerASCIIPrintf(view," %s = ",nitem->name);CHKERRQ(ierr); for (i=0; i<nitem->msize; i++) { ierr = PetscViewerASCIIPrintf(view,"%D ",value[i]);CHKERRQ(ierr); } ierr = PetscViewerASCIIPrintf(view,"; %s\n",nitem->help);CHKERRQ(ierr); } else if (nitem->dtype == PETSC_BOOL) { PetscBool *value = (PetscBool*)(((char*)bag) + nitem->offset); PetscInt i; /* some Fortran compilers use -1 as boolean */ ierr = PetscViewerASCIIPrintf(view," %s = ",nitem->name);CHKERRQ(ierr); for (i=0; i<nitem->msize; i++) { if (((int) value[i]) == -1) value[i] = PETSC_TRUE; /* the checks here with != PETSC_FALSE and PETSC_TRUE is a special case; here we truly demand that the value be 0 or 1 */ if (value[i] != PETSC_FALSE && value[i] != PETSC_TRUE) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Boolean value for %s %s is corrupt; integer value %d",nitem->name,nitem->help,value); ierr = PetscViewerASCIIPrintf(view," %s",PetscBools[value[i]]);CHKERRQ(ierr); } ierr = PetscViewerASCIIPrintf(view,"; %s\n",nitem->help);CHKERRQ(ierr); } else if (nitem->dtype == PETSC_ENUM) { PetscEnum value = *(PetscEnum*)(((char*)bag) + nitem->offset); PetscInt i = 0; while (nitem->list[i++]) ; ierr = PetscViewerASCIIPrintf(view," %s = %s; (%s) %s\n",nitem->name,nitem->list[value],nitem->list[i-3],nitem->help);CHKERRQ(ierr); } nitem = nitem->next; } } else if (isbinary) { PetscInt classid = PETSC_BAG_FILE_CLASSID, dtype; PetscInt deprecatedbagsize = 0; PetscViewerFormat format; ierr = PetscViewerBinaryWrite(view,&classid,1,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); ierr = PetscViewerBinaryWrite(view,&deprecatedbagsize,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); ierr = PetscViewerBinaryWrite(view,&bag->count,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); ierr = PetscViewerBinaryWrite(view,bag->bagname,PETSC_BAG_NAME_LENGTH,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); ierr = PetscViewerBinaryWrite(view,bag->baghelp,PETSC_BAG_HELP_LENGTH,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); while (nitem) { ierr = PetscViewerBinaryWrite(view,&nitem->offset,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); dtype = (PetscInt)nitem->dtype; ierr = PetscViewerBinaryWrite(view,&dtype,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); ierr = PetscViewerBinaryWrite(view,nitem->name,PETSC_BAG_NAME_LENGTH,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); ierr = PetscViewerBinaryWrite(view,nitem->help,PETSC_BAG_HELP_LENGTH,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); ierr = PetscViewerBinaryWrite(view,&nitem->msize,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); /* some Fortran compilers use -1 as boolean */ if (dtype == PETSC_BOOL && ((*(int*) (((char*)bag) + nitem->offset) == -1))) *(int*) (((char*)bag) + nitem->offset) = PETSC_TRUE; ierr = PetscViewerBinaryWrite(view,(((char*)bag) + nitem->offset),nitem->msize,nitem->dtype,PETSC_FALSE);CHKERRQ(ierr); if (dtype == PETSC_ENUM) { ierr = PetscViewerBinaryWriteStringArray(view,(char**)nitem->list);CHKERRQ(ierr); } nitem = nitem->next; } ierr = PetscViewerGetFormat(view,&format);CHKERRQ(ierr); if (format == PETSC_VIEWER_BINARY_MATLAB) { MPI_Comm comm; FILE *info; ierr = PetscObjectGetComm((PetscObject)view,&comm);CHKERRQ(ierr); ierr = PetscViewerBinaryGetInfoPointer(view,&info);CHKERRQ(ierr); ierr = PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr); ierr = PetscFPrintf(comm,info,"#$$ Set.%s = PetscBinaryRead(fd);\n",bag->bagname);CHKERRQ(ierr); ierr = PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr); } } PetscFunctionReturn(0); }
PetscErrorCode VecView_Seq_Binary(Vec xin,PetscViewer viewer) { PetscErrorCode ierr; int fdes; PetscInt n = xin->map->n,classid=VEC_FILE_CLASSID; FILE *file; const PetscScalar *xv; #if defined(PETSC_HAVE_MPIIO) PetscBool isMPIIO; #endif PetscBool skipHeader; PetscViewerFormat format; PetscFunctionBegin; /* Write vector header */ ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr); if (!skipHeader) { ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); ierr = PetscViewerBinaryWrite(viewer,&n,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); } /* Write vector contents */ #if defined(PETSC_HAVE_MPIIO) ierr = PetscViewerBinaryGetUseMPIIO(viewer,&isMPIIO);CHKERRQ(ierr); if (!isMPIIO) { #endif ierr = PetscViewerBinaryGetDescriptor(viewer,&fdes);CHKERRQ(ierr); ierr = VecGetArrayRead(xin,&xv);CHKERRQ(ierr); ierr = PetscBinaryWrite(fdes,(void*)xv,n,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr); ierr = VecRestoreArrayRead(xin,&xv);CHKERRQ(ierr); ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); if (format == PETSC_VIEWER_BINARY_MATLAB) { MPI_Comm comm; FILE *info; const char *name; ierr = PetscObjectGetName((PetscObject)xin,&name);CHKERRQ(ierr); ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); ierr = PetscViewerBinaryGetInfoPointer(viewer,&info);CHKERRQ(ierr); ierr = PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr); ierr = PetscFPrintf(comm,info,"#$$ Set.%s = PetscBinaryRead(fd);\n",name);CHKERRQ(ierr); ierr = PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr); } #if defined(PETSC_HAVE_MPIIO) } else { MPI_Offset off; MPI_File mfdes; PetscMPIInt lsize; ierr = PetscMPIIntCast(n,&lsize);CHKERRQ(ierr); ierr = PetscViewerBinaryGetMPIIODescriptor(viewer,&mfdes);CHKERRQ(ierr); ierr = PetscViewerBinaryGetMPIIOOffset(viewer,&off);CHKERRQ(ierr); ierr = MPI_File_set_view(mfdes,off,MPIU_SCALAR,MPIU_SCALAR,(char*)"native",MPI_INFO_NULL);CHKERRQ(ierr); ierr = VecGetArrayRead(xin,&xv);CHKERRQ(ierr); ierr = MPIU_File_write_all(mfdes,(void*)xv,lsize,MPIU_SCALAR,MPI_STATUS_IGNORE);CHKERRQ(ierr); ierr = VecRestoreArrayRead(xin,&xv);CHKERRQ(ierr); ierr = PetscViewerBinaryAddMPIIOOffset(viewer,n*sizeof(PetscScalar));CHKERRQ(ierr); } #endif ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr); if (file) { if (((PetscObject)xin)->prefix) { ierr = PetscFPrintf(PETSC_COMM_SELF,file,"-%svecload_block_size %D\n",((PetscObject)xin)->prefix,PetscAbs(xin->map->bs));CHKERRQ(ierr); } else { ierr = PetscFPrintf(PETSC_COMM_SELF,file,"-vecload_block_size %D\n",PetscAbs(xin->map->bs));CHKERRQ(ierr); } } PetscFunctionReturn(0); }