static PetscErrorCode TransferWrite(PetscViewer viewer,FILE *fp,PetscMPIInt srank,PetscMPIInt root,const void *send,void *recv,PetscMPIInt count,PetscDataType datatype,PetscMPIInt tag) { PetscMPIInt rank; PetscErrorCode ierr; MPI_Comm comm; MPI_Datatype mpidatatype; PetscFunctionBegin; ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); ierr = PetscDataTypeToMPIDataType(datatype,&mpidatatype);CHKERRQ(ierr); if (rank == srank && rank != root) { ierr = MPI_Send((void*)send,count,mpidatatype,root,tag,comm);CHKERRQ(ierr); } else if (rank == root) { const void *buffer; if (root == srank) { /* self */ buffer = send; } else { MPI_Status status; PetscMPIInt nrecv; ierr = MPI_Recv(recv,count,mpidatatype,srank,tag,comm,&status);CHKERRQ(ierr); ierr = MPI_Get_count(&status,mpidatatype,&nrecv);CHKERRQ(ierr); if (count != nrecv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Array size mismatch"); buffer = recv; } ierr = PetscViewerVTKFWrite(viewer,fp,buffer,count,datatype);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@C PetscBinarySynchronizedRead - Reads from a binary file. Collective on MPI_Comm Input Parameters: + comm - the MPI communicator . fd - the file . n - the number of items to read - type - the type of items to read (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR) Output Parameters: . p - the buffer Level: developer Notes: Does a PetscBinaryRead() followed by an MPI_Bcast() PetscBinarySynchronizedRead() uses byte swapping to work on all machines. Integers are stored on the file as 32 long, regardless of whether they are stored in the machine as 32 or 64, this means the same binary file may be read on any machine. Concepts: files^synchronized reading of binary files Concepts: binary files^reading, synchronized .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscBinaryRead(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedSeek() @*/ PetscErrorCode PetscBinarySynchronizedRead(MPI_Comm comm,int fd,void *p,PetscInt n,PetscDataType type) { PetscErrorCode ierr; PetscMPIInt rank; MPI_Datatype mtype; char *fname = NULL; void *ptmp = NULL; PetscFunctionBegin; if (type == PETSC_FUNCTION) { n = 64; type = PETSC_CHAR; ptmp = p; fname = (char*)malloc(n*sizeof(char)); if (!fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Cannot allocate space for function name"); p = (void*)fname; } ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { ierr = PetscBinaryRead(fd,p,n,type);CHKERRQ(ierr); } ierr = PetscDataTypeToMPIDataType(type,&mtype);CHKERRQ(ierr); ierr = MPI_Bcast(p,n,mtype,0,comm);CHKERRQ(ierr); if (type == PETSC_FUNCTION) { #if defined(PETSC_SERIALIZE_FUNCTIONS) ierr = PetscDLLibrarySym(PETSC_COMM_SELF,&PetscDLLibrariesLoaded,NULL,fname,(void**)ptmp);CHKERRQ(ierr); #else *(void**)ptmp = NULL; #endif free(fname); } PetscFunctionReturn(0); }
/*@C PetscBinarySynchronizedRead - Reads from a binary file. Collective on MPI_Comm Input Parameters: + comm - the MPI communicator . fd - the file . n - the number of items to read - type - the type of items to read (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR) Output Parameters: . p - the buffer Options Database Key: . -binary_longints - indicates the file was generated on a Cray vector machine (not the T3E/D) and the ints are stored as 64 bit quantities, otherwise they are stored as 32 bit Level: developer Notes: Does a PetscBinaryRead() followed by an MPI_Bcast() PetscBinarySynchronizedRead() uses byte swapping to work on all machines. Integers are stored on the file as 32 long, regardless of whether they are stored in the machine as 32 or 64, this means the same binary file may be read on any machine. Concepts: files^synchronized reading of binary files Concepts: binary files^reading, synchronized .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscBinaryRead(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedSeek() @*/ PetscErrorCode PETSC_DLLEXPORT PetscBinarySynchronizedRead(MPI_Comm comm,int fd,void *p,PetscInt n,PetscDataType type) { PetscErrorCode ierr; PetscMPIInt rank; MPI_Datatype mtype; PetscFunctionBegin; ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { ierr = PetscBinaryRead(fd,p,n,type);CHKERRQ(ierr); } ierr = PetscDataTypeToMPIDataType(type,&mtype);CHKERRQ(ierr); ierr = MPI_Bcast(p,n,mtype,0,comm);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode PetscViewerBinaryMPIIO(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool write) { PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; PetscErrorCode ierr; MPI_Datatype mdtype; PetscMPIInt cnt = PetscMPIIntCast(count); MPI_Status status; MPI_Aint ul,dsize; PetscFunctionBegin; ierr = PetscDataTypeToMPIDataType(dtype,&mdtype);CHKERRQ(ierr); ierr = MPI_File_set_view(vbinary->mfdes,vbinary->moff,mdtype,mdtype,(char *)"native",MPI_INFO_NULL);CHKERRQ(ierr); if (write) { ierr = MPIU_File_write_all(vbinary->mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr); } else { ierr = MPIU_File_read_all(vbinary->mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr); } ierr = MPI_Type_get_extent(mdtype,&ul,&dsize);CHKERRQ(ierr); vbinary->moff += dsize*cnt; PetscFunctionReturn(0); }
/*@C PetscBinarySynchronizedRead - Reads from a binary file. Collective on MPI_Comm Input Parameters: + comm - the MPI communicator . fd - the file descriptor . num - the maximum number of items to read - type - the type of items to read (PETSC_INT, PETSC_REAL, PETSC_SCALAR, etc.) Output Parameters: + data - the buffer - count - the number of items read, optional Level: developer Notes: Does a PetscBinaryRead() followed by an MPI_Bcast() If count is not provided and the number of items read is less than the maximum number of items to read, then this routine errors. PetscBinarySynchronizedRead() uses byte swapping to work on all machines. Integers are stored on the file as 32 long, regardless of whether they are stored in the machine as 32 or 64, this means the same binary file may be read on any machine. Concepts: files^synchronized reading of binary files Concepts: binary files^reading, synchronized .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscBinaryRead(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedSeek() @*/ PetscErrorCode PetscBinarySynchronizedRead(MPI_Comm comm,int fd,void *data,PetscInt num,PetscInt *count,PetscDataType type) { PetscErrorCode ierr; PetscMPIInt rank; MPI_Datatype mtype; PetscInt ibuf[2] = {0, 0}; char *fname = NULL; void *fptr = NULL; PetscFunctionBegin; if (type == PETSC_FUNCTION) { num = 64; type = PETSC_CHAR; fname = (char*)malloc(num*sizeof(char)); fptr = data; data = (void*)fname; if (!fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Cannot allocate space for function name"); } ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { ibuf[0] = PetscBinaryRead(fd,data,num,count?&ibuf[1]:NULL,type); } ierr = MPI_Bcast(ibuf,2,MPIU_INT,0,comm);CHKERRQ(ierr); ierr = (PetscErrorCode)ibuf[0];CHKERRQ(ierr); ierr = PetscDataTypeToMPIDataType(type,&mtype);CHKERRQ(ierr); ierr = MPI_Bcast(data,count?ibuf[1]:num,mtype,0,comm);CHKERRQ(ierr); if (count) *count = ibuf[1]; if (type == PETSC_FUNCTION) { #if defined(PETSC_SERIALIZE_FUNCTIONS) ierr = PetscDLLibrarySym(PETSC_COMM_SELF,&PetscDLLibrariesLoaded,NULL,fname,(void**)fptr);CHKERRQ(ierr); #else *(void**)fptr = NULL; #endif free(fname); } PetscFunctionReturn(0); }
/*@C PetscBinarySynchronizedRead - Reads from a binary file. Collective on MPI_Comm Input Parameters: + comm - the MPI communicator . fd - the file . n - the number of items to read - type - the type of items to read (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR) Output Parameters: . p - the buffer Options Database Key: . -binary_longints - indicates the file was generated on a Cray vector machine (not the T3E/D) and the ints are stored as 64 bit quantities, otherwise they are stored as 32 bit Level: developer Notes: Does a PetscBinaryRead() followed by an MPI_Bcast() PetscBinarySynchronizedRead() uses byte swapping to work on all machines. Integers are stored on the file as 32 long, regardless of whether they are stored in the machine as 32 or 64, this means the same binary file may be read on any machine. Concepts: files^synchronized reading of binary files Concepts: binary files^reading, synchronized .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscBinaryRead(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedSeek() @*/ PetscErrorCode PetscBinarySynchronizedRead(MPI_Comm comm,int fd,void *p,PetscInt n,PetscDataType type) { PetscErrorCode ierr; PetscMPIInt rank; MPI_Datatype mtype; char *fname; PetscBool functionload = PETSC_FALSE; void *ptmp = NULL; PetscFunctionBegin; if (type == PETSC_FUNCTION) { functionload = PETSC_TRUE; n = 64; type = PETSC_CHAR; ptmp = p; /* warning memory leak */ fname = (char*)malloc(64*sizeof(char)); p = (void*)fname; } ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { ierr = PetscBinaryRead(fd,p,n,type);CHKERRQ(ierr); } ierr = PetscDataTypeToMPIDataType(type,&mtype);CHKERRQ(ierr); ierr = MPI_Bcast(p,n,mtype,0,comm);CHKERRQ(ierr); if (functionload) { #if defined(PETSC_SERIALIZE_FUNCTIONS) ierr = PetscDLLibrarySym(PETSC_COMM_SELF,&PetscDLLibrariesLoaded,NULL,fname,(void**)ptmp);CHKERRQ(ierr); #else *(void**)ptmp = NULL; #endif } PetscFunctionReturn(0); }